带有URLConnection的HTTP GET请求无法访问任何页面



我正在Ubuntu 12.04上工作。这是我使用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客户端访问web内容,如

telnet www.google.comGET/

,它不仅对www.google.com,而且对每个URL都给出了类似的结果。我是印度理工学院孟买分校的一名学生,也许这与https://ifwb.iitb.ac.in有关。我也想坚持java.net,而不是apache httpclient。所以,帮帮我吧

由于请求不完整,您似乎被服务器拒绝了。使用任何嗅探器,如Fiddler或Wireshark来"以身作则"是一个好主意:比较您的请求和来自特定软件(如浏览器)的请求。

下面是一个摘自Wireshark dump的摘录,IE10如何发送GET请求到感兴趣的URL。正如您所看到的,有各种字段描述了客户端的功能和期望,因此被查询的服务器可以以最合适和可使用的形式返回答案。参考Google/RFC查看传入的每个参数的含义:

/搜索吗?q =花HTTP/1.1

接受:text/html, application/xhtml+xml, /

接收语言:en - us

User-Agent: Mozilla/5.0 (compatible;MSIE 10.0;Windows NT 6.1;WOW64;三叉戟/6.0)

Accept-Encoding: gzip, deflate

主持人:www.google.com

DNT: 1

连接:维生

Cookie:[一些私人信息在这里]

最新更新