尝试检索网页时的连接时间 [英] Connection Time when trying to retrieve a webpage
问题描述
我面临一个奇怪的问题(可能很简单,但是我在:()上苦苦挣扎.请帮助我解决这个问题.
平台:WindowsXP,Java
问题陈述:
通过浏览器,我可以连接到网页.但是通过编程,它给出了连接超时错误(该代码在我的家用PC上工作正常,但在我的办公室PC上却无法正常工作).我也尝试了Windows的WGET来检索网页,但这也给了连接超时(即使winhttrack无法正常工作).我使用以下Java代码来查找代理设置.表示使用了直接连接.
Hi,
I''m facing a strange problem (may be simple, but i''m struggling on this :( ). Please help me to resolve this.
Platform: WindowsXP, Java
Problem statement:
Via browser i''m able to connect to a webpage. But through programming it''s giving connection timeout error (The code is working fine in my home PC but not in my office PC). Also i tried WGET for windows to retrieve a web page, but that too giving connection timeout (Even winhttrack is not working). I used the following java code to find out the proxy settings. It says DIRECT connection is used.
private static ProxyHost getProxyHost() {
ProxyHost proxyHost = null;
try {
System.setProperty("java.net.useSystemProxies", "true");
List<Proxy> l = ProxySelector.getDefault().select(
new URI("http://google.com"));
for (Iterator<Proxy> iter = l.iterator(); iter.hasNext();) {
Proxy proxy = (Proxy) iter.next();
InetSocketAddress addr = (InetSocketAddress) proxy.address();
if (addr != null) {
System.out.println("proxy: " + addr.getHostName() + ":"
+ addr.getPort());
proxyHost = new ProxyHost(addr.getHostName(), addr
.getPort());
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return proxyHost;
}
检索网页的代码:
The code to retrieve webpage:
HttpClient httpClient = new HttpClient();
try {
responseCode = httpClient.executeMethod(method);
if (responseCode != 200) {
return null;
}
br = new BufferedReader(new InputStreamReader(method
.getResponseBodyAsStream()));
String temp;
StringBuffer resp = new StringBuffer();
while ((temp = br.readLine()) != null) {
resp.append(temp);
}
return resp.toString().replaceAll("\\n", "");
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
method.releaseConnection();
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
预先感谢您的帮助.
Thanks in advance for your help.
推荐答案
新URI("http://google.com));
new URI("http://google.com"));
尝试http://www.google.com.
Try http://www.google.com.
这篇关于尝试检索网页时的连接时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!