Java HTTPPost Request with Apache HttpClient

前端 未结 2 1507
甜味超标
甜味超标 2021-01-22 03:14

I need a java program to generate the following request. I am using Apache HttpClient Library but still not able to produce a request like this:

This is what my python p

相关标签:
2条回答
  • 2021-01-22 03:44

    Accept-Encoding is part of the HTTP Header, in addition to the parameters that you mistakenly sent as POST parameters:

    Here is how to send it using HTTP Client:

    httpPostRequest.setHeader("Content-Length", "6"); 
    httpPostRequest.setHeader("Accept-Encoding", "identity"); 
    httpPostRequest.setHeader("Host", "a.host.com");
    
    0 讨论(0)
  • 2021-01-22 03:58

    If you want to change the Http Header Accept-Encoding, you chang like this

    httpPostRequest.setHeader("Accept-Encoding", "identity");
    

    Your codes

    final List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(6);
    nameValuePairs.add(new BasicNameValuePair("Host: ", "a.host.com");
    nameValuePairs.add(new BasicNameValuePair("Accept-Encoding: ", "identity"));
    nameValuePairs.add(new BasicNameValuePair("Content-Length: ", "6"));
    
    httpPostRequest.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    

    are used to send the value pairs in the http request body, NOT IN THE HEADER

    0 讨论(0)
提交回复
热议问题