当前位置 : 主页 > 编程语言 > java >

http带json,链接带参数

来源:互联网 收集:自由互联 发布时间:2021-06-28
http带json,链接带参数 public static String sendPost(String url, String data) { String response = null; try { CloseableHttpClient httpclient = null; CloseableHttpResponse httpresponse = null; try { httpclient = HttpClients.createDefa
http带json,链接带参数
public static String sendPost(String url, String data) {
		    String response = null;
		    try {
		      CloseableHttpClient httpclient = null;
		      CloseableHttpResponse httpresponse = null;
		      try {
		        httpclient = HttpClients.createDefault();
		        HttpPost httppost = new HttpPost(url);
		        StringEntity stringentity = new StringEntity(data,
		            ContentType.create("text/json", "UTF-8"));
		        httppost.setEntity(stringentity);
		        httpresponse = httpclient.execute(httppost);
		        response = EntityUtils
		            .toString(httpresponse.getEntity());
		      } finally {
		        if (httpclient != null) {
		          httpclient.close();
		        }
		        if (httpresponse != null) {
		          httpresponse.close();
		        }
		      }
		    } catch (Exception e) {
		      e.printStackTrace();
		    }
		    return response;
		  }
网友评论