lundi 20 avril 2015

Java .getInputStream() openConnection() HTTP response code ERRORS

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();
    }

Aucun commentaire:

Enregistrer un commentaire