对于某个网站,我正在使用java进行HTTP头请求以获取头数据。我使用了以下代码来完成此操作。
String url2 = "https://pagalworld.com.se/siteuploads/files/sfd14/6934/Har Har Shambhu Ringtone_320(PagalWorld.com.se).mp3";
String req_method = "HEAD";
HttpsURLConnection conn = null;
try {
System.setProperty("sun.net.http.allowRestrictedHeaders", "true");
URL UrlConnection = new URL(url2);
conn = (HttpsURLConnection) UrlConnection.openConnection();
String host = UrlConnection.getHost().toString()+":80";
conn.setRequestMethod(req_method);
conn.setRequestProperty("Host", host);
conn.setRequestProperty("Accept", "*/*");
conn.setRequestProperty("Connection","keep-alive");
conn.setRequestProperty(DownloadApiConfig.USER_AGENT_REQUEST_HEADER, DownloadApiConfig.USER_AGENT_REQUEST_HEADER_VALUE);
System.out.println("Host:Port = "+host);
Map<String,List<String>> data = conn.getRequestProperties();
System.out.println("Connection code : "+conn.getResponseCode());
System.out.println("Connection Msg : "+conn.getResponseMessage());
for(String key:data.keySet()){
System.out.println(key + " " + data.get(key));
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
conn.disconnect();
}
现在的问题是,服务器的响应代码为400(错误的请求(。但当我用相同的请求头从邮递员那里发出相同的请求时,它会给出响应代码200(好(
使用Postman的http头请求
另一件事是,如果我使用与以前相同的请求头使用okhttp客户端库进行请求,那么它也会使用响应代码200进行响应。
现在我的代码出了什么问题?
我在您的代码中找不到任何重大问题。我试着在我的一个Android项目中运行你的代码。为了避免NetworkOnMainThreadException
,我在线程中运行了代码片段。然后运行它,并在日志中找到预期的响应标头。
可能的解决方案(你现在能做什么?(
- 尝试在项目中运行我的
callApi()
方法 - 将您的url更改为
https://pagalworld.com.se/siteuploads/files/sfd14/6934/Har%20Har%20Shambhu%20Ringtone_320(PagalWorld.com.se).mp3
(将SPACE替换为%20
(。有可能出现带有空白URL的BAD请求
这是重要的输出
I/System.out: ConnHeadRes: null HTTP/1.1 200 OK
I/System.out: ConnHeadRes: Accept-Ranges bytes
I/System.out: ConnHeadRes: Age 693
I/System.out: ConnHeadRes: CF-Cache-Status HIT
I/System.out: ConnHeadRes: CF-RAY 73f0ccbd1a5169b1-DAC
I/System.out: ConnHeadRes: Connection keep-alive
I/System.out: ConnHeadRes: content-disposition attachment
I/System.out: ConnHeadRes: Content-Length 1221768
I/System.out: ConnHeadRes: Content-Type application/octet-stream
I/System.out: ConnHeadRes: Date Tue, 23 Aug 2022 03:35:42 GMT
I/System.out: ConnHeadRes: Expect-CT max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
I/System.out: ConnHeadRes: last-modified Thu, 11 Aug 2022 14:41:49 GMT
I/System.out: ConnHeadRes: NEL {"success_fraction":0,"report_to":"cf-nel","max_age":604800}
I/System.out: ConnHeadRes: Report-To {"endpoints":[{"url":"https://a.nel.cloudflare.com/report/v3?s=nywwKM746VO8KiwrIJAIq6hoJ1D8eJphddRHtpP%2BkXxWPQCJayYi23um4V7mlUXd4kdWOXN%2FrXrTN6NA7sZWa%2BzU4L6tCx2v28VAkH51TPNfDVbeHGTHgwY8PCuulIdN3ek%2FloEn"}],"group":"cf-nel","max_age":604800}
I/System.out: ConnHeadRes: Server cloudflare
I/System.out: ConnHeadRes: Strict-Transport-Security max-age=15552000; includeSubDomains; preload
I/System.out: ConnHeadRes: Vary Accept-Encoding
I/System.out: ConnHeadRes: X-Android-Received-Millis 1661225741490
I/System.out: ConnHeadRes: X-Android-Response-Source NETWORK 200
I/System.out: ConnHeadRes: X-Android-Selected-Protocol http/1.1
I/System.out: ConnHeadRes: X-Android-Sent-Millis 1661225741442
I/System.out: ConnHeadRes: x-content-type-options nosniff
I/System.out: ConnHeadRes: x-nginx-upstream-cache-status MISS
我在代码中更改了什么
- 在线程内运行代码
- 更新
USER_AGENT_REQUEST_HEADER
字段和值 - 获取
Map
对象中的响应标头 - 迭代Map对象并在日志中打印键值
以下是代码示例:
void callApi() {
Thread thread = new Thread(() -> {
try {
String url2 = "https://pagalworld.com.se/siteuploads/files/sfd14/6934/Har Har Shambhu Ringtone_320(PagalWorld.com.se).mp3";
String req_method = "HEAD";
HttpsURLConnection conn = null;
try {
System.setProperty("sun.net.http.allowRestrictedHeaders", "true");
URL UrlConnection = new URL(url2);
conn = (HttpsURLConnection) UrlConnection.openConnection();
String host = UrlConnection.getHost() + ":80";
conn.setRequestMethod(req_method);
conn.setRequestProperty("Host", host);
conn.setRequestProperty("Accept", "*/*");
conn.setRequestProperty("Connection", "keep-alive");
conn.setRequestProperty("user-agent", "android");
System.out.println("Host:Port = " + host);
Map<String, List<String>> data = conn.getRequestProperties();
System.out.println("Connection code : " + conn.getResponseCode());
System.out.println("Connection Msg : " + conn.getResponseMessage());
for (String key : data.keySet()) {
System.out.println(key + " " + data.get(key));
}
Map<String, List<String>> responseHeader = conn.getHeaderFields();
for (Map.Entry<String, List<String>> entry : responseHeader.entrySet()) {
for (String value: entry.getValue()) {
System.out.println("ConnHeadRes: " + entry.getKey() + " " + value);
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (ProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
conn.disconnect();
}
} catch (Exception e) {
e.printStackTrace();
}
});
thread.start();
}
所以,据我所知,我可以说,您的代码中没有问题。