簡體   English   中英

在Java代碼中獲取“ java.io.IOException:服務器返回的HTTP響應代碼:URL的403:”

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

我正在嘗試驗證100K個人的linkedIn配置文件,並編寫了一個偽代碼,但給出了“ java.io.IOException:服務器返回的HTTP響應代碼:403表示URL: https//www.linkedin.com/in/test。用戶

我嘗試設置不同的setRequestProperty,但是不起作用。

公共靜態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");

}

以上代碼的輸出代碼為:

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.

您或者沒有該站點的訪問權限(嘗試通過瀏覽器登錄並嘗試從同一瀏覽器運行腳本,如果您的訪問權限在同一瀏覽器的不同標簽之間共享,也可以,但是請確保您授權)或對鏈接的請求包含網站不想共享的敏感信息。

HTTP錯誤代碼403是與對所請求資源的授權有關的錯誤:

HTTP 403提供了與HTTP 401不同的錯誤情況; 雖然客戶端未通過身份驗證時返回HTTP 401,並且表示在進行有效身份驗證后可能返回成功的響應, 而客戶端由於某種原因(除了身份驗證之外)由於某種原因被禁止訪問資源時,返回HTTP 403

很難理解你的工作方式。 LinkedIn鏈接需要登錄。 但是您確實需要以某種方式對其進行調試,並且需要使用正確的原始原始輸出到服務器,否則您將無法完成輸出。 如果您有Java示例程序,請查看他們是否有錯字,但是再次沒有LinkedIn的屏幕截圖或文本,我將無法調試它。 也許嘗試添加示例,我將盡力為您提供幫助(只需讓我使用公開個人資料登錄其他地方即可)。 當然,還要確保在正確的字段中有您的真實密碼和用戶帳戶( authUsrauthPwd不應像其他所有內容一樣被復制粘貼)。

HTTP 403是來自服務器的合法響應。 因此該行為是有效的。 但是,我建議使用一些HTTP客戶端實用程序,而不是編寫自己的代碼來發出Http請求。 這將減少由您自己的代碼引起的問題的機會。 由於一些HTTP客戶端,我建議的Apache HTTP客戶端OK HTTP客戶端或MgntUtils HTTP客戶端(見MgntUtils HttpClient的javadoc的位置 ,在github上完整MgntUtils庫是在這里和Maven倉庫是在這里 )。
免責聲明:MgntUtils庫是我編寫的

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM