简体   繁体   中英

403 forbidden for url in java but not in browser

I'm behind a corporate firewall, but i can paste the URL in my browser with and without my proxy settings enabled within the browser and can retrieve the data fine. I just can't within java.

Any ideas?

Code:

 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;
}

Error:

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)

Note: The '' portion is for anonymizing.

As you are receiving a "403: Forbidden" error, it means that your Java code can reach the URL, but it lacks something that is required to access it.

In the browser, press F12 (developer/debug mode) and request the URL again. Check the headers and cookies that are being sent. Most likely you will need to add one of these for you to be able to receive the content you need.

添加“User-Agent”标头为我修复了它:

connection.setRequestProperty("User-Agent", "Mozilla/5.0");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM