2

I am not able to open a URLConnection with a particular web resource . I am getting

" java.net.ConnectException: Connection timed out:" . Is it because of that domain is blocking the direct URL connection ? If so how they are blocking this ? below is the code snippet i wrote .

Code snippet:

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

public class TestFileRead{

public static void main(String args[]){

    try{

        String serviceUrl = "http://xyz.com/examples.zip";
        HttpURLConnection serviceConnection = (HttpURLConnection) new URL(serviceUrl).openConnection();
        System.out.println(serviceConnection);
        serviceConnection.addRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)"); 

        DataInputStream din=new DataInputStream(serviceConnection.getInputStream());
        FileOutputStream fout=new FileOutputStream("downloaded");
        DataOutputStream dout=new DataOutputStream(fout);
        int bytes;
        while(din.available()>0){
            bytes=din.readByte();
            dout.write(bytes);
        }
    }catch(Exception ex){
        ex.printStackTrace();
    }

}

}

9
  • can you access the same URL from the same machine from your web browser ? there may be proxy issue. Commented Jan 19, 2011 at 13:19
  • yes I am able to access the same URL from web browser in my machine. Commented Jan 19, 2011 at 13:20
  • is there any proxy setting in your web browser set? Commented Jan 19, 2011 at 13:23
  • also there may be a firewall on your side denying the Java process. Commented Jan 19, 2011 at 13:39
  • Do you have this problem in a servlet only or also in a plain vanilla Java application? If the first, please mention make/version of servletcontainer. If the last, then the servlets tag was unnecessary. Commented Jan 19, 2011 at 13:40

2 Answers 2

1

You are probably using the proxy setup in your browser to access the Yahoo home page which explains why it works in your browser and not in your code. You require a proxy configuration for your Java application.

The simplest way would be to set the system property http.proxyHost and http.proxyPort when running the code (in Eclipse or when running from command line just add -Dhttp.proxyHost=your.host.com -Dhttp.proxyPort=80) and you should be good to go. Pick up the proxy settings from your browser configuration/settings.

EDIT: This link does a decent job of explaining the possible solutions when dealing with proxies in Java.

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

4 Comments

Its not working for me ,I also tried by setting the automatic proxy configuration script in the 'Java control panel' which is the same script being used in browser, but that also didnt work for me .
@selvam: There are many reasons why it would not work ranging from typos to anything else. Can you update your question with the code snippet and how you are executing the code (i.e. system parameters etc.)?
I have updated the question with the code snippet . Its just a plain java program. I am not using any system parameters. As I already said I have configured the proxy-auto-config(pac) java script in java control panel, the same script is being used in browser.
@selvam: I've never used pac to configure a JVM process proxy. Can you try the system parameter approach suggested in my post and let me know if that works? Testing it out would help us rule out the possibilities.
0

Try this, it works fine for me, returning the index page.

String serviceUrl = "http://yahoo.com";     
HttpURLConnection serviceConnection = (HttpURLConnection) new URL(serviceUrl).openConnection();
serviceConnection.addRequestProperty("User-Agent", "blah"); //some sites deny access to some pages when User-Agent is Java
BufferedReader in = new BufferedReader(new InputStreamReader(serviceConnection.getInputStream()));

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.