Windows Phone 8-帖子Json'in request body'给出了一个例外 [英] Windows Phone 8 -Post Json 'in request body' give an exception
问题描述
Windows Phone 8 -帖子Json 在请求正文中" 给出了例外,但它在"Windows控制台应用程序" 上有效.为什么会这样呢?
Windows Phone 8 -Post Json 'in request body' give an exception but, it is working on 'windows Console app'. Why could it be?
例外: AsyncWaitHandle'(((System.Net.Browser.OHWRAsyncResult)asynchronousResult).AsyncWaitHandle'引发了 类型"System.NotSupportedException"的异常System.Threading.WaitHandle {System.NotSupportedException}
Exception: AsyncWaitHandle '((System.Net.Browser.OHWRAsyncResult)asynchronousResult).AsyncWaitHandle' threw an exception of type 'System.NotSupportedException' System.Threading.WaitHandle {System.NotSupportedException}
听到我的密码.
private void Button_Click_1(object sender, RoutedEventArgs e)
{
byte[] encodedPassword = System.Text.Encoding.UTF8.GetBytes("Key" + ":" + "Value");
string encodedAuto = System.Convert.ToBase64String(encodedPassword);
// Create a new HttpWebRequest object.
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://uri");
// Set the Method property to 'POST' to post data to the URI.
request.Method = "POST";
request.ContentType = "application/json; charset=utf-8";
request.Accept = "application/json";
request.Headers["Authorization"] = "Basic " + encodedAuto;
request.AllowAutoRedirect = true;
request.AllowWriteStreamBuffering = true;
// start the asynchronous operation
request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), request);
}
private static void GetRequestStreamCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
User user = new User();
user.password = password;
user.username = username;
// End the operation
Stream postStream = request.EndGetRequestStream(asynchronousResult)
/* String input = "{" +
"\"username\" : " + "\"" + username + "\"" + "," +
" \"password\" : " + "\"" + password + "\"" +
"}"; */
var input = JsonConvert.SerializeObject(user);
// Convert the string into a byte array.
byte[] byteArray = Encoding.UTF8.GetBytes(input);
// Write to the request stream.
postStream.Write(byteArray, 0, byteArray.Length);
postStream.Flush();
postStream.Close();
// Start the asynchronous operation to get the response
request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
}
private static void GetResponseCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
// End the operation
HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult);
Stream streamResponse = response.GetResponseStream();
StreamReader streamRead = new StreamReader(streamResponse);
string responseString = streamRead.ReadToEnd();
// Close the stream object
streamResponse.Close();
streamRead.Close();
// Release the HttpWebResponse
response.Close();
allDone.Set();
}
谢谢!
推荐答案
我发现了真实设备的问题.问题是'https'连接. Windows Phone模拟器为所有请求方法的'https'链接提供了例外.
I figured out the problem with a real device. The problem is the 'https' connections. Windows Phone Emulator gives exception with 'https' links for all request methods.
这篇关于Windows Phone 8-帖子Json'in request body'给出了一个例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!