2013-06-20 57 views
0

我正在使用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。所以帮我解决这个问题。

回答

0

看来你被服务器拒绝,因为不完整的请求。使用任何嗅探器(如Fiddler或Wireshark)“学习示例”是一个不错的主意:比较您的请求和来自特定软件(如浏览器)的请求。

下面是Wireshark dump的摘录,IE10如何向感兴趣的URL发送GET请求。正如您所看到的,描述客户端功能和期望的各个领域都是如此,所以查询的服务器可以以最合适和易用的形式返回答案。与谷歌/ RFC请教看到经过的每个参数的含义:

GET /搜Q =花HTTP/1.1

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

接受语言:EN-US

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

接受编码:gzip,紧缩

主持人:www.google.com

DNT:1个

连接:保持活动

的Cookie:一些民营这里说明]