介绍
这篇文章将为大家详细讲解有关如何在java中利用HttpClient对json格式的参数进行传输,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
重点是这两句话:
httpPost.setHeader (“Content-Type",,“application/json; charset=UTF-8"); se.setContentType (CONTENT_TYPE_TEXT_JSON);
这两句话的作用与jmeter的设置头信息类似
package com.base; import java.io.UnsupportedEncodingException; import org.apache.http.HttpEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.impl.conn.PoolingClientConnectionManager; import org.apache.http.util.EntityUtils;/* *, ,* @author QiaoJiafei ,* @version 创建时间:2015年11月4日,下午1:55:45 ,*类说明, ,*/public class  HttpGetByJson { ,public static  void 主要(String 参数[]),throws 异常{ final 才能String CONTENT_TYPE_TEXT_JSON =,“文本/json"; DefaultHttpClient 才能;client =, new DefaultHttpClient ( ,,new PoolingClientConnectionManager ()); ,, String 才能;url =,“http://172.16.30.226:8091 svc/认证/register"; ,String js =,“{\“用户名\“:\“18600363833 \“,\“validateChar \“: \“706923 \“, \“randomChar \“: \“706923 \“, \“密码\“:\“123456 \“,\“confirmPwd \“: \“123456 \“, \“recommendMobile \“: \“\”, \“经办人身份证\“:\“320601197608285792 \”,\“realName \“: \“阙岩\”,\“verifyCode \“}”; ,, ,HttpPost HttpPost =, new HttpPost (url),, ,httpPost.setHeader (“Content-Type",,“application/json; charset=UTF-8"); ,, ,StringEntity se =, new StringEntity (js); ,se.setContentType (CONTENT_TYPE_TEXT_JSON); ,httpPost.setEntity (se); , ,CloseableHttpResponse response2 =,空; ,=,,response2  client.execute (httpPost); ,HttpEntity entity2 =,空;=,,entity2  response2.getEntity (); ,String s2 =, EntityUtils.toString (entity2,“UTF-8"); ,System.out.println (s2); ,} , }
<>强补充:HttpClient以json形式的参数调用http接口并对返回的json数据进行处理(可以带文件)强>
1,参数的url就是被调用的地址,地图是你要传的参数。参数转成json我使用的是gson方式转换的。
主要使用的jar包有httpclient-4.5.3.jar, httpcore-4.4.6.jar, commons-codec-1.9.jar, gson-2.2.4.jar和commons-logging-1.2.jar。
如果发送的帖子请求想传送文件,需添加httpmime-4.5.3。jar包,并设置如下代码:
HttpEntity multipartEntityBuilder =, MultipartEntityBuilder.create () .addBinaryBody (“file",, new 文件(“D: \ \ workspace \ \程序\ \ WebContent \ \程序\ \ 1991. zip")) .build ();
第一个参数表示请求字段名,第二个参数就是文件。
还想添加参数则
HttpEntity multipartEntityBuilder =, MultipartEntityBuilder.create () .addTextBody (“name",,“张三“).addBinaryBody (“file",, new 文件(“D: \ \ workspace \ \程序\ \ WebContent \ \程序\ \ 1991. zip")) .build (); httpPost.setEntity (multipartEntityBuilder);
import java.io.IOException; import java.util.Map; import org.apache.http.HttpEntity; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import com.google.gson.Gson; public class  HttpClientUtil { , ,private final  static String  CONTENT_TYPE_TEXT_JSON =,“文本/json"; , ,public static  String postRequest (String url, Map<字符串,Object>,参数),throws ClientProtocolException, IOException { , ,CloseableHttpClient client =, HttpClients.createDefault (); ,HttpPost HttpPost =, new HttpPost (url); ,httpPost.setHeader (“Content-Type",,“application/json; charset=UTF-8"); , ,Gson Gson =, new Gson (); ,String parameter =, gson.toJson(参数); ,StringEntity se =, new StringEntity(参数); ,se.setContentType (CONTENT_TYPE_TEXT_JSON); ,httpPost.setEntity (se); ,CloseableHttpResponse response =, client.execute (httpPost); null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null如何在java中利用HttpClient对json格式的参数进行传输