Link to home
Start Free TrialLog in
Avatar of Naeemg
Naeemg

asked on

IOException while getting InputStream from URLConnection

hi all,
i'm getting IOException while getting InputStream from URLConnection,
but the document exists on that url location i've tested in Internet explorer and other applications. my code is below.
can anyone help me in this regards, that why this error happened in java, while using the same url in browser displays the contents of this file. also to solve this problem.
thanks

    java.net.URL thisURL = null;
    InputStream inStream = null;
    try
    {
      thisURL = new java.net.URL(http://groups.google.com.pk/group/Java--XML/feed/topics.xml);

      try
      {
        URLConnection connection = thisURL.openConnection();
        //connection.connect();
        //System.out.println(connection.getContentType());
        //System.out.println(connection.getContent().toString());
        inStream = connection.getInputStream();
      }
      catch (UnknownHostException e)
      {
      }
      catch (IOException e)
      {
        System.out.println("Error in opening URLConnection, Reading or Writing");
      }
Avatar of Mick Barry
Mick Barry
Flag of Australia image

What sort of IOException, and where is it occurring?

     System.out.println("Error in opening URLConnection, Reading or Writing");
     e.printStackTrace();
Avatar of Naeemg
Naeemg

ASKER

when it reaches at
inStream = connection.getInputStream();
then IOException occurrs.
Avatar of Naeemg

ASKER

java.io.IOException

      at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:591)

      at java.net.URLConnection.getContent(URLConnection.java:585)

Caused by: java.io.IOException: Server returned HTTP response code: 403 for URL: http://groups.google.com.pk/group/Java--XML/feed/msgs.xml

      at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:789)

      at sun.net.www.protocol.http.HttpURLConnection.getHeaderField(HttpURLConnection.java:1446)

      at java.net.URLConnection.getContentType(URLConnection.java:382)

403 is forbidden, for some reason the server server is not allowing you to access that page.
Perhaps because the request is not coming from a browser.
Avatar of Naeemg

ASKER

but if u try it to paste in browser and open this link. what will happen on ur end?
Avatar of Naeemg

ASKER

i've tried this link in several other applications and it works fine and displays contents. But in my java application using aboe code it throus IOException with code 403 that describes that its forbidden.
Why java application don't have access ? And how can i do access?
check the code on working apps to see what the difference is.
Avatar of Naeemg

ASKER

how can i see the code?
But i've tested in XMLWriter , SharpReader, and IExplorer.
then get a http proxy to view the request sent to see what you are doing different.
There is something in (or not in) your request that the web server does not like.
Avatar of Naeemg

ASKER

dear Objects, i've posted my code above, would u plz append the proxy code?
no i mean use a http proxy like http://www.xk72.com/charles/
Avatar of Naeemg

ASKER

i've read , but didn't get any solution.
Try setting the User-Agent header to MSIE or something plausible
Avatar of Naeemg

ASKER

dear CEHJ, would u plz brief me, i couldn't fully understand.
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
yes mentioned that earlier :)
>> yes mentioned that earlier :)

Where?
Avatar of Naeemg

ASKER

Great reply CEHJ. wonderful.
Avatar of Naeemg

ASKER

I would like u to plz explain about: (perameters also).

connection.setRequestProperty("User-Agent", "MSIE");

and
thanks.
> I would like u to plz explain about: (perameters also).

It makes the request look more like its coming from a browser.

The site may be doing some (very primitive) checking that scripts are not hitting the site (or programs like yours).  Checking for a legitimate user agent string is a way of checking that the user agent (browser) is legit as far as they are concerned
:-)
WHich is exactly what I suggested was the problem earlier :)  Good to see you got is solved.