2017-03-01 41 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