简体   繁体   中英

Getting “java.io.IOException: Server returned HTTP response code: 403 for URL: ” in java code

I am trying to validate the linkedIn profile of 100K person and wrote a dummy code but its giving "java.io.IOException: Server returned HTTP response code: 403 for URL: https://www.linkedin.com/in/test.user "

I have tried setting different setRequestProperty but not working.

public static void main(final String[] args) {

    String output = "";
    int TIMEOUT_VALUE = 99999999;
    HttpURLConnection conn = null;
    BufferedReader br = null;
    String urlEndPoint = "";
    String authUser = "";
    String authPwd = "";
    try {
        long start = System.nanoTime();

        urlEndPoint = "https://www.linkedin.com/in/test.user";
        authUser = "linkedin-username";
        authPwd = "linkedin-password";
        URL url = new URL(urlEndPoint);
        conn = (HttpURLConnection) url.openConnection();
        conn.setRequestProperty("username", authUser);
        conn.setRequestProperty("password", authPwd);
        conn.setRequestProperty("Connection", "Keep-Alive");
        conn.setRequestProperty("Keep-Alive", "header");
        conn.setRequestProperty("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
        conn.setConnectTimeout(TIMEOUT_VALUE);
        conn.setReadTimeout(TIMEOUT_VALUE);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Accept-Language", "en-US,en;q=0.9,mt;q=0.8");
        conn.setRequestProperty("Accept-Encoding", "gzip,deflate,br");
        conn.setRequestProperty("Host", "www.linkedin.com");
        conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36");
        conn.setRequestProperty("http.agent", "Chrome/71.0.3578.80 (Windows NT 10.0; Win64; x64)");
        conn.setDoOutput(true);
        String userPassword = authUser + ":" + authPwd;
        String encoding = Base64Encoder.encode(userPassword);
        conn.setRequestProperty("Authorization", "Basic " + encoding);
        OutputStream os = conn.getOutputStream();
        os.flush();
        conn.connect();
        br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
        while ((output = br.readLine()) != null) {
            System.out.println(output);
        }

        if (br != null) {
            br.close();
        }
        if (os != null) {
            os.close();
        }

        long elapsed = System.nanoTime() - start;

    } catch (MalformedURLException e) {
        //this.logger.error("Error occurred during processPartyTerrRelationship ", e);
        e.printStackTrace();
    } catch (IOException e) {
        //this.logger.error("Error occurred during processPartyTerrRelationship ", e);
        e.printStackTrace();
    } catch (Exception e) {
        //this.logger.error("Error occurred during processPartyTerrRelationship ", e);
        e.printStackTrace();
    } finally {
        try {
            if (conn != null) {

                conn.disconnect();
            }
        } catch (Exception e) {
            //this.logger.error("Error occurred during processPartyTerrRelationship ", e);
            e.printStackTrace();
        }
    }
    //logger.info("processPartyTerrRelationship called ends");

}

The outcode of above code is :

java.io.IOException: Server returned HTTP response code: 403 for URL: https://www.linkedin.com/in/test.user
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1894)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1492)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:263)
    at ValidateLinkedInProfiles.main(ValidateLinkedInProfiles.java:57)

HTTP 403 is a standard HTTP status code communicated to clients by an HTTP server to indicate that the server understood the request, but will not fulfill it. There are a number of sub-status error codes that provide a more specific reason for responding with the 403 status code.

您或者没有该站点的访问权限(尝试通过浏览器登录并尝试从同一浏览器运行脚本,如果您的访问权限在同一浏览器的不同标签之间共享,也可以,但是请确保您授权)或对链接的请求包含网站不想共享的敏感信息。

The HTTP error code 403 is an error related to the authorization to the requested resource:

HTTP 403 provides a distinct error case from HTTP 401; while HTTP 401 is returned when the client has not authenticated, and implies that a successful response may be returned following valid authentication, HTTP 403 is returned when the client is not permitted access to the resource for some reason besides authentication

It's hard to understand how you're working. The LinkedIn link requires login. But you indeed need to debug it somehow and need a raw real output to the server with the correct one otherwise you will not complete it. If you have Java example program, see if they or you have a typo, but again without screenshot or text from LinkedIn I cannot debug it. Maybe try to add the examples and I will try to help you (just make me login with my public profile to other places). Also make sure there is your real password and your user account in the correct fields of course ( authUsr , authPwd shall not be copy paste unlike everything else).

HTTP 403 is a legitimate response from a server. So the behavior is valid. However, I would recommend to use some HTTP client utility rather then writing your own code to make Http request. This will reduce the chance of a problem caused by your own code. As some Http clients I would suggest Apache Http Client or OK Http client or MgntUtils Http Client (see MgntUtils HttpClient javadoc here , Full MgntUtils library on github is here and Maven repository is here ).
Disclaimer: MgntUtils library is written by me

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