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(conne ction.getC ontentType ());
//System.out.println(conne ction.getC ontent().t oString()) ;
inStream = connection.getInputStream( );
}
catch (UnknownHostException e)
{
}
catch (IOException e)
{
System.out.println("Error in opening URLConnection, Reading or Writing");
}
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(conne
//System.out.println(conne
inStream = connection.getInputStream(
}
catch (UnknownHostException e)
{
}
catch (IOException e)
{
System.out.println("Error in opening URLConnection, Reading or Writing");
}
ASKER
when it reaches at
inStream = connection.getInputStream( );
then IOException occurrs.
inStream = connection.getInputStream(
then IOException occurrs.
ASKER
java.io.IOException
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:591)
at java.net.URLConnection.get Content(UR LConnectio n.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.get ContentTyp e(URLConne ction.java :382)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:591)
at java.net.URLConnection.get
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.get
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.
ASKER
but if u try it to paste in browser and open this link. what will happen on ur end?
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?
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.
ASKER
how can i see the code?
But i've tested in XMLWriter , SharpReader, and IExplorer.
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.
There is something in (or not in) your request that the web server does not like.
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/
ASKER
i've read , but didn't get any solution.
Try setting the User-Agent header to MSIE or something plausible
ASKER
dear CEHJ, would u plz brief me, i couldn't fully understand.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
yes mentioned that earlier :)
>> yes mentioned that earlier :)
Where?
Where?
ASKER
Great reply CEHJ. wonderful.
ASKER
I would like u to plz explain about: (perameters also).
connection.setRequestPrope rty("User- Agent", "MSIE");
and
thanks.
connection.setRequestPrope
and
thanks.
> I would like u to plz explain about: (perameters also).
It makes the request look more like its coming from a browser.
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.
System.out.println("Error in opening URLConnection, Reading or Writing");
e.printStackTrace();