0
一個朋友告訴我這個示例代碼,C#實現HTTP POST,做的winform應用程序計算出: http://www.terminally-incoherent.com/blog/2008/05/05/send-a-https-post-request-with-c/爲什麼我收到此錯誤:遠程服務器返回錯誤:(417)預期失敗
而在Metro應用中實現:
// this is what we are sending
string post_data = "[email protected]&pass=example123";
// this is where we will send it
string uri = "http://app.proceso.com.mx/win8/login";
// create a request
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
request.Method = "POST";
// turn our request string into a byte stream
byte[] postBytes = Encoding.UTF8.GetBytes(post_data);
// this is important - make sure you specify type this way
request.ContentType = "application/x-www-form-urlencoded";
Stream requestStream = await request.GetRequestStreamAsync();
// now send it
requestStream.Write(postBytes, 0, postBytes.Length);
// grab te response and print it out to the console along with the status code
WebResponse response = await request.GetResponseAsync();
//var a = new StreamReader(response.GetResponseStream()).ReadToEnd();
StreamReader requestReader = new StreamReader(response.GetResponseStream());
String webResponse = requestReader.ReadToEnd();
我意識到,HttpWebRequest的不含ProtocolVersion並拋出我這個錯誤在這行:
WebResponse response = await request.GetResponseAsync();
// ERROR: The remote server returned an error: (417) Expectation Failed.
我想最後一個屬性是解決方案。我怎麼解決這個問題? 在此先感謝
嘗試在頂部放置以下代碼:'ServicePointManager.Expect100Continue = false;'另外,爲什麼使用Async版本的方法,然後使用'await'進行阻塞,所有這些函數都有同步版本。 –