2017-03-01 84 views
0

我使用公司防火墙,但是我可以将浏览器中的URL粘贴到浏览器中,并且无需在浏览器中启用代理设置,并且可以检索数据。我不能在java内部。403禁止在java中使用url,但在浏览器中禁止使用

任何想法?

代码:

private static String getURLToString(String strUrl) throws IOException { 
    // LOG.debug("Calling URL: [" + strUrl + "]"); 
    String content = ""; 

    URLConnection connection = new URL(strUrl).openConnection(); 
    connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11"); 
    connection.connect(); 
    BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), Charset.forName("UTF-8"))); 

    String inputLine; 
    while ((inputLine = br.readLine()) != null) { 
     content += inputLine; 
    } 

    br.close(); 

    return content; 
} 

错误:

java.io.FileNotFoundException: Response: '403: Forbidden' for url: '<url here>' 
    at weblogic.net.http.HttpURLConnection.getInputStream(HttpURLConnection.java:778) 
    at weblogic.net.http.SOAPHttpURLConnection.getInputStream(SOAPHttpURLConnection.java:37) 

注: '' 部分为匿名。

+0

您可以使用'tcpdump'或'wireshark'转储流量并分析差异。 –

回答

1

当您收到“403:Forbidden”错误时,这意味着您的Java代码可以访问该URL,但它缺少访问它所需的内容。

在浏览器中,按F12(开发/调试模式)并再次请求URL。检查正在发送的标题和Cookie。您很可能需要添加其中的一个才能够接收您需要的内容。

+0

我通过connection.setRequestProperty()手动添加了它们,并收到相同的错误。 – qbolt