Java代码里利用Fiddler抓包调试设置

Fiddler启动时已经将自己注册为系统的默认代理服务器,应用程序在访问网络时会去获取系统的默认代理,如果需要捕获java访问网络时的数据,只需要在启动java程序时设置代理服务器为Fiddler即可

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

-D 是java中设置系统变量的方式
下面的帖子很清楚的说明了设置Fiddler为http或者https的代理,包括如何设置https的证书

http://stackoverflow.com/questions/8549749/how-to-capture-https-with-fiddler-in-java

这是官网对这方面的描述:http://docs.oracle.com/javase/6/docs/technotes/guides/net/proxies.html

http://www.chentaoqian.com/archives/149

也可以通过程序进行设置(注意:设置代理一定要在程序请求之前)

System.setProperty("http.proxySet", "true");
 
System.setProperty("http.proxyHost", "127.0.0.1");
 
System.setProperty("http.proxyPort", "8888");

在Node Js中这样就行了

var http = require('http');var opt = {host:'127.0.0.1',port:'8888',method:'POST',path:'http://localhost/php/firstPro/test/weitest.php'}var body = '';var req = http.request(opt,function(res){console.log('Got Response'+ res.statusCode);res.on('data',function(d){ body += d; }).on('end',function(){console.log(res.headers)console.log(body);})});req.write("some data");req.end();

java http 程序设置代理

如果直接用java的 http API进行代理设置,如果是访问本地 ,则要在localhost后面加上. ,这应该是基于安全还是其他什么原因 ,如 http://localhost.:8080/app/index.jsp

    public static String writeBytes(String url ,String data) throws Exception{
        
        System.setProperty("http.proxySet", "true");
 
        System.setProperty("http.proxyHost", "127.0.0.1");
 
        System.setProperty("http.proxyPort", "8888");
        
        HttpURLConnection httpURLConnection = (HttpURLConnection)new URL(url).openConnection();
        
//        httpURLConnection.connect();
        httpURLConnection.setRequestProperty("Accept-Charset", "utf-8");
        httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        httpURLConnection.setDoInput(true);    //true表示允许获得输入流,读取服务器响应的数据,该属性默认值为true  
        httpURLConnection.setDoOutput(true);   //true表示允许获得输出流,向远程服务器发送数据,该属性默认值为false  
        httpURLConnection.setUseCaches(false); //禁止缓存  
        httpURLConnection.setReadTimeout(30000);    //30秒读取超时  
        httpURLConnection.setConnectTimeout(30000); //30秒连接超时  
        httpURLConnection.setRequestMethod("POST");  
        
        httpURLConnection.setRequestProperty("Content-Length", data.length() + "");
        
        OutputStream writer = httpURLConnection.getOutputStream();
        writer.write(data.getBytes());
//        writer.write( new byte[]{13,10,13,10} );
        writer.flush();
        
        InputStream in = httpURLConnection.getInputStream();  
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();  
        byte[] buff = new byte[1024];  
        int len = -1;  
        while((len= in.read(buff)) != -1){  
            buffer.write(buff, 0, len);  
        }  
        String result  = buffer.toString("utf-8");  
        
        System.out.println(result);
        
        return result;
    }

用HttpClient进行代理

HttpClient httpclient = new HttpClient(connectionManager);     
HttpHost proxy = new HttpHost("127.0.0.1",8888);  
httpclient.getParams().setParameter(ConnRouteParams.DEFAULT_PROXY, proxy);

httpclient4 是这样设置,但是在commons-httpclient-3.1.jar 这个是无效的,这样就可以

httpclient.getHostConfiguration().setProxy("localhost", 8888);

 

posted @ 2019-01-14 20:51  夏威夷8080  阅读(4802)  评论(0编辑  收藏  举报