我正在使用Ubuntu 12.04。这是使用URLConnection实现HTTP GET方法的简单代码。使用URLConnection的HTTP GET请求无法访问任何页面
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
public class HttpURLConnectionExample {
private final String USER_AGENT = "Mozilla/5.0";
public static void main(String[] args) throws Exception {
HttpURLConnectionExample http = new HttpURLConnectionExample();
System.out.println("Testing 1 - Send Http GET request");
http.sendGet();
}
// HTTP GET request
private void sendGet() throws Exception {
String url = "https://www.google.com/search?q=flower";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// optional default is GET
con.setRequestMethod("GET");
//add request header
con.setRequestProperty("User-Agent", USER_AGENT);
int responseCode = con.getResponseCode();
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
//print result
System.out.println(response.toString());
}
}
但是当我编译并运行Ubuntu的终端这段代码,这段代码的输出不产生由URL指定的页面的内容。相反,它给出以下输出
Testing 1 - Send Http GET request
Sending 'GET' request to URL : http://www.google.com/search?q=flower
Response Code : 307
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"><html><head><title>307 Temporary Redirect</title></head><body><h1>Temporary Redirect</h1><p>The document has moved <a href="https://ifwb.iitb.ac.in/index.php?add=www.google.com/search">here</a>.</p><hr><address>Apache/2.2.22 (Fedora) Server at www.google.com Port 80</address></body></html>
此问题适用于我在代码中指定的任何URL。此外,我尝试使用Telnet客户端来访问网络内容,如
远程登录www.google.com 80 GET/
,并让不仅为www.google.com但对于类似的结果每个网址。 我是IIT Bombay的学生,可能与https://ifwb.iitb.ac.in有关。 我也想坚持java.net而不是apache httpclient。所以帮我解决这个问题。