配置 Eclipse 以允许 Fiddler 拦截请求
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5302476/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Configuring Eclipse to allow Fiddler to intercept requests
提问by nevets1219
In Eclipse there are two places that I've attempted to configure so that Fiddler can intercept the HTTP/HTTPS requests that I'm sending out:
在 Eclipse 中,我尝试配置了两个地方,以便 Fiddler 可以拦截我发送的 HTTP/HTTPS 请求:
Windows > Preference > General > Network Connections
- I've tried Native/Direct/Manual- In VM arguments, I add the following
-DproxySet=true -DproxyHost=127.0.0.1 -DproxyPort=8888
Windows > Preference > General > Network Connections
- 我试过本机/直接/手动- 在 VM 参数中,我添加以下内容
-DproxySet=true -DproxyHost=127.0.0.1 -DproxyPort=8888
EDIT: I've tried the new properties suggested by rgerganov as well.
编辑:我也尝试过 rgerganov 建议的新属性。
I haven't touched any "network" related settings in Fiddler and I've set it to monitor all processes.
我没有在 Fiddler 中触及任何与“网络”相关的设置,我已经将它设置为监视所有进程。
I tried with Wireshark and I was able to intercept the requests with no modifications to Eclipse but information presented in Wireshark is too in-depth and I don't need most of the details provided by Wireshark.
我尝试使用 Wireshark,我能够在不修改 Eclipse 的情况下拦截请求,但是 Wireshark 中提供的信息太深入了,我不需要 Wireshark 提供的大部分细节。
EDIT: Here's the sample code which I'm trying:
编辑:这是我正在尝试的示例代码:
public static void doPOST() {
String post_url = "https://lookup.mxtelecom.com/USLookup";
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion( params, HttpVersion.HTTP_1_1 );
HttpProtocolParams.setContentCharset( params, "UTF-8" );
HttpProtocolParams.setUseExpectContinue( params, true );
SchemeRegistry supportedSchemes = new SchemeRegistry();
supportedSchemes.register( new Scheme( "https", SSLSocketFactory.getSocketFactory(), 443 ) );
supportedSchemes.register( new Scheme( "http", PlainSocketFactory.getSocketFactory(), 80 ) );
ClientConnectionManager ccm = new ThreadSafeClientConnManager( params, supportedSchemes );
HttpClient m_Client = new DefaultHttpClient( ccm, params );
HttpPost httpPost = new HttpPost( post_url );
List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add( new BasicNameValuePair( "something", "useful" ) );
ResponseHandler<String> responseHandler = new ResponseHandler<String>() {
@Override
public String handleResponse( HttpResponse response ) throws ClientProtocolException, IOException {
if ( response.getEntity() != null ) {
return EntityUtils.toString( response.getEntity() );
}
return null;
}
};
try {
UrlEncodedFormEntity entity = new UrlEncodedFormEntity( postParameters, "UTF-8" );
httpPost.setEntity( entity );
results = m_Client.execute( httpPost, responseHandler );
} catch ( ClientProtocolException e ) {
e.printStackTrace();
} catch ( IOException e ) {
e.printStackTrace();
}
}
Eclipse Build id: 20100218-1602 // 3.5.2.R35x
Fiddler2 v2.3.2.6
jdk1.6.0_21
Eclipse 构建 ID:20100218-1602 // 3.5.2.R35x
Fiddler2 v2.3.2.6
jdk1.6.0_21
Please let me know if you need any other information.
如果您需要任何其他信息,请告诉我。
回答by nevets1219
HttpClient
needs to be made aware of the proxy information. Several approaches can be used:
HttpClient
需要知道代理信息。可以使用几种方法:
See 2.7 in HTTP Component's documentation:
请参阅 HTTP 组件文档中的2.7 :
"Connect to the target host via a proxy is by setting the default proxy parameter"
DefaultHttpClient httpclient = new DefaultHttpClient(); HttpHost proxy = new HttpHost("127.0.0.1", 8888); httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
Using "the standard JRE proxy selector to obtain proxy information"
DefaultHttpClient httpclient = new DefaultHttpClient(); ProxySelectorRoutePlanner routePlanner = new ProxySelectorRoutePlanner( httpclient.getConnectionManager().getSchemeRegistry(), ProxySelector.getDefault()); httpclient.setRoutePlanner(routePlanner);
and then adding the following as VM arguments:
-Dhttp.proxyHost=127.0.0.1 -Dhttp.proxyPort=8888 -Dhttps.proxyHost=127.0.0.1 -Dhttps.proxyPort=8888
Custom
RoutePlanner
implementation (I did not explore this option)
“通过代理连接到目标主机是通过设置默认代理参数”
DefaultHttpClient httpclient = new DefaultHttpClient(); HttpHost proxy = new HttpHost("127.0.0.1", 8888); httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
使用“标准JRE代理选择器获取代理信息”
DefaultHttpClient httpclient = new DefaultHttpClient(); ProxySelectorRoutePlanner routePlanner = new ProxySelectorRoutePlanner( httpclient.getConnectionManager().getSchemeRegistry(), ProxySelector.getDefault()); httpclient.setRoutePlanner(routePlanner);
然后将以下内容添加为 VM 参数:
-Dhttp.proxyHost=127.0.0.1 -Dhttp.proxyPort=8888 -Dhttps.proxyHost=127.0.0.1 -Dhttps.proxyPort=8888
自定义
RoutePlanner
实现(我没有探索这个选项)
回答by jdramaix
You have many possibility to configure proxy in java :
您有很多可能在 java 中配置代理:
- Using system properties
- via VM arguments (assuming you want to enable proxy for http and https traffic) :
- 使用系统属性
- 通过 VM 参数(假设您要为 http 和 https 流量启用代理):
java -Dhttp.proxyHost=127.0.0.1 -Dhttp.proxyPort=8888 -Dhttps.proxyHost=127.0.0.1 -Dhttps.proxyPort=8888
java -Dhttp.proxyHost=127.0.0.1 -Dhttp.proxyPort=8888 -Dhttps.proxyHost=127.0.0.1 -Dhttps.proxyPort=8888
via System properties :
// Get system properties Properties sysProperties = System.getProperties(); // Specify proxy settings sysProperties.put("https.proxyHost", "127.0.0.1"); sysProperties.put("https.proxyPort", "8888"); sysProperties.put("http.proxyHost", "127.0.0.1"); sysProperties.put("http.proxyPort", "8889");
通过系统属性:
// Get system properties Properties sysProperties = System.getProperties(); // Specify proxy settings sysProperties.put("https.proxyHost", "127.0.0.1"); sysProperties.put("https.proxyPort", "8888"); sysProperties.put("http.proxyHost", "127.0.0.1"); sysProperties.put("http.proxyPort", "8889");
The "disavdantage" of this method : once the proxy is set for a protocol, all connection for that protocol will use the proxy
这种方法的“缺点”:一旦为协议设置了代理,该协议的所有连接都将使用代理
java.net.Proxy : available since java 1.5. Allow you to specify a proxy configuration to use for opening a connection
SocketAddress addr = new InetSocketAddress("127.0.0.1", 8888); Proxy proxy = new Proxy(Proxy.Type.HTTP, addr); URL url = new URL("http://stackoverflow.com/"); InputStream in = url.openConnection(proxy).getInputStream();
ProxySelector class : available since Java 1.5. allow you to select a proxy by URI ! It's an abstract class, a simple implementation returning your proxy for all http and https connection looks like :
public static class MyProxySelector extends ProxySelector { private List<Proxy> proxy; private List<Proxy> noProxy; MyProxySelector() { proxy = new ArrayList<Proxy>(); SocketAddress addr = new InetSocketAddress("127.0.0.1", 8888); proxy.add(new Proxy(Proxy.Type.HTTP, addr)); noProxy = new ArrayList<Proxy>(); noProxy.add(Proxy.NO_PROXY); } public List<Proxy> select(URI uri) { System.err.println("Connection to the proxy for uri :"+uri); if (uri == null) { throw new IllegalArgumentException("URI can't be null."); } String protocol = uri.getScheme(); if ("http".equalsIgnoreCase(protocol) || "https".equalsIgnoreCase(protocol)) { //if http or https connection, return our proxy config return proxy; } else { // Otherwise don't use a proxy return noProxy; } } @Override public void connectFailed(URI uri, SocketAddress sa, IOException ioe) { System.err.println("Connection to the proxy failed !"); //throw exception or do some stuff... } }
java.net.Proxy :从 java 1.5 开始可用。允许您指定用于打开连接的代理配置
SocketAddress addr = new InetSocketAddress("127.0.0.1", 8888); Proxy proxy = new Proxy(Proxy.Type.HTTP, addr); URL url = new URL("http://stackoverflow.com/"); InputStream in = url.openConnection(proxy).getInputStream();
ProxySelector 类:自 Java 1.5 起可用。允许您通过 URI 选择代理!它是一个抽象类,一个简单的实现,返回所有 http 和 https 连接的代理,如下所示:
public static class MyProxySelector extends ProxySelector { private List<Proxy> proxy; private List<Proxy> noProxy; MyProxySelector() { proxy = new ArrayList<Proxy>(); SocketAddress addr = new InetSocketAddress("127.0.0.1", 8888); proxy.add(new Proxy(Proxy.Type.HTTP, addr)); noProxy = new ArrayList<Proxy>(); noProxy.add(Proxy.NO_PROXY); } public List<Proxy> select(URI uri) { System.err.println("Connection to the proxy for uri :"+uri); if (uri == null) { throw new IllegalArgumentException("URI can't be null."); } String protocol = uri.getScheme(); if ("http".equalsIgnoreCase(protocol) || "https".equalsIgnoreCase(protocol)) { //if http or https connection, return our proxy config return proxy; } else { // Otherwise don't use a proxy return noProxy; } } @Override public void connectFailed(URI uri, SocketAddress sa, IOException ioe) { System.err.println("Connection to the proxy failed !"); //throw exception or do some stuff... } }
And set your proxy selector as default proxy selector
并将您的代理选择器设置为默认代理选择器
public static void main(String[] args){
ProxySelector.setDefault(new MyProxySelector());
//...
}
I prefer this last mechanism because you can put some logging and see wath Java is doing !
我更喜欢最后一种机制,因为您可以进行一些日志记录并查看 Java 正在做什么!
Important remark :
重要说明:
By default, Fiddler captures only traffic coming from web-browsers !! Change that to capture traffic of all processes :
(source: comexis.eu)
默认情况下,Fiddler 只捕获来自网络浏览器的流量!!更改它以捕获所有进程的流量:(来源:comexis.eu)
回答by rgerganov
The second method should work, however the properties are http.proxyHost
and http.proxyPort
.
第二种方法应该可以工作,但是属性是http.proxyHost
和http.proxyPort
。
回答by Steven Mai
Also, in Fiddler if you need to capture All Processes from a HTTPS connection, then you need to go to the Tools menu - Fiddler Options - HTTPS - check the 'Decrypt HTTPS traffic' and select '...from all processes'.
此外,在 Fiddler 中,如果您需要从 HTTPS 连接捕获所有进程,那么您需要转到工具菜单 - Fiddler 选项 - HTTPS - 检查“解密 HTTPS 流量”并选择“...来自所有进程”。
Selecting the 'All Processes' from Fiddler's status bar apparently is just for HTTP connections.
从 Fiddler 的状态栏中选择“所有进程”显然仅适用于 HTTP 连接。
回答by lwpro2
JVM parameter of proxy host and proxy port can be used. At the same time, Fiddler certificate needs to be installed to sniffer https connections.
可以使用代理主机和代理端口的JVM参数。同时需要安装 Fiddler 证书来嗅探 https 连接。