java httpclient 发起post请求 并且添加 fiddler代理抓包,处理字符集

此博客为:fiddler 抓包  java  httpclient 发起post请求

从其他博客看到的方法:

 

1. jvm 设置启动代理参数

-DproxySet=true
 
-DproxyHost=127.0.0.1
 
-DproxyPort=8888

失败!

 

2.代码中设置代理

System.setProperty("http.proxySet", "true");

System.setProperty("http.proxyHost", "127.0.0.1");

System.setProperty("http.proxyPort", "8888");

失败!

 

解决问题:

             1. 发起的 请求体 用 StringEntity 类创建,并且加入utf-8字符集,否则中文在抓包数据中乱码

                     String body = "{\"config\":{\"wo张三\":\"123\",\"mac\":\"FC:2F:EF:72:5F:43\"}}";

           StringEntity entity = new StringEntity(body,"utf-8");

             2. fiddler 抓包java发起的请求。需要先开启fiddler,设置代理端口为 8888,或者其他

              代理设置的正确姿势:

             3.代码中添加代理 使用HttpHost创建代理,

           HttpHost proxy = new HttpHost("127.0.0.1",8888)                               
           response =  client.execute(proxy,httpPost);

                     这个问题花了1天时间,以此纪念

 

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.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.StatusLine;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;



public class test{

    public void testpost() throws IOException{
        
        //普通的client 设置方法
        CloseableHttpClient client = HttpClients.createDefault();

        String entityStr = null;

        CloseableHttpResponse response = null;

        String url = "http://192.168.10.16/api/ilink/device/gwpolicy_authpolicy_add?lang=zh-cn&token=8cwylf6att9t9njva4cyx3ghqcfth4tv";

        //传送json类型的字符串的请求体
        String body = "{\"config\":{\"wo张三\":\"123\",\"mac\":\"FC:2F:EF:72:5F:43\"}}";

        //构建请求实体,utf-8字符集
        StringEntity entity = new StringEntity(body,"utf-8");

        HttpPost httpPost = new HttpPost(url) ;


        //添加请求头
        httpPost.addHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36");
        httpPost.addHeader("Content-Type","application/json;charset=UTF-8");


        //添加实体到post请求中
        httpPost.setEntity(entity);

        //fiddler 抓包 开启的代理,若不需要,则注释,client.execute()的参数就不传入此代理
        HttpHost proxy = new HttpHost("127.0.0.1",8888);

        //发起请求
        response =  client.execute(proxy,httpPost);


        //获取响应状态码
        StatusLine statusLine = response.getStatusLine();
        int statu = statusLine.getStatusCode();
        System.out.println(statu);

        //获取响应实体
        HttpEntity rentity = response.getEntity();


        //响应实体解码
        entityStr = EntityUtils.toString(rentity, "UTF-8");
        System.out.println(entityStr);

    }


}



 

 

你可能感兴趣的:(java httpclient 发起post请求 并且添加 fiddler代理抓包,处理字符集)