发送请求
RequestConfig config = RequestConfig.custom()
.setConnectTimeout(5000)
.setConnectionRequestTimeout(5000)
.setSocketTimeout(5000)
.build();
HttpClient client = HttpClientBuilder.create().build();
// get请求
// HttpResponse response = client.execute(new HttpGet("http://blog.csdn.net/penyoudi1/article/details/73609049"));
// 参数
List<BasicNameValuePair> params = Arrays.asList(
new BasicNameValuePair("page", "1"),
new BasicNameValuePair("rows", "20"));
// post请求
HttpUriRequest requestBuilder = RequestBuilder
.post()
.setUri("http://api.shanjiezhifu.com/kadi/goods/list")
.addParameters(params.toArray(new NameValuePair[params.size()]))
.setConfig(config).build();
HttpResponse response = client.execute(requestBuilder);
if (response.getStatusLine().getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
String string = EntityUtils.toString(entity, "utf8");
System.out.println(string);
} else {
System.out.println(response.getStatusLine().getReasonPhrase());
throw new Exception("请求失败");
}
网友评论