怎么在Java中发送post
本篇文章给大家分享的是有关怎么在Java中发送post,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
成都创新互联是一家专注于成都做网站、成都网站建设与策划设计,保亭黎族网站建设哪家好?成都创新互联做网站,专注于网站建设十多年,网设计领域的专业建站公司;建站业务涵盖:保亭黎族等地区。保亭黎族做网站价格咨询:13518219792
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 ) ); //设置参数 Listnvps = new ArrayList (); 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的特点有哪些
Java的特点有哪些 1.Java语言作为静态面向对象编程语言的代表,实现了面向对象理论,允许程序员以优雅的思维方式进行复杂的编程。 2.Java具有简单性、面向对象、分布式、安全性、平台独立与可移植性、动态性等特点。 3.使用Java可以编写桌面应用程序、Web应用程序、分布式系统和嵌入式系统应用程序等。
以上就是怎么在Java中发送post,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注创新互联行业资讯频道。
网页标题:怎么在Java中发送post
转载注明:http://lswzjz.com/article/jejcdd.html