To a certain website , i am making HTTP head request to get header data using java. I have used the following code to do that.
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();
}
Now the problem is , Server is responding with response code 400 (Bad request). But when i am making the same request from postman with same request headers, it gives response code 200 (Ok).
http head request using Postman
Another thing is, if i am making the request with same request headers as before using okhttp client library then also it's responding with response code 200.
Now what's wrong with my code ?