URLConnection(Proxy proxy) 忽略设置的代理
我正在尝试测试一个承受多台机器负载的 SOCKS 代理。我的代码大纲类似于使用
- 一个客户端直接连接到服务器,下载测试文件,并记录所花费的时间。
- 通过代理与一台客户端连接,下载测试文件,并记录所花费的时间。
- 通过代理连接多个客户端,下载测试文件,记录时间。
1和2以相同的功能执行。
private static void baseline() {
Download withProxy = new Download(socksPort, targetFile);
Download withoutProxy = new Download(true, socksPort, targetFile); //The true argument just indicates not to use the proxy.
try { //Come to think of it, I could just call run() directly here since this part is meant to be done serially.
withProxy.start();
withProxy.join();
withoutProxy.start();
withoutProxy.join();
//Some code for getting the times goes here.
} catch (Exception e) {
System.out.println("Couldn't get baseline.");
e.printStackTrace();
}
}
下载对象继承自Thread。大部分工作是在 run() 方法中完成的,如下所示:
public void run() {
try {
URL url = new URL("http://" + targetFile);
URLConnection urconn = null;
if (baseline) {
urconn = url.openConnection(Proxy.NO_PROXY);
} else {
Proxy proxy = new Proxy(Proxy.Type.SOCKS, proxyAddr);
urconn = url.openConnection(proxy);
}
InputStreamReader isr = new InputStreamReader(urconn.getInputStream());
System.out.println("Thread " + id + " is downloading.");
long startTime = System.currentTimeMillis();
char[] buf = new char[64];
while (isr.read(buf) != -1) {
;
}
long endTime = System.currentTimeMillis();
isr.close();
System.out.println("Thread " + id + " has completed.");
delta = (endTime - startTime);
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
问题是,当我调用基线函数时,它总是使用第一个选择的代理 - 如果我先运行 withproxy 线程,则 withoutproxy 线程将使用代理。如果我先运行 withoutproxy,withproxy 会忽略代理。真正奇怪的是,稍后当我尝试通过代理与多个客户端连接时,基线连接如何工作并不重要 - 如果基线连接不使用代理,多个客户端连接仍然使用代理。
我在这里缺少什么?
谢谢
I'm trying to test a SOCKS proxy that's under load from multiple machines. The outline of my code is something along the lines of
- Connect directly to the server with one client, download the test file, and record the time it took.
- Connect through the proxy with one client, download the test file, and record the time it took.
- Connect through the proxy with multiple clients, download the test file, record the time.
1 and 2 are performed in the same function.
private static void baseline() {
Download withProxy = new Download(socksPort, targetFile);
Download withoutProxy = new Download(true, socksPort, targetFile); //The true argument just indicates not to use the proxy.
try { //Come to think of it, I could just call run() directly here since this part is meant to be done serially.
withProxy.start();
withProxy.join();
withoutProxy.start();
withoutProxy.join();
//Some code for getting the times goes here.
} catch (Exception e) {
System.out.println("Couldn't get baseline.");
e.printStackTrace();
}
}
The download object inherits from Thread. Most of the work is done in the run() method, which looks like this:
public void run() {
try {
URL url = new URL("http://" + targetFile);
URLConnection urconn = null;
if (baseline) {
urconn = url.openConnection(Proxy.NO_PROXY);
} else {
Proxy proxy = new Proxy(Proxy.Type.SOCKS, proxyAddr);
urconn = url.openConnection(proxy);
}
InputStreamReader isr = new InputStreamReader(urconn.getInputStream());
System.out.println("Thread " + id + " is downloading.");
long startTime = System.currentTimeMillis();
char[] buf = new char[64];
while (isr.read(buf) != -1) {
;
}
long endTime = System.currentTimeMillis();
isr.close();
System.out.println("Thread " + id + " has completed.");
delta = (endTime - startTime);
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
The problem is that when I call the baseline function, it always uses the first choice of proxy - if I run the withproxy thread first, the withoutproxy thread will use the proxy. If I run withoutproxy first, withproxy ignores the proxy. The really odd thing is that later on when I try to connect through the proxy with multiple clients, it doesn't matter how the baseline connections worked - if the baseline connection didn't use a proxy, the multiple client connections still do.
What am I missing here?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我设法修复了它 - 无论出于何种原因,后续调用 url.openconnection() 之间的时间都会有所不同。在每个 start() 之间调用 Thread.sleep(10000) 是有效的。
I managed to fix it - For whatever reason, the time between subsequent calls to url.openconnection() makes a difference. Calling Thread.sleep(10000) between each start() works.