您的位置:首页 > 理论基础 > 计算机网络

HttpClient发送get请求,post请求,携带cookie访问,json提交

2017-09-06 00:00 1056 查看
POM:

<!-- http client-->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5</version>
</dependency>

HttpClientUtil:

GET:

public static String get(String urlNameString) {
String result = "";
CookieStore cookieStore = new BasicCookieStore();
CloseableHttpClient httpClient = HttpClients.custom()
.setDefaultCookieStore(cookieStore)
.build();
try {
HttpGet get = new HttpGet(urlNameString);//这里发送get请求
BasicClientCookie cookie = new BasicClientCookie("token", "49ba41f0-862b-4c31-add8-b07066b777d9");
cookie.setDomain("xxx.com.cn");
cookie.setPath("/");
cookieStore.addCookie(cookie);
// 通过请求对象获取响应对象
HttpResponse response = httpClient.execute(get);
// 判断网络连接状态码是否正常(0--200都数正常)
result = EntityUtils.toString(response.getEntity(), "utf-8");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}

POST:

public static String post(String urlNameString, Integer id, Boolean bo) {
String result = "";
CookieStore cookieStore = new BasicCookieStore();
CloseableHttpClient httpClient = HttpClients.custom()
.setDefaultCookieStore(cookieStore)
.build();
try {
StringEntity entity = new StringEntity("{\"id\":\"" + id + "\",\"enable\":" + bo + "}", "utf-8");//解决中文乱码问题
entity.setContentEncoding("UTF-8");
entity.setContentType("application/json");

HttpPost post = new HttpPost(urlNameString);//这里发送get请求
post.setEntity(entity);

BasicClientCookie cookie = new BasicClientCookie("token", "f9ed047d-f708-4cda-b1bc-d6abccbde576");
cookie.setDomain("xxx.com.cn");
cookie.setPath("/");
cookieStore.addCookie(cookie);
// 通过请求对象获取响应对象
HttpResponse response = httpClient.execute(post);
// 判断网络连接状态码是否正常(0--200都数正常)
result = EntityUtils.toString(response.getEntity(), "utf-8");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}

Post Josn

1     public static String httpPostWithJSON(String url) throws Exception {
2
3         HttpPost httpPost = new HttpPost(url);
4         CloseableHttpClient client = HttpClients.createDefault();
5         String respContent = null;
6
7 //        json方式
8         JSONObject jsonParam = new JSONObject();
9         jsonParam.put("name", "admin");
10         jsonParam.put("pass", "123456");
11         StringEntity entity = new StringEntity(jsonParam.toString(),"utf-8");//解决中文乱码问题
12         entity.setContentEncoding("UTF-8");
13         entity.setContentType("application/json");
14         httpPost.setEntity(entity);
15         System.out.println();
16
17
18 //        表单方式
19 //        List<BasicNameValuePair> pairList = new ArrayList<BasicNameValuePair>();
20 //        pairList.add(new BasicNameValuePair("name", "admin"));
21 //        pairList.add(new BasicNameValuePair("pass", "123456"));
22 //        httpPost.setEntity(new UrlEncodedFormEntity(pairList, "utf-8"));
23
24
25         HttpResponse resp = client.execute(httpPost);
26         if(resp.getStatusLine().getStatusCode() == 200) {
27             HttpEntity he = resp.getEntity();
28             respContent = EntityUtils.toString(he,"UTF-8");
29         }
30         return respContent;
31     }
32

调用:

public static void main(String[] args) throws IOException {

//result = HttpClientUtil.get(getUrl);

String data = "643,1839,2992,3890,3991,4002,4080,4270,4277,4333,4334,4401,4495,4501,4563,4573,4584,4653,4709,4717,4732,4766,4913,5424,5699,5705,6224,6235,6329,6342,6528,6529,6552,6555,6660,6667,6923,6970,8020,8146,8982,9354,9358,9364,9522,9554,9561,9960,10059,10188";

doForeach(Arrays.asList(data.split(",")));
}

private static void doForeach(List<String> ids) {
for (String id : ids) {
String result = HttpClientUtil.post(postUrl, Integer.parseInt(id), true);

System.out.println(result);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: