Why I get 411 Length required error?

前端 未结 8 1814
我寻月下人不归
我寻月下人不归 2020-12-04 19:01

This is how I call a service with .NET:

var requestedURL = \"https://accounts.google.com/o/oauth2/token?code=\" + code + \"&client_id=\" + client_id + \"         


        
相关标签:
8条回答
  • 2020-12-04 19:25

    When you're using HttpWebRequest and POST method, you have to set a content (or a body if you prefer) via the RequestStream. But, according to your code, using authRequest.Method = "GET" should be enough.

    In case you're wondering about POST format, here's what you have to do :

    ASCIIEncoding encoder = new ASCIIEncoding();
    byte[] data = encoder.GetBytes(serializedObject); // a json object, or xml, whatever...
    
    HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
    request.Method = "POST";
    request.ContentType = "application/json";
    request.ContentLength = data.Length;
    request.Expect = "application/json";
    
    request.GetRequestStream().Write(data, 0, data.Length);
    
    HttpWebResponse response = request.GetResponse() as HttpWebResponse;
    
    0 讨论(0)
  • 2020-12-04 19:27

    Change the way you requested the method from POST to GET ..

    0 讨论(0)
  • 2020-12-04 19:29
    var requestedURL = "https://accounts.google.com/o/oauth2/token?code=" + code + "&client_id=" + client_id + "&client_secret=" + client_secret + "&redirect_uri=" + redirect_uri + "&grant_type=authorization_code";
    HttpWebRequest authRequest = (HttpWebRequest)WebRequest.Create(requestedURL);
    authRequest.ContentType = "application/x-www-form-urlencoded";
    authRequest.Method = "POST";
    //Set content length to 0
    authRequest.ContentLength = 0;
    WebResponse authResponseTwitter = authRequest.GetResponse();
    

    The ContentLength property contains the value to send as the Content-length HTTP header with the request.

    Any value other than -1 in the ContentLength property indicates that the request uploads data and that only methods that upload data are allowed to be set in the Method property.

    After the ContentLength property is set to a value, that number of bytes must be written to the request stream that is returned by calling the GetRequestStream method or both the BeginGetRequestStream and the EndGetRequestStream methods.

    for more details click here

    0 讨论(0)
  • 2020-12-04 19:30

    When you make a POST HttpWebRequest, you must specify the length of the data you are sending, something like:

    string data = "something you need to send"
    byte[] postBytes = Encoding.ASCII.GetBytes(data);
    request.ContentLength = postBytes.Length;
    

    if you are not sending any data, just set it to 0, that means you just have to add to your code this line:

    request.ContentLength = 0;
    

    Usually, if you are not sending any data, chosing the GET method instead is wiser, as you can see in the HTTP RFC

    0 讨论(0)
  • 2020-12-04 19:33

    you need to add Content-Length: 0 in your request header.

    a very descriptive example of how to test is given here

    0 讨论(0)
  • 2020-12-04 19:36

    Hey i'm using Volley and was getting Server error 411, I added to the getHeaders method the following line :

    params.put("Content-Length","0");
    

    And it solved my issue

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