2013-08-21 11 views
33

इस तरह मैं नेट के साथ एक सेवा को कॉल:मुझे 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" का उपयोग करना पर्याप्त होना चाहिए।

मामले में आप पोस्ट प्रारूप के बारे में सोच रहे हैं, यहाँ आप क्या करना है क्या करना है:

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-plication-using-c-sharp"? मुझे एक ही समस्या है। मैं एसएफटीपी यूआरएल –

8

जब आप एक POST HttpWebRequest, आप डेटा आप भेज रहे हैं, कुछ की तरह की लंबाई का उल्लेख करना होगा बनाने:,

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

अगर आप किसी भी डेटा नहीं भेज रहे हैं, बस इसे 0 पर सेट आप का मतलब है कि

request.ContentLength = 0; 

आमतौर पर, अगर आप किसी भी डेटा नहीं भेज रहे, chosing GET विधि बजाय समझदार है, जैसा कि आप HTTP RFC

में देख सकते हैं: बस अपने कोड के लिए इस लाइन जोड़ने के लिए
+0

धन्यवाद एक लोट ... यह मेरे लिए उपयोगी –

+1

प्रथानुसार एक डेटा एक पोस्ट के साथ भेजे जाने के लिए उम्मीद करेंगे है, यह मिलता है जब आप कर रहे हैं का उपयोग करने के गलत और बुरे व्यवहार किया जाएगा होगा * सर्वर पर डेटा संशोधित *। एक POST अनुरोध क्लाइंट और सर्वर के बीच किसी भी प्रॉक्सी कैश से बचना चाहिए। अनुरोध के परिणामस्वरूप सर्वर पर कुछ भी बदलने के इरादे के बिना सर्वर से डेटा प्राप्त किया जा रहा है, तो ठीक है। – Michael

17

आप अपने अनुरोध हेडर में Content-Length: 0 जोड़ने की जरूरत है।

कैसे परीक्षण करने के लिए की एक बहुत वर्णनात्मक उदाहरण here

+0

हां हिट करने की कोशिश कर रहा हूं हां, सामग्री-लंबाई जोड़ना: 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 हो रही थी , मैं getLeaders विधि में निम्नलिखित लिन में जोड़ा गया ई:

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 संपत्ति में अन्य मान दर्शाता अनुरोध डेटा अपलोड और है कि केवल तरीकों कि डेटा अपलोड विधि संपत्ति में सेट किया जा करने की अनुमति है कि। ContentLength संपत्ति एक मान पर सेट है

के बाद, बाइट्स की संख्या अनुरोध धारा है कि GetRequestStream विधि या दोनों BeginGetRequestStream और EndGetRequestStream तरीकों को फोन करके दिया जाता है करने के लिए लिखा होना चाहिए।

अधिक जानकारी के लिए क्लिक करें here

संबंधित मुद्दे