HttpClient发送get请求,post请求,携带cookie访问,json提交
HttpClient发送get请求,post请求,携带cookie访问,json提交
春哥大魔王 发表于3个月前
HttpClient发送get请求,post请求,携带cookie访问,json提交
  • 发表于 3个月前
  • 阅读 37
  • 收藏 1
  • 点赞 0
  • 评论 0

腾讯云 新注册用户 域名抢购1元起>>>   

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);
        }
    }
共有 人打赏支持
粉丝 6
博文 46
码字总数 20422
×
春哥大魔王
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: