Click here to Skip to main content
15,889,730 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
Posted

1 solution

wrote:
new URI("http://google.com"));


Try http://www.google.com.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900