Home > Blockchain >  Server responding with response code 400 when making http head request using httpurlconnection
Server responding with response code 400 when making http head request using httpurlconnection

Time:08-24

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 ?

CodePudding user response:

I couldn't found any major issue on your code. I've tried to run your code inside one of my Android project. To avoid NetworkOnMainThreadException I've run the code snippet inside a thread. Then run it and found the expected response header in the log.

Possible Solutions (What can you do now?)

  1. Try run my callApi() method inside your project.
  2. Change your url to https://pagalworld.com.se/siteuploads/files/sfd14/6934/Har Har Shambhu Ringtone_320(PagalWorld.com.se).mp3 (replaced SPACE with ). There is a chance for BAD request with white space URL.

Here is the significant output

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+kXxWPQCJayYi23um4V7mlUXd4kdWOXN/rXrTN6NA7sZWa+zU4L6tCx2v28VAkH51TPNfDVbeHGTHgwY8PCuulIdN3ek/loEn"}],"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

What I've changed in the code?

  1. Run the code inside a thread
  2. Update USER_AGENT_REQUEST_HEADER field and value.
  3. Get response headers in a Map object.
  4. Iterate the Map object and print key-value in log.

Here is the code sample:

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();
}

So, as far I understand I can say, there is no issue in your code.

  • Related