Monday 15 April 2013

HTTP GET request with URLConnection is unable to access any page -


I am working on Ubuntu 12.04. This is my simple code to implement the HTTP GET method using URLConnection.

  Import java.io.buffferedReader; Import java.io.dataOutputStream; Import java.io.InputStreamReader; Import java.net.HttpURLConnection; Import java.net.URL; Import javax.net.ssl.HttpsURLConnection; Public class HttpURLConnectionExample {Personal ending string USER_AGENT = "Mozilla / 5.0"; Public static zero master throws exception [] args {HttpURLConnectionExample http = new HttpURLConnectionExample (); System.out.println ("1 test - send HTTP request"); Http.sendGet (); } // Get HTTP request private zero sendGet () throws exception {string} url = "https://www.google.com/search?q=flower"; Url obj = new url (url); HttpURLConnection con = (HttpURLConnection) obj.openConnection (); // Optional default is GET con.setRequestMethod ("GET"); // Requesting Header con.setRequestProperty ("user-agent", USER_AGENT); Int responsivecode = con.getResponseCode (); System.out.println ("send \" request to url: "+ url); System.out.println ("response code:" + responsecode); BufferedReader = new in BufferedReader (new InputStreamReader (con.getInputStream ())); String inputline; String buffer response = new stringbuffer (); While ((inputLine = in.readline ()) = null) {response.append (inputLine); } In.close (); // Print result System.out.println (response.toString ()); }}   

But when I compile this code and run this code from the Ubuntu terminal, the output of this code does not return the contents of the page specified by the URL. Http://www.google.com/search?q=flower Answer Code :: - Rather it gives low output

  Send a request for 'received' request for URL 1 Send HTTP GET request 307 & lt ;! DOCTYPE HTML Public "- // IETF // DTD HTML 2.0 / N" & gt; & Lt; Html & gt; & Lt; Top & gt; & Lt; Title & gt; 307 temporary redirection & lt; / Title & gt; & Lt; / Head & gt; & Lt; Body & gt; & Lt; H1 & gt; Temporary redirection & lt; / H1> & Lt; P & gt; The document has been moved & lt; A href = "https://ifwb.iitb.ac.in/index.php?add=www.google.com/search" & gt; Here & lt; / A & gt; & Lt; / P & gt; & Lt; Hour & gt; & Lt; Address & gt; Apache / 2.2.22 (Fedora) on the server www.google.com Port 80 & lt; / Address & gt; & Lt; / Body & gt; & Lt; / Html & gt;   

This problem holds for any URL specified in the code. In addition, I tried to access web content using Telnet client like

Telnet www.google.com 80 /

and it Returns similar results not only for Www.google.com, but for each URL. I am a student at IIT Bombay and maybe there is something to do with it. I want to stay at JavaWiki and do not want an APHAPXclient. So help me out of this.

"itemprop =" text ">

It seems you are rejected by the incomplete request due to the server.

Below is a fraction of the Wirershark dump, how does IE send 10 GET requests for the interested url as you can see, there are many fields that describe the capabilities and your customer's Expects expectations, so the query server can return the answer in the most appropriate and consumable form. Consult Google / RFC to see the meaning of each parameter:

Received / Search? Q = Flowers HTTP / 1.1

Accept: text / html, application / xHTML + xml, /

acceptance-language: N-US

User-agent: Mozilla / 5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident / 6.0)

Accept-encoding: gzip, deflate

Host: www.google.com

DNT: 1

Connection: Elive

Cookies: [Some personal information here]

No comments:

Post a Comment