2013-08-21 628 views
33

這是我如何調用使用.NET服務:爲什麼我會得到411長度要求的錯誤?

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"; 
WebResponse authResponseTwitter = authRequest.GetResponse(); 

但調用此方法時,我得到:

Exception Details: System.Net.WebException: The remote server returned an error: (411) Length Required.

我該怎麼辦?

回答

54

當你使用HttpWebRequest和POST方法時,你必須通過RequestStream來設置一個內容(或者你喜歡的主體)。但是,根據你的代碼,使用authRequest.Method =「GET」應該足夠了。

如果你想知道關於POST格式,這裏是你必須做的:

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

你可以請參考我的問題「http://stackoverflow.com/questions/35308945/accessing-sftp-url-from-console-application-using-c-sharp」?我有類似的問題。我正在嘗試訪問SFTP URL –

8

當你犯了一個POST HttpWebRequest的,你必須指定你要發送的數據,類似的長度:

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

,如果你不發送任何數據,只是將其設置爲0,這意味着你只需要添加到您的代碼這一行:

request.ContentLength = 0; 

通常情況下,如果你不發送任何數據,艇員選拔的GET方法反而是明智的,因爲你可以在HTTP RFC

+0

感謝洛特...這將非常有用,我 –

+1

雖然習慣上人們所期望的數據與POST發送,這將是不正確的,不好的做法,使用GET當你*修改服務器上的數據。 POST請求應該避免客戶端和服務器之間的代理緩存。如果數據是從服務器獲取的,GET沒有問題,但由於請求而無意更改服務器上的任何內容。 – Michael

17

您需要在您的請求頭添加Content-Length: 0

如何測試一個十分生動的例子說明here

+0

是的,添加Content-Length:0有幫助。謝謝! – Roboblob

1

Google search

2nd result

System.Net.WebException: The remote server returned an error: (411) Length Required. 

This is a pretty common issue that comes up when trying to make call a REST based API method through POST. Luckily, there is a simple fix for this one.

This is the code I was using to call the Windows Azure Management API. This particular API call requires the request method to be set as POST, however there is no information that needs to be sent to the server.

var request = (HttpWebRequest) HttpWebRequest.Create(requestUri); 
request.Headers.Add("x-ms-version", "2012-08-01"); request.Method = 
"POST"; request.ContentType = "application/xml"; 

To fix this error, add an explicit content length to your request before making the API call.

request.ContentLength = 0;

1

嘿,我用亂射,並讓服務器錯誤411 ,我在getHeaders方法中添加了以下lin E:

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

它解決了我的問題

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

ContentLength屬性包含發送與請求的Content-length HTTP標頭值。

-1之外的任何值都在ContentLength屬性中表示請求上載數據,並且只允許上載數據的方法在Method屬性中設置。所述ContentLength屬性被設置爲一個值

後,字節該號碼必須被寫入到由調用GetRequestStream方法或兩者BeginGetRequestStreamEndGetRequestStream方法返回的請求流。

更多詳情請點擊here

相關問題