0

I am trying to do the following (in Java):

  • connect to some proxy server & http_url some

But I am having some errors like : java.net.ConnectException: Connection timed out: connect...

Or errors related to HTTP response code : 302, 400, FileNotFound, file server error, etc.

In some changes I did, I even got 200 code. (when I only use openConnection() =>( without the proxy IP address). That is my best run trace.

I have had all class of : (Unknown Source) in the error msg, from IDE Eclipse Luna console.

Some of the error come in the form / or from : .getInputStream() method, I don't know if there is about setDoInput(), setDoOutput, the Encoding, or whatever:

Can some body help me?

Here is my code:

url = new URL(http_url);
HttpURLConnection conn;
    try {
        conn = (HttpURLConnection)url.openConnection(proxy);
        conn.setRequestMethod("GET");
        conn.setRequestProperty("User-Agent", USERAGENT);

        conn.setUseCaches(false);
        conn.setRequestProperty("Accept", "*/*");
        conn.addRequestProperty("Referer", "http://www.google.com/");
        conn.setRequestProperty("Accept-Encoding", "gzip,deflate,sdch");
        conn.setRequestProperty("Accept-Language", "en-US,en;q=0.8");
        conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
        conn.setRequestProperty("X-Requested-With", "XMLHttpRequest");
        conn.setDoInput(true);
        System.out.println("response msg  " + conn.getResponseMessage() + " CODE");

        System.out.println("errorStream msg " + conn.getErrorStream());
        System.out.println("inputStream msg " + conn.getInputStream());
        String header_date = conn.getHeaderField("Date");
        System.out.println(" date es: " + header_date);
        String line = null;
        StringBuffer tmp = new StringBuffer();
        System.out.println("the code is :" + conn.getResponseCode());

        if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));

            while ((line = in.readLine()) != null) {
                tmp.append(line);
            }
            System.out.println("value line is:  " + line +"& date is: " + header_date);

            Scrape(String.valueOf(tmp)); // temp.toString()

            in.close();
            in = null;
            url = null;
            conn.disconnect();
            conn = null;
        } else {   
            System.out.println("something bad happened code <>200, debug from your server"); 
        }        
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();    
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (UnsupportedEncodingException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
Jens
  • 6,243
  • 1
  • 49
  • 79
  • 1
    You should definitively edit your questions and strip it down so it becomes easier to read. Also strip down your code to show exactly ONE problem that you have. I tried to understand your problem but failed to do so... – Jens Apr 15 '15 at 16:27
  • Thanks Jens I know the text is dense but the problem is openConnection(proxy) if you dont use proxy the code works fine, if you use: System.setProperty("http.proxyHost", Host); System.setProperty("http.proxyPort", Port); also it works AND WHATS THE PROBLEM????? is it an Abstract class that needs an extend/overrides????? – Ernesto Ibáñez Apr 24 '15 at 15:33
  • I have had read StackOverflow questions about openConnection(proxy) and have seen there is many people stacked in this same problem. I dont have notice about a main bug in Java about this proxy issue with openConnection() method. – Ernesto Ibáñez Apr 24 '15 at 15:52
  • See this link for the same openConnection (proxy) problem: http://www.postseek.com/meta/df0bd802cfb7788ee9c17834075ac94b Thanks for your help. – Ernesto Ibáñez Apr 24 '15 at 16:01
  • Well as long as you just confirm that your text is dense but don't change anything it will not get better. Your question is incomprehensible. Are you behind a companies proxy that you need a connection through? If so how can you ever get a return code of `200`? Really your question is a "mess". I don't really understand what your situation is and what you are asking for. – Jens Apr 24 '15 at 16:56

1 Answers1

0

To solve your Proxy problem you can try using Proxy as below

Proxy proxy= new Proxy(Proxy.Type.HTTP, new InetSocketAddress(<Proxy IP Address as String>, <Proxy Port Number as Integer>));
        HttpURLConnection http_conn=(HttpURLConnection)request_url.openConnection(proxy);
balaaagi
  • 502
  • 11
  • 21
  • Ups!!!!! sorry Mr. Srinivasan THAT DOES NOT WORKED!!!!! I am using some code like: SocketAddress addr = new InetSocketAddress(proxyHost, proxyPort); Proxy httpProxy = new Proxy(Proxy.Type.HTTP, addr); with a string for the host and an integer for the port and there is no problem with that. WHY IF I USE : System.setProperty("http.proxyHost", Host); System.setProperty("http.proxyPort", Port); – Ernesto Ibáñez Apr 24 '15 at 16:08
  • The Link to the Proxy class is from the `reflection` package. Imho, that has nothing to do with being behind a HTTP Proxy Server... – Jens Apr 24 '15 at 16:44
  • java.net.SocketException: Unexpected end of file from server at sun.net.www.http.HttpClient.parseHTTPHeader(Unknown Source) at sun.net.www.http.HttpClient.parseHTTP(Unknown Source) at sun.net.www.http.HttpClient.parseHTTPHeader(Unknown Source) at sun.net.www.http.HttpClient.parseHTTP(Unknown Source) atsun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source) at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source) at java.net.HttpURLConnection.getResponseCode(Unknown Source) at com.blogger.HTTPScrap.main(HTTPScrap.java:109) THIS IS MY RUN now 0925 hours – Ernesto Ibáñez Apr 28 '15 at 13:55
  • What is the reason why (HttpURLConnection)url.openConnection(proxy); DOES NOT WORK &&&& (HttpURLConnection)url.openConnection(); DO The WORK???? WHY? Whats the diference? – Ernesto Ibáñez Apr 28 '15 at 14:08