使用Fiddler捕获java的网络通信数据

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);



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值