Why I get 411 Length required error?

前端 未结 8 1844
我寻月下人不归
我寻月下人不归 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条回答
  •  萌比男神i
    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

提交回复
热议问题