2

I am trying to fetch and download file in html format. Following is the code block:

import java.net.*;
import java.io.*;

public class URLReader {

    public static void main(String[] args) throws Exception {

    System.setProperty("http.proxyHost", "webcache.mydomain.com");
    System.setProperty("http.proxyPort", "8080");

    URL url = new URL("http://www.java2s.com/");
    BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));

    String inputLine;
    while ((inputLine = in.readLine()) != null)
        System.out.println(inputLine);
        in.close();
    }
}

Here, when i am trying to Run this java file it show me following error:

D:\Build>javac URLReader.java

D:\Build>java URLReader

Exception in thread "main" java.net.ConnectException: Connection timed out: connect
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:351)
    at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:213)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:200)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
    at java.net.Socket.connect(Socket.java:529)
    at java.net.Socket.connect(Socket.java:478)
    at sun.net.NetworkClient.doConnect(NetworkClient.java:163)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:388)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:523)
    at sun.net.www.http.HttpClient.<init>(HttpClient.java:227)
    at sun.net.www.http.HttpClient.New(HttpClient.java:300)
    at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:977)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:925)
    at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:836)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1172)
    at java.net.URL.openStream(URL.java:1010)
    at URLReader.main(URLReader.java:12)

I don't think there any problem with code because When i tried to run this code at Home it worked for me. But when i tried it in my office it showed me the error! So i guess there must be some kind of blockage in my office.

Can i set any proxy here in code so that it might work?

5
  • Off topic: Why use fully-qualified class names when you have the packages imported? Commented May 28, 2013 at 5:28
  • I am new to Java! I am not getting what you are saying. Can you Please elaborate Commented May 28, 2013 at 5:33
  • Refer to the answer from @Cratytus below. But mind you, even after you've checked for the existence of a proxy, that you have two logic errors in your code that will prevent its successful execution. Commented May 28, 2013 at 5:35
  • I have Updated my question. and still after adding proxy its not working. Is there any logical error! Commented May 28, 2013 at 5:49
  • Your proxy may not be correct. Try putting the proxy explicitly in IE proxy configuration and see if u get the page. Commented May 28, 2013 at 6:33

2 Answers 2

4

If you are behind a proxy you need to configure Java to use it. Read here on how to set your proxy for your network program

Sign up to request clarification or add additional context in comments.

Comments

1

Try this as mentioned in this link http://docs.oracle.com/javase/6/docs/technotes/guides/net/proxies.html

//Set the http proxy to webcache.mydomain.com:8080

System.setProperty("http.proxyHost", "webcache.mydomain.com");
System.setPropery("http.proxyPort", "8080");

// Next connection will be through proxy.
URL url = new URL("http://java.sun.com/");
InputStream in = url.openStream();

// Now, let's 'unset' the proxy.
System.setProperty("http.proxyHost", null);

// From now on http connections will be done directly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.