英文:
How to check if Java is using configured proxy server?
问题
我在运行命令中设置了-Dhttps.proxyHost=
,有没有一些日志记录我可以启用,以便在进行代理调用时可以看到?我需要验证这是否在起作用。
我还通过-Dhttp.nonProxyHosts=
设置了一些主机白名单,因此我想确保它不会对这些主机进行代理。
英文:
I have the -Dhttps.proxyHost=
set in the run command, is there some kind of logging I can enable to see when it is proxying a call? I need to verify that this is working.
I have also whitelisted a number of hosts via -Dhttp.nonProxyHosts=
so I want to make sure it is not proxying these.
答案1
得分: 2
1. 使用 java.util.logging
创建名为 logging.properties
的文件,内容如下:
handlers = java.util.logging.ConsoleHandler
java.util.logging.ConsoleHandler.level = ALL
sun.net.www.protocol.http.HttpURLConnection.level = FINEST
然后添加 -Djava.util.logging.config.file=/path/to/logging.properties
到 JVM 选项。
现在,当通过代理建立 HTTPS 连接时,您将会看到日志消息:
May 30, 2020 2:12:56 AM sun.net.www.protocol.http.HttpURLConnection plainConnect0
FINEST: ProxySelector 请求 https://example.com/
May 30, 2020 2:12:56 AM sun.net.www.protocol.https.HttpsClient New
FINEST: 正在查找 URL https://example.com 的 HttpClient,代理值为 HTTP @ 127.0.0.1:443
May 30, 2020 2:12:56 AM sun.net.www.protocol.https.HttpsClient <init>
FINEST: 创建新的 HttpsClient,url:https://example.com,代理:HTTP @ 127.0.0.1:443,连接超时:-1
May 30, 2020 2:12:56 AM sun.net.www.protocol.http.HttpURLConnection plainConnect0
FINEST: 使用的代理:HTTP @ 127.0.0.1:443
2. 另一种选项,假设您在 Linux 上,在跟踪所有 connect
系统调用时:
$ sudo strace -f -p <PID> -e connect
在使用 strace
附加到 Java 进程的 <PID>
后,您将看到进程连接的所有地址。如果使用了代理,将会显示代理的地址。
[pid 12345] connect(263, {sa_family=AF_INET, sin_port=htons(443), sin_addr=inet_addr("127.0.0.1")}, 16) = -1 EINPROGRESS (操作现在正在进行中)
英文:
1. Using java.util.logging
Create logging.properties
file with the following contents:
handlers = java.util.logging.ConsoleHandler
java.util.logging.ConsoleHandler.level = ALL
sun.net.www.protocol.http.HttpURLConnection.level = FINEST
Then add -Djava.util.logging.config.file=/path/to/logging.properties
JVM option.
Now you'll see log messages whenever HTTPS connection is established through a proxy:
May 30, 2020 2:12:56 AM sun.net.www.protocol.http.HttpURLConnection plainConnect0
FINEST: ProxySelector Request for https://example.com/
May 30, 2020 2:12:56 AM sun.net.www.protocol.https.HttpsClient New
FINEST: Looking for HttpClient for URL https://example.com and proxy value of HTTP @ 127.0.0.1:443
May 30, 2020 2:12:56 AM sun.net.www.protocol.https.HttpsClient <init>
FINEST: Creating new HttpsClient with url:https://example.com and proxy:HTTP @ 127.0.0.1:443 with connect timeout:-1
May 30, 2020 2:12:56 AM sun.net.www.protocol.http.HttpURLConnection plainConnect0
FINEST: Proxy used: HTTP @ 127.0.0.1:443
2. Another option, assuming you are on Linux, is to trace all connect
system calls:
$ sudo strace -f -p <PID> -e connect
After attaching to a Java process <PID>
with strace
, you'll see all addresses where the process connects to. If a proxy is used, there will be an address of the proxy.
[pid 12345] connect(263, {sa_family=AF_INET, sin_port=htons(443), sin_addr=inet_addr("127.0.0.1")}, 16) = -1 EINPROGRESS (Operation now in progress)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论