Vam*_*esh 3 java http http-status-code-403
我正在尝试使用java创建一个http get请求,当我执行代码时,我正在获取403 forbidden代码.有没有办法摆脱它?我的代码是
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());
}
}
Run Code Online (Sandbox Code Playgroud)
如果您在API和服务条款之外执行此操作,Google搜索将拒绝您的请求.
$ 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.
Run Code Online (Sandbox Code Playgroud)
他们可能会看着你的User-Agent标题.如果您添加类似于浏览器的浏览器,它可能会起作用,但您正在规避服务条款并寻求不受支持的行为.(对不起,我也试了一次.)
仅供参考,这是主要搜索引擎(Google,Bing,Yahoo)的常见限制.有一些你可以编程查询; 你将不得不使用那些.