在Java代码中获取“ java.io.IOException:服务器返回的HTTP响应代码:URL的403:”

时间:2019-01-10 10:56:07

标签: java

我正在尝试验证10万个人的linkedIn配置文件,并编写了一个伪代码,但给出了“ java.io.IOException:服务器返回的HTTP响应代码:URL为https://www.linkedin.com/in/test.user的403:”

我尝试设置其他setRequestProperty,但是不起作用。

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");

}

以上代码的输出代码为:

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)

4 个答案:

答案 0 :(得分:0)

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.

您要么无权访问该站点(如果在同一浏览器的不同选项卡之间共享访问权限,也可以尝试从浏览器登录并尝试从同一浏览器运行脚本,但是请确保您已获得授权),或者对链接的请求包含网站不想共享的敏感信息。

答案 1 :(得分:0)

HTTP error code 403是与对所请求资源的授权有关的错误:

  

HTTP 403提供了与HTTP 401不同的错误情况;虽然在客户端未通过身份验证时返回HTTP 401,并暗示在进行有效身份验证后可能会返回成功的响应,但是在由于某种原因而不允许客户端访问客户端时,返回HTTP 403 认证

答案 2 :(得分:0)

很难理解你的工作方式。 LinkedIn链接需要登录。但是您确实需要以某种方式对其进行调试,并且需要使用正确的原始原始输出到服务器,否则您将无法完成输出。如果您有Java示例程序,请查看他们是否有错字,但是再次没有LinkedIn的屏幕截图或文本,我将无法调试它。也许尝试添加示例,我将尽力为您提供帮助(只需让我使用公开个人资料登录其他地方即可)。当然,还要确保在正确的字段中有您的真实密码和用户帐户(authUsrauthPwd不应像其他任何东西一样被复制粘贴)。

答案 3 :(得分:0)

HTTP 403是来自服务器的合法响应。因此该行为是有效的。但是,我建议使用一些HTTP客户端实用程序,而不是编写自己的代码来发出Http请求。这将减少由您自己的代码引起的问题的机会。作为某些Http客户端,我建议使用Apache Http ClientOK Http client或MgntUtils Http客户端(请参阅MgntUtils HttpClient javadoc here,github上的Full MgntUtils库为here,Maven存储库为{{ 3}})。
免责声明:MgntUtils库是我编写的