maven依赖:
org.apache.httpcomponents
httpclient
4.4
org.apache.httpcomponents
httpmime
4.4
package com.xinyuan.haze.common.utils;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import
org.apache.http.client.ClientProtocolException;
import
org.apache.http.client.entity.UrlEncodedFormEntity;
import
org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import
org.apache.http.entity.mime.MultipartEntityBuilder;
import
org.apache.http.entity.mime.content.FileBody;
import
org.apache.http.entity.mime.content.StringBody;
import
org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import
org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
public class HttpClientUtils {
//post提交表单
public static String post(String url, Map params)
{
String body = null;
// 创建默认的httpClient实例.
CloseableHttpClient httpclient =
HttpClients.createDefault();
// 创建httppost
HttpPost httppost = new
HttpPost("http://localhost:8080/myDemo/Ajax/serivceJ.action");
// 创建参数队列
List formparams = new ArrayList();
formparams.add(new BasicNameValuePair("type",
"house"));
UrlEncodedFormEntity uefEntity;
try {
uefEntity
= new UrlEncodedFormEntity(formparams, "UTF-8");
httppost.setEntity(uefEntity);
System.out.println("executing request " + httppost.getURI());
CloseableHttpResponse response = httpclient.execute(httppost);
try {
HttpEntity entity =
response.getEntity();
if (entity != null) {
System.out.println("--------------------------------------");
System.out.println("Response content: " +
EntityUtils.toString(entity, "UTF-8"));
System.out.println("--------------------------------------");
}
} finally
{
response.close();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
//
关闭连接,释放资源
try {
httpclient.close();
} catch
(IOException e) {
e.printStackTrace();
}
}
return body;
}
//给请求
public static String
get(String url) {
String body =
null;
CloseableHttpClient
httpclient =
HttpClients.createDefault();
CloseableHttpResponse response = null;
try {
// 创建httpget.
HttpGet
httpget = new HttpGet(url);
System.out.println("executing request " +
httpget.getURI());
// 执行get请求.
response = httpclient.execute(httpget);
// 获取响应实体
HttpEntity
entity = response.getEntity();
System.out.println("--------------------------------------");
// 打印响应状态
System.out.println(response.getStatusLine());
if (entity
!= null) {
// 打印响应内容长度
System.out.println("Response
content length: " + entity.getContentLength());
// 打印响应内容
System.out.println("Response
content: " + EntityUtils.toString(entity));
}
System.out.println("------------------------------------");
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
//
关闭连接,释放资源
try {
httpclient.close();
} catch
(IOException e) {
e.printStackTrace();
}
if(response != null){
try
{
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return body;
}
public void upload()
{
CloseableHttpClient httpclient =
HttpClients.createDefault();
try {
HttpPost
httppost = new
HttpPost("http://localhost:8080/myDemo/Ajax/serivceFile.action");
FileBody
bin = new FileBody(new File("F:\\image\\sendpix0.jpg"));
StringBody
comment = new StringBody("A binary file of some kind",
ContentType.TEXT_PLAIN);
HttpEntity
reqEntity = MultipartEntityBuilder.create().addPart("bin",
bin).addPart("comment", comment).build();
httppost.setEntity(reqEntity);
System.out.println("executing request " +
httppost.getRequestLine());
CloseableHttpResponse response = httpclient.execute(httppost);
try {
System.out.println("----------------------------------------");
System.out.println(response.getStatusLine());
HttpEntity resEntity =
response.getEntity();
if (resEntity != null) {
System.out.println("Response content length: " +
resEntity.getContentLength());
}
EntityUtils.consume(resEntity);
} finally
{
response.close();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
httpclient.close();
} catch
(IOException e) {
e.printStackTrace();
}
}
}
}