1.导入HttpCLient的jar包
<dependencies>
<dependency>
<groupId>org.apache.httpcomponents
</groupId>
<artifactId>httpclient
</artifactId>
<version>4.5.2
</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents
</groupId>
<artifactId>httpcore
</artifactId>
<version>4.4.6
</version>
</dependency>
</dependencies>
以下会依次列举常见的几种请求类型 : get , post param , post json , post file
2.GET
public static void testGet(String url
) throws ClientProtocolException
, IOException
{
RequestConfig defaultRequestConfig
= RequestConfig
.custom().setSocketTimeout(3000).setConnectTimeout(3000)
.setConnectionRequestTimeout(3000).build();
CloseableHttpClient httpclient
= HttpClients
.createDefault();
try {
HttpGet httpget
= new HttpGet(url
);
httpget
.setConfig(defaultRequestConfig
);
ResponseHandler
<String> responseHandler
= new ResponseHandler<String>() {
public String
handleResponse(HttpResponse response
) throws ClientProtocolException
, IOException
{
int status
= response
.getStatusLine().getStatusCode();
if (status
>= 200 && status
< 300) {
HttpEntity entity
= response
.getEntity();
return entity
!= null
? EntityUtils
.toString(entity
) : null
;
} else {
throw new ClientProtocolException("Unexpected response status: " + status
);
}
}
};
String responseBody
= httpclient
.execute(httpget
, responseHandler
);
System
.out
.println(responseBody
);
} finally {
httpclient
.close();
}
}
3.POST 参数
public static void testPostParam(String url
,Map
<String,String> map
) throws ClientProtocolException
, IOException
{
RequestConfig defaultRequestConfig
= RequestConfig
.custom().setSocketTimeout(3000).setConnectTimeout(3000)
.setConnectionRequestTimeout(3000).build();
CloseableHttpClient httpClient
= HttpClients
.custom().setDefaultRequestConfig(defaultRequestConfig
).build();
httpClient
= HttpClients
.createDefault();
HttpPost post
= new HttpPost("http://httpbin.org/post");
post
.setConfig(defaultRequestConfig
);
List
<BasicNameValuePair> list
= new ArrayList<BasicNameValuePair>();
for(Entry
<String,string> entry
: map
.entrySet()){
list
.add(new BasicNameValuePair(entry
.getKey(), entry
.getValue()));
}
HttpEntity ent
= new UrlEncodedFormEntity(list
, "UTF-8");
post
.setEntity(ent
);
ResponseHandler
<String> responseHandler
= new ResponseHandler<String>() {
public String
handleResponse(HttpResponse response
) throws ClientProtocolException
, IOException
{
int status
= response
.getStatusLine().getStatusCode();
System
.out
.println(response
.getEntity().toString());
if (status
>= 200 && status
< 300) {
HttpEntity entity
= response
.getEntity();
return entity
!= null
? EntityUtils
.toString(entity
) : null
;
} else {
throw new ClientProtocolException("Unexpected response status: " + status
);
}
}
};
System
.out
.println(httpClient
.execute(post
, responseHandler
));
}
4.POST JSON
public static void testPostJson(String url
,String jsonStr
) throws ClientProtocolException
, IOException
{
RequestConfig defaultRequestConfig
= RequestConfig
.custom().setSocketTimeout(3000).setConnectTimeout(3000)
.setConnectionRequestTimeout(3000).build();
CloseableHttpClient httpClient
= HttpClients
.custom().setDefaultRequestConfig(defaultRequestConfig
).build();
httpClient
= HttpClients
.createDefault();
HttpPost post
= new HttpPost(url
);
post
.setConfig(defaultRequestConfig
);
StringEntity stringEntity
= new StringEntity(jsonStr
);
stringEntity
.setContentType("text/json");
stringEntity
.setContentEncoding(new BasicHeader(HTTP
.CONTENT_TYPE
, "application/json"));
post
.setEntity(stringEntity
);
ResponseHandler
<String> responseHandler
= new ResponseHandler<String>() {
public String
handleResponse(HttpResponse response
) throws ClientProtocolException
, IOException
{
int status
= response
.getStatusLine().getStatusCode();
if (status
>= 200 && status
< 300) {
HttpEntity entity
= response
.getEntity();
return entity
!= null
? EntityUtils
.toString(entity
) : null
;
} else {
throw new ClientProtocolException("Unexpected response status: " + status
);
}
}
};
System
.out
.println(httpClient
.execute(post
, responseHandler
));
}
5.POST FILE
public static void testPostFile(String url
,String filePath
,string fileName
) throws ClientProtocolException
, IOException
{
RequestConfig defaultRequestConfig
= RequestConfig
.custom().setSocketTimeout(5000).setConnectTimeout(3000)
.setConnectionRequestTimeout(3000).build();
CloseableHttpClient httpClient
= HttpClients
.custom().setDefaultRequestConfig(defaultRequestConfig
).build();
httpClient
= HttpClients
.createDefault();
HttpPost post
= new HttpPost(url
);
post
.setConfig(defaultRequestConfig
);
MultipartEntityBuilder builder
= MultipartEntityBuilder
.create();
builder
.setMode(HttpMultipartMode
.BROWSER_COMPATIBLE
);
builder
.addTextBody("name", "test");
builder
.addBinaryBody("file", new File(filePath
), ContentType
.DEFAULT_BINARY
,
fileName
);
post
.setEntity(builder
.build());
ResponseHandler
<String> responseHandler
= new ResponseHandler<String>() {
public String
handleResponse(HttpResponse response
) throws ClientProtocolException
, IOException
{
int status
= response
.getStatusLine().getStatusCode();
if (status
>= 200 && status
< 300) {
HttpEntity entity
= response
.getEntity();
return entity
!= null
? EntityUtils
.toString(entity
) : null
;
} else {
throw new ClientProtocolException("Unexpected response status: " + status
);
}
}
};
System
.out
.println(httpClient
.execute(post
, responseHandler
));
}