本篇文章给大家分享的是有关怎么在Java中发送post,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
1、post请求用于发送json 格式的参数:
/** * post请求(用于请求json格式的参数) * * @param url 地址 * @param params json格式的参数 * @return */ public static String doPost(String url, String params) throws Exception { CloseableHttpClient httpclient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost( url );// 创建httpPost httpPost.setHeader( "Accept", "application/json" ); httpPost.setHeader( "Content-Type", "application/json" ); String charSet = "UTF-8"; StringEntity entity = new StringEntity( params, charSet ); httpPost.setEntity( entity ); CloseableHttpResponse response = null; try { response = httpclient.execute( httpPost ); StatusLine status = response.getStatusLine(); int state = status.getStatusCode(); if (state == HttpStatus.SC_OK) { HttpEntity responseEntity = response.getEntity(); String jsonString = EntityUtils.toString( responseEntity ); return jsonString; } else { logger.error( "请求返回:" + state + "(" + url + ")" ); } } finally { if (response != null) { try { response.close(); } catch (IOException e) { e.printStackTrace(); } } try { httpclient.close(); } catch (IOException e) { e.printStackTrace(); } } return null; }
2、用于发送key-value格式的参数
/** * post请求(用于key-value格式的参数) * * @param url * @param params * @return */ public static String doPost(String url, Map params) { BufferedReader in = null; try { // 定义HttpClient HttpClient client = new DefaultHttpClient(); // 实例化HTTP方法 HttpPost request = new HttpPost(); request.setURI( new URI( url ) ); //设置参数 List<NameValuePair> nvps = new ArrayList<NameValuePair>(); for (Iterator iter = params.keySet().iterator(); iter.hasNext(); ) { String name = (String) iter.next(); String value = String.valueOf( params.get( name ) ); nvps.add( new BasicNameValuePair( name, value ) ); //System.out.println(name +"-"+value); } request.setEntity( new UrlEncodedFormEntity( nvps, HTTP.UTF_8 ) ); HttpResponse response = client.execute( request ); int code = response.getStatusLine().getStatusCode(); if (code == 200) { //请求成功 in = new BufferedReader( new InputStreamReader( response.getEntity() .getContent(), "utf-8" ) ); StringBuffer sb = new StringBuffer( "" ); String line = ""; String NL = System.getProperty( "line.separator" ); while ((line = in.readLine()) != null) { sb.append( line + NL ); } in.close(); return sb.toString(); } else { // System.out.println( "状态码:" + code ); return null; } } catch (Exception e) { e.printStackTrace(); return null; } }
第三,发送get请求
/** * get请求 * * @return */ public static String doGet(String url) { try { HttpClient client = new DefaultHttpClient(); //发送get请求 HttpGet request = new HttpGet( url ); HttpResponse response = client.execute( request ); /**请求发送成功,并得到响应**/ if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { /**读取服务器返回过来的json字符串数据**/ String strResult = EntityUtils.toString( response.getEntity() ); return strResult; } } catch (IOException e) { e.printStackTrace(); } return null; }
Java的特点有哪些 1.Java语言作为静态面向对象编程语言的代表,实现了面向对象理论,允许程序员以优雅的思维方式进行复杂的编程。 2.Java具有简单性、面向对象、分布式、安全性、平台独立与可移植性、动态性等特点。 3.使用Java可以编写桌面应用程序、Web应用程序、分布式系统和嵌入式系统应用程序等。
以上就是怎么在Java中发送post,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。