?
Solved

IOException while getting InputStream from URLConnection

Posted on 2005-05-10
24
Medium Priority
?
2,753 Views
Last Modified: 2008-02-01
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");
      }
0
Comment
Question by:Naeemg
  • 10
  • 9
  • 5
24 Comments
 
LVL 92

Expert Comment

by:objects
ID: 13966039
What sort of IOException, and where is it occurring?

     System.out.println("Error in opening URLConnection, Reading or Writing");
     e.printStackTrace();
0
 
LVL 5

Author Comment

by:Naeemg
ID: 13966053
when it reaches at
inStream = connection.getInputStream();
then IOException occurrs.
0
 
LVL 5

Author Comment

by:Naeemg
ID: 13966060
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)

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 92

Expert Comment

by:objects
ID: 13966073
403 is forbidden, for some reason the server server is not allowing you to access that page.
0
 
LVL 92

Expert Comment

by:objects
ID: 13966077
Perhaps because the request is not coming from a browser.
0
 
LVL 5

Author Comment

by:Naeemg
ID: 13966089
but if u try it to paste in browser and open this link. what will happen on ur end?
0
 
LVL 5

Author Comment

by:Naeemg
ID: 13966173
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?
0
 
LVL 92

Expert Comment

by:objects
ID: 13966278
check the code on working apps to see what the difference is.
0
 
LVL 5

Author Comment

by:Naeemg
ID: 13966295
how can i see the code?
But i've tested in XMLWriter , SharpReader, and IExplorer.
0
 
LVL 92

Expert Comment

by:objects
ID: 13966336
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.
0
 
LVL 5

Author Comment

by:Naeemg
ID: 13966349
dear Objects, i've posted my code above, would u plz append the proxy code?
0
 
LVL 92

Expert Comment

by:objects
ID: 13966506
no i mean use a http proxy like http://www.xk72.com/charles/
0
 
LVL 5

Author Comment

by:Naeemg
ID: 13966579
i've read , but didn't get any solution.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13966598
Try setting the User-Agent header to MSIE or something plausible
0
 
LVL 5

Author Comment

by:Naeemg
ID: 13966674
dear CEHJ, would u plz brief me, i couldn't fully understand.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 800 total points
ID: 13966772
     static void uagent() {
            java.net.URL thisURL = null;
            InputStream inStream = null;
            BufferedReader in = null;
            String buffer = null;
            try {
                  try {
                        thisURL = new java.net.URL(
                                    "http://groups.google.com.pk/group/Java--XML/feed/topics.xml");
                        URLConnection connection = thisURL.openConnection();
                        connection.setRequestProperty("User-Agent", "MSIE");
                        // connection.connect();
                        // System.out.println(connection.getContentType());
                        // System.out.println(connection.getContent().toString());
                        inStream = connection.getInputStream();
                        in = new BufferedReader(new InputStreamReader(inStream));
                        while ((buffer = in.readLine()) != null) {
                              System.out.println(buffer);
                        }
                        
                  } catch (Exception e) {
                        e.printStackTrace();
                  }
            } finally {
                  try {
                        in.close();
                  } catch (IOException e) {
                        /* ignore */
                  }
            }
      }
0
 
LVL 92

Expert Comment

by:objects
ID: 13966797
yes mentioned that earlier :)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13966810
>> yes mentioned that earlier :)

Where?
0
 
LVL 5

Author Comment

by:Naeemg
ID: 13966930
Great reply CEHJ. wonderful.
0
 
LVL 5

Author Comment

by:Naeemg
ID: 13966951
I would like u to plz explain about: (perameters also).

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

and
thanks.
0
 
LVL 92

Expert Comment

by:objects
ID: 13966966
> I would like u to plz explain about: (perameters also).

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

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13966970
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
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13967020
:-)
0
 
LVL 92

Expert Comment

by:objects
ID: 13967066
WHich is exactly what I suggested was the problem earlier :)  Good to see you got is solved.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month17 days, 1 hour left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question