在Java,URL和System.setProperty中设置代理?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了在Java,URL和System.setProperty中设置代理?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1480字,纯文字阅读大概需要3分钟。
内容图文
我制作了一个Java应用程序,可以从互联网获取响应时间.我正在从代理后面进行工作,现在遇到了无法解决的问题,这是我的代码的一部分
URL website = new URL("http", proxy, Integer.parseInt(proxyPort), websiteUrl)
BufferedReader in = new BufferedReader(new InputStreamReader(website.openStream()));
long start = System.currentTimeMillis();
while ((in.readLine()) != null){}
long elapsedTimeMillis = System.currentTimeMillis()-start;
VS
URL website = new URL(websiteUrl);
System.setProperty("http.proxyHost", proxy);
System.setProperty("http.proxyPort", proxyPort);
BufferedReader in = new BufferedReader(new InputStreamReader(website.openStream()));
long start = System.currentTimeMillis();
while ((in.readLine()) != null){}
long elapsedTimeMillis = System.currentTimeMillis()-start;
当我使用
URL website = new URL(websiteUrl);
System.setProperty("http.proxyHost", proxy);
System.setProperty("http.proxyPort", proxyPort);
我平均得到0.064的响应时间
当我使用
URL website = new URL("http", proxy, Integer.parseInt(proxyPort), websiteUrl)
我得到的响应时间更高,为0.219.我怎么知道哪一个给我准确的时间?
解决方法:
在第一种情况下,检查用于构造实例化URL的构造器的Javadoc,它没有执行您想要的操作:
URL(String protocol, String host, int port, String file) Creates a URL object from the specified protocol, host, port number, and file.
在第二种情况下,您通过代理调用网站,在第一种情况中,您将代理称为网络服务器,其中包括
http://proxyhost:3128/http://mysite.com/index.html
…响应不是您所期望的,并且您留在LAN中,因此时代截然不同.
内容总结
以上是互联网集市为您收集整理的在Java,URL和System.setProperty中设置代理?全部内容,希望文章能够帮你解决在Java,URL和System.setProperty中设置代理?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。