为什么在运行Java应用程序时proxyHost/proxyPort不起作用? [英] Why don't proxyHost/proxyPort work when running my Java application?

查看:852
本文介绍了为什么在运行Java应用程序时proxyHost/proxyPort不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个与某些REST服务通信的Java应用程序,我想使用Fiddler查看HTTP流量.

I have a java app that talks to some REST services, and I want to look at the HTTP traffic using Fiddler.

Fiddler充当localhost:8888上的代理,因此应该使用以下Java VM选项将java配置为使用此代理:

Fiddler acts as a proxy on localhost:8888, so the following Java VM options are supposed to configure java to use this proxy:

-Dhttp.proxyHost=localhost -Dhttp.proxyPort=8888

但是,如果我在运行要调试的Java应用程序时传递这些参数,则在Fiddler中看不到任何流量.

However, if I pass these parameters when running the java app that I want to debug, I see no traffic in Fiddler.

我编写了一个测试Java应用程序,该应用程序仅使用HttpURLConnection执行HTTP GET.

I wrote a test Java app that simply performs an HTTP GET using HttpURLConnection.

如果我从Eclipse调试时指定了上述命令行参数,则可以在fiddler中查看来自此应用程序的HTTP流量.

I can view the HTTP traffic from this app in fiddler, if I specify the above-mentioned command-line parameters when debugging it from Eclipse.

http.proxyHost/Port可能不适用于所有Java HTTP操作的原因是什么?

What are the reasons that http.proxyHost/Port might not work for all java HTTP operations?

推荐答案

您可以使用以下代码(HttpClient 4.x)告诉HttpClient遵守JDK系统参数.

You can tell HttpClient to honor the JDK system arguments using the below code (HttpClient 4.x).

public static final DefaultHttpClient HTTP = new DefaultHttpClient();
ProxySelectorRoutePlanner routePlanner = new ProxySelectorRoutePlanner(HTTP.getConnectionManager().getSchemeRegistry(),
ProxySelector.getDefault());
HTTP.setRoutePlanner(routePlanner);

这篇关于为什么在运行Java应用程序时proxyHost/proxyPort不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆