Skip to main content

403 code on google using URLConnection and URLEncoder

2 replies [Last post]
pinoyraja
Offline
Joined: 2006-09-20

hi. i would like make a method to get a live currency converter from dollar to peso. i tried getting the data using the a post to google using "http://www.google.com/search?q=1+usd+in+php"

if i post this to the browser, the page goes fine. if i put this in the URL object, i get a 403 code. here is the complete code

import java.io.*;
import java.net.*;

public class WebPull {

public static void main (String[] args) {
try {
String data = URLEncoder.encode("1 usd in php", "UTF-8");
URL url = new URL("http://www.google.com/search?q=" + data);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);

// Get the response
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
StringBuilder strGarb = new StringBuilder();
while ((line = rd.readLine()) != null) {
// Process line...
strGarb.append(line);
}
rd.close();
int pos1 = strGarb.indexOf("1 U.S. dollar = ");
int pos2 = strGarb.indexOf(" Philippine pesos");
(if pos1 > -1)
String output = strGarb.substring(pos1, pos2);
else
String output = "currency not found"
System.out.println("output is " + output);

} catch (MalformedURLException mue) {
mue.printStackTrace();
System.exit(1);

} catch (IOException ioe) {
ioe.printStackTrace();
System.exit(1);
}

} // end of main

} // end of class definition

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
pinoyraja
Offline
Joined: 2006-09-20

thanks man for the tip. after googling, i found out this tip. ill go try it tomorrow and see if it works

you need to fool Google by pretending to be a legitimate browser.

String search= "What you want to search for";
String google="http://www.google.ca/search?q=" + search + "&hl=en&ie=UTF-8&oe=UTF8";

URL urlObject = new URL(google);
URLConnection con = urlObject.openConnection();
con.setRequestProperty
( "User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)" );
webData = new BufferedReader(new InputStreamReader(con.getInputStream()));
...

alanb
Offline
Joined: 2005-08-08

403 = Forbidden (see the status code section of RFC2616). You make find that the site checks the User-Agent request header in which case setting the http.agent system property to something that is allowed by the site could work.