• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

URL reader error in my web application

 
varun bihani
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to use this code in my servlet:
public static void main(String[] args) throws Exception {
URL yahoo = new URL("http://images.google.com/images?q=this");
BufferedReader in = new BufferedReader(new InputStreamReader(yahoo
.openStream()));

String inputLine;

while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);

in.close();

but it gives this error :
java.io.IOException: Server returned HTTP response code: 403 for URL: http://images.google.com/images?q=this
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:814)
at java.net.URL.openStream(URL.java:913)
at URLReader.main(URLReader.java:8)


This works for yahoo image search but not for google..
Google image search works with .NET code available at http://www.codeproject.com/cs/library/google_image_search_api.asp
but it doesnt work with java
Please help.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64959
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you looked up what a 403 error means?
 
varun bihani
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know. 403 is Access forbidden. but why does it work on .NET and not on Java ?
 
alban maillere
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know google check your user agent ...
try something like this
URLConnection urlconnection = yahoo.openConnection();
urlconnection.setRequestProperty("User-Agent",
"Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)");
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic