Why I get 411 Length required error?

前端 未结 8 1840
我寻月下人不归
我寻月下人不归 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;
    

提交回复
热议问题