现状描述:项目涉及到与第三方交互的内容,调用对方的接口,采用HTTPS协议的方式,只能够用HttpClient包进行请求了,同时由于请求的URL是HTTPS的,为了避免需要证书,所以用一个类继承DefaultHttpClient类,忽略校验过程。
jar包:httpclient-4.3.4.jar
代码示例:
//创建默认HttpClient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
//创建httppost
HttpPost post = new HttpPost("http://xxx.xxx.xxx.xxx:xx/xxx/");
//创建参数队列
List list = new ArrayList();
list.add(new BasicNameValuePair("userName", "13688889999"));
list.add(new BasicNameValuePair("userPasswd", "88889999"));
list.add(new BasicNameValuePair("kaptcha", "2d3f"));
CloseableHttpResponse response = null;
try {
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list,"utf-8");
post.setEntity(entity);
System.out.println(post.getURI()+"@@@@@@@@@@@@@@");
//执行
response = httpClient.execute(post);
System.out.println("##########" + response.getStatusLine().getStatusCode());
//获取返回实体
HttpEntity httpEntity = response.getEntity();
String string = EntityUtils.toString(httpEntity);
System.out.println(string);
} catch (Exception e) {
e.printStackTrace();
} finally {
//释放资源
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}