Question

I am trying to make a http get request using java ,when i am executing the code ,i am getting 403 forbiddencode .Is there any way to get rid of that ?my code is

 import java.io.BufferedReader;
 import java.io.InputStreamReader;
 import java.net.*;


public class Http {
private final String USER_AGENT = "Mozilla/5.0";
public static void main(String [] args){

    Http http=new Http();
    try {
        http.get();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}
private void get() throws Exception{
    URL ob=new URL("http://www.google.com/search?q=vamsi");
    HttpURLConnection con=(HttpURLConnection) ob.openConnection();
    con.setRequestMethod("GET");
    con.setRequestProperty("USER_AGENT", USER_AGENT);
    int responseCode=con.getResponseCode();
    System.out.println("Response code is "+responseCode);
    BufferedReader buf=new BufferedReader(new InputStreamReader(con.getInputStream()));
    String input;
    StringBuffer response =new StringBuffer();
    while((input=buf.readLine())!=null){
        response.append(input);

    }
    buf.close();

    System.out.println(response.toString());
}   


}
Was it helpful?

Solution

Google Search will deny your request if you are doing it outside of their API and Terms of Service.

$ wget http://www.google.com/search?q=vamsi
Resolving www.google.com (www.google.com)... 74.125.225.212, 74.125.225.210, 74.125.225.211, ...
Connecting to www.google.com (www.google.com)|74.125.225.212|:80... connected.
HTTP request sent, awaiting response... 403 Forbidden
2014-02-01 23:21:08 ERROR 403: Forbidden.

They may be looking at your User-Agent header. If you add one that resembles a browser, it may work, though you are circumventing the Terms of Service and seeking unsupported behavior. (Sorry, I too tried this once.)

See also Why does Google Search return HTTP Error 403?

FYI, this is a common restriction of major search engines (Google, Bing, Yahoo). There are some that you can query programatically; you will have to use those.

OTHER TIPS

This is the solution:

con.setRequestProperty("User-Agent", USER_AGENT);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top