Java http 请求在网站上被禁止
Java http requests get banned on site
我正在尝试使用我编写的 Java 程序从站点获取信息。
当我使用 selenium 时一切正常,但是当我尝试使用 prowser
或 httpurlconnection
时,该站点阻止了我的 IP。
我正在将用户代理设置为 Chrome/44.0.2403.155
,以及原始站点请求中的其他参数(我在 HTTPFox
的帮助下获得它们)。
我正在处理 cookie(我从 FirefoxWebDriver
获得的 cookie),但结果总是一样:我的 IP 被阻止了。
我获得了我需要的信息六次,然后站点封锁了我的 IP。当我不使用 cookies ("logged out mode") 时一切正常。
您访问的站点是通过 HTTPS 还是 HTTP 访问的?
请尝试将 GET 请求打印到站点的代码。
public class ConnectToHTTPS {
public static void main(String[] args) throws Exception {
// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[] {new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
}
};
// Install the all-trusting trust manager
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
// Create all-trusting host name verifier
HostnameVerifier allHostsValid = new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
};
// Install the all-trusting host verifier
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
URL url = new URL("YOUR SITE ADDRESS");
HttpURLConnection cox= (HttpURLConnection) url.openConnection();
// URLConnection con = url.openConnection();
Reader reader = new InputStreamReader(cox.getInputStream());
while (true) {
int ch = reader.read();
if (ch==-1) {
break;
}
System.out.print((char)ch);
}
}
}
或
只需尝试在您的 java 程序中设置 Proxy
System.setProperty("http.proxyHost", "YOUR PROXY");
URL url = new URL("YOUR WebSite");
URLConnection con = url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
con.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
或者如果证书问题得到解决
You can use GET once the above worked
// Install the all-trusting host verifier
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
String url = "http://www.google.com/search?q=mkyong";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// optional default is GET
con.setRequestMethod("GET");
我正在尝试使用我编写的 Java 程序从站点获取信息。
当我使用 selenium 时一切正常,但是当我尝试使用 prowser
或 httpurlconnection
时,该站点阻止了我的 IP。
我正在将用户代理设置为 Chrome/44.0.2403.155
,以及原始站点请求中的其他参数(我在 HTTPFox
的帮助下获得它们)。
我正在处理 cookie(我从 FirefoxWebDriver
获得的 cookie),但结果总是一样:我的 IP 被阻止了。
我获得了我需要的信息六次,然后站点封锁了我的 IP。当我不使用 cookies ("logged out mode") 时一切正常。
您访问的站点是通过 HTTPS 还是 HTTP 访问的?
请尝试将 GET 请求打印到站点的代码。
public class ConnectToHTTPS {
public static void main(String[] args) throws Exception {
// Create a trust manager that does not validate certificate chains
TrustManager[] trustAllCerts = new TrustManager[] {new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
}
};
// Install the all-trusting trust manager
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, trustAllCerts, new java.security.SecureRandom());
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
// Create all-trusting host name verifier
HostnameVerifier allHostsValid = new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
};
// Install the all-trusting host verifier
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
URL url = new URL("YOUR SITE ADDRESS");
HttpURLConnection cox= (HttpURLConnection) url.openConnection();
// URLConnection con = url.openConnection();
Reader reader = new InputStreamReader(cox.getInputStream());
while (true) {
int ch = reader.read();
if (ch==-1) {
break;
}
System.out.print((char)ch);
}
}
}
或
只需尝试在您的 java 程序中设置 Proxy
System.setProperty("http.proxyHost", "YOUR PROXY");
URL url = new URL("YOUR WebSite");
URLConnection con = url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(
con.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
或者如果证书问题得到解决
You can use GET once the above worked
// Install the all-trusting host verifier
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
String url = "http://www.google.com/search?q=mkyong";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// optional default is GET
con.setRequestMethod("GET");