1、参数的url就是被调用的地址,map是你要传的参数。参数转成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。
如果发送的post请求想传送文件,需添加httpmime-4.5.3.jar包,并设置如下代码:
HttpEntity multipartEntityBuilder = MultipartEntityBuilder.create().addBinaryBody("file", new File("D:\\workspace\\programm\\WebContent\\programm\\1991.zip")).build();
第一个参数表示请求字段名,第二个参数就是文件。
还想添加参数则
HttpEntity multipartEntityBuilder = MultipartEntityBuilder.create().addTextBody("name", "张三").addBinaryBody("file", new File("D:\\workspace\\programm\\WebContent\\programm\\1991.zip")).build();
httpPost.setEntity(multipartEntityBuilder);
importjava.io.IOException;importjava.util.Map;importorg.apache.http.HttpEntity;importorg.apache.http.client.ClientProtocolException;importorg.apache.http.client.methods.CloseableHttpResponse;importorg.apache.http.client.methods.HttpPost;importorg.apache.http.entity.StringEntity;importorg.apache.http.impl.client.CloseableHttpClient;importorg.apache.http.impl.client.HttpClients;importorg.apache.http.util.EntityUtils;importcom.google.gson.Gson;public classHttpClientUtil {private final static String CONTENT_TYPE_TEXT_JSON = "text/json";public static String postRequest(String url, Map param) throwsClientProtocolException, IOException{
CloseableHttpClient client=HttpClients.createDefault();
HttpPost httpPost= newHttpPost(url);
httpPost.setHeader("Content-Type", "application/json;charset=UTF-8");
Gson gson= newGson();
String parameter=gson.toJson(param);
StringEntity se= newStringEntity(parameter);
se.setContentType(CONTENT_TYPE_TEXT_JSON);
httpPost.setEntity(se);
CloseableHttpResponse response=client.execute(httpPost);
HttpEntity entity=response.getEntity();
String result= EntityUtils.toString(entity, "UTF-8");returnresult;
}
}
2、返回的结果也可以使用gson转换成对象进行下一步操作。
importcom.google.gson.Gson;public classGsonUtil {public static T jsonToObject(String jsonData, Classtype) {
Gson gson= newGson();
T result=gson.fromJson(jsonData, type);returnresult;
}public static voidmain(String[] args) {
String json= "{'id':'1','name':'zhang','address':'Hubei'}";
jsonToObject(json, Person.class);
Person person= jsonToObject(json, Person.class);
System.out.println(person);
}
}
建立要转成的对象的类。
importjava.util.Date;public classPerson {private intid;privateString name;private intage;privateString address;public intgetId() {returnid;
}public void setId(intid) {this.id =id;
}publicString getName() {returnname;
}public voidsetName(String name) {this.name =name;
}public intgetAge() {returnage;
}public void setAge(intage) {this.age =age;
}publicString getAddress() {returnaddress;
}public voidsetAddress(String address) {this.address =address;
}@OverridepublicString toString() {return "Person [id=" + id + ", name=" + name + ", age=" + age + ", address=" + address + "]";
}
}
3、发送以键值对形式的参数的post请求
packagecom.avatarmind.httpclient;importjava.util.ArrayList;importjava.util.List;importorg.apache.http.HttpEntity;importorg.apache.http.NameValuePair;importorg.apache.http.client.entity.UrlEncodedFormEntity;importorg.apache.http.client.methods.CloseableHttpResponse;importorg.apache.http.client.methods.HttpPost;importorg.apache.http.impl.client.CloseableHttpClient;importorg.apache.http.impl.client.HttpClients;importorg.apache.http.message.BasicNameValuePair;importorg.apache.http.util.EntityUtils;public classHttpClient3 {public static void main(String[] args) throwsException {
CloseableHttpClient client=HttpClients.createDefault();
String url= "http://yuntuapi.amap.com/datamanage/table/create";
HttpPost httpPost= newHttpPost(url);//参数形式为key=value&key=value
List formparams = new ArrayList();
formparams.add(new BasicNameValuePair("key", "060212638b94290e3dd0648c15753b64"));
formparams.add(new BasicNameValuePair("name", "火狐"));//加utf-8进行编码
UrlEncodedFormEntity uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
httpPost.setEntity(uefEntity);
CloseableHttpResponse response=client.execute(httpPost);
HttpEntity entity=response.getEntity();
String result= EntityUtils.toString(entity, "UTF-8");
System.out.println(result);
}
}