我正在尝试验证 100K 人的LinkedIn配置文件并编写了一个虚拟代码,但它给出了"java.io.IOException:服务器返回 HTTP 响应代码:403 用于 URL:https://www.linkedin.com/in/test.user"
我尝试设置不同的设置请求属性,但不起作用。
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)
代码 403 是与对请求的资源的授权相关的错误:
HTTP 403 提供了与 HTTP 401不同的错误情况;当客户端未进行身份验证时返回 HTTP 401,并暗示在有效身份验证后可能会返回成功的响应,而当由于身份验证以外的某种原因不允许客户端访问资源时,将返回 HTTP 403
很难理解你是如何工作的。LinkedIn链接需要登录。但是您确实需要以某种方式调试它,并且需要使用正确的原始真实输出到服务器,否则您将无法完成它。如果您有Java示例程序,请查看他们或您是否有错别字,但同样没有屏幕截图或LinkedIn文本,我无法调试它。也许尝试添加示例,我会尽力帮助您(只需让我使用我的公开个人资料登录其他地方)。当然,还要确保在正确的字段中有您的真实密码和用户帐户(authUsr
,authPwd
不得像其他所有内容那样复制粘贴)。
HTTP 403 是来自服务器的合法响应。因此,该行为是有效的。但是,我建议使用一些HTTP客户端实用程序,而不是编写自己的代码来发出Http请求。这将减少由您自己的代码引起的问题的可能性。对于一些 Http 客户端,我会建议使用 Apache Http Client 或 OK Http 客户端或 MgntUtils Http 客户端(参见 MgntUtils HttpClient javadoc 这里,github 上的完整 MgntUtils 库在这里,Maven 存储库在这里)。
免责声明:MgntUtils库由我编写
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.
您要么无权访问该站点(尝试从浏览器登录并尝试从同一浏览器运行脚本,如果您的访问权限在同一浏览器的不同选项卡之间共享,这也很好,但请确保您已获得授权)或对链接的请求包含站点不想共享的敏感信息。