• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • Carey Brown
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • fred rosenberger
  • salvin francis

URL reader error in my web application

 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 67553
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know. 403 is Access forbidden. but why does it work on .NET and not on Java ?
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)");
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic