我想写一个站点的get请求()。但当我这样做时,它会说:“哎呀!如果你看到这个,你的浏览器没有正确加载页面。请尝试按Control-F5强制重新加载页面。”我不明白为什么。我刚从浏览器(google-chrome)复制了请求。我收到了200回复。我该如何解决?谢谢!
代码: import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.HttpClientBuilder;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.CookieHandler;
import java.net.CookieManager;
public class HttpUrlConnectionExample2 {
private final String USER_AGENT = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.85 Safari/537.36";
public static void main(String[] args) throws Exception {
String url = "https://www.interpals.net";
HttpUrlConnectionExample2 http = new HttpUrlConnectionExample2();
CookieHandler.setDefault(new CookieManager());
String page = http.sendGet(url);
if (page.contains("Oops")) {
System.out.println("HAS OOPS");
}
}
private String sendGet(String link) throws Exception {
HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(link);
request.addHeader("user-agent", USER_AGENT);
request.addHeader("method", "GET");
request.addHeader("path", "/index.php");
request.addHeader("scheme", "https");
request.addHeader("version", "HTTP/1.1");
request.addHeader("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
//request.addHeader("accept-encoding", "gzip, deflate, sdch");
request.addHeader("accept-language", "ru,en-US;q=0.8,en;q=0.6");
request.addHeader("cache-control", "max-age=0");
request.addHeader("upgrade-insecure-requests", "1");
HttpResponse response = client.execute(request);
System.out.println("\nSending 'GET' request to URL : " + link);
System.out.println("Response Code : " +
response.getStatusLine().getStatusCode());
BufferedReader rd = new BufferedReader(
new InputStreamReader(response.getEntity().getContent()));
StringBuffer result = new StringBuffer();
String line;
while ((line = rd.readLine()) != null) {
result.append(line);
}
System.out.println(result);
return result.toString();
}
}