- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试测试承受多台计算机负载的 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 会忽略代理。真正奇怪的是,稍后当我尝试通过代理与多个客户端连接时,基线连接如何工作并不重要 - 如果基线连接不使用代理,多个客户端连接仍然使用代理。
我在这里缺少什么?
谢谢
最佳答案
我设法修复了它 - 无论出于何种原因,后续调用 url.openconnection() 之间的时间都会有所不同。在每次 start() 之间调用 Thread.sleep(10000) 有效。
关于java - URLConnection(Proxy proxy) 忽略设置的代理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6442567/
在我的程序中,我需要跟踪已打开的与某些 HTTP 服务器的连接列表 - 以便在需要时立即断开连接。 我遇到了以下问题。如果我连接到 HTTP 服务器,一切正常,但如果连接到 HTTPS,则连接不会从列
我正在尝试写信给 URLConnection#getOutputStream ,但是,在我调用 URLConnection#getInputStream 之前,实际上并没有发送任何数据。 .即使我设置
我有一组对象,我试图通过 POST 发送到 API。第一个对象将按原样通过,然后我得到: java.net.ProtocolException: cannot write request body a
我在尝试发送文本时遇到 URLConnection 编码问题。 我的代码是这样的: final URL url = new URL(urlString); final URLConnection ur
我有这种方法,可以从雅虎财经下载 .csv 文件并将其保存在本地。它是在循环期间访问的,因此它从列表中下载许多文件。然而,有时符号输入不正确、不再存在或连接超时。如何修改此方法,以便重试连接超时并跳过
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import
打开 URLConnection 时,我使用以下代码来获取内容长度,但它返回 -1。 URL url = new URL(sUrl[0]); URLConnection connection = ur
默认情况下,URLConnection 的超时为 0,这是无限制的。 XXXXX 的合理值是多少? URL url = ... URLConnection uCon = url.openConnect
我无法打开具有特定网络资源的 URLConnection。我得到了 “java.net.ConnectException:连接超时:”。是因为该域阻止了直接 URL 连接吗?如果是这样,他们是如何阻止
我看过文本文件示例,但是否以与 URLConnection 相同的方式将音频文件直接保存到服务器? 最佳答案 是的,一样的。尽管确保使用二进制输出流将内容写入磁盘。 类似于: URLConne
我一直在尝试从网页获取信息,特别是此网站:http://www.ncbi.nlm.nih.gov/pubmed?term=%22pulmonary%20disease%2C%20chronic%20o
在我使用 Apache 库 (org.apach.httpclient) 向带有参数 (BasicNameValuePair) 的 Php 脚本发出请求之前, 然后现在我想删除那些库以减小 APK 大
我正在使用 android 应用程序,我正在从 url 下载文件。一切正常,但是当互联网连接介于两者之间(打开连接后)时,下载超时永远不会发生并且连接永远不会结束。 给我一个解决这个问题的方案
我只是想了解一下 JacksonJson 库。为此,我尝试将 Places API 中的 JSON 数据转换为字符串。 我的 key 有效(我在浏览器和另一个应用程序中进行了测试),但出现错误。这是代
我目前正在 Eclipse 上使用 Java 7、Maven、Spring MVC 和 Eclipselink JPA 编写 Web 服务,以访问连接到内部网络的温度/湿度传感器的值。我使用 curl
HttpsURLConnection 有问题 - 未使用代理。 这是代码: //proxy String type = "https"; System.getProperties().put(type
我目前正在 Eclipse 上使用 Java 7、Maven、Spring MVC 和 Eclipselink JPA 编写一个 Web 服务,以访问连接到内部网络的温度/湿度传感器的值。我使用cur
在我的应用程序中,我需要下载一些网页。我是这样做的 URL url = new URL(myUrl); HttpURLConnection conn = (HttpURLConnection) url
我正在尝试使用 URLConnection 获得最低级别的字节计数.我已经用 CountingInputStream 计算了两个流传递的数据和 CountingOutputStream来自 Apach
我正在使用这段代码创建一个常规的 HTTP 连接: URLConnection cn = new URL( "http://...." ).openConnection(); cn.connect()
我是一名优秀的程序员,十分优秀!