WindowsPhone8-PostJSON';在请求正文中';破例
Windows Phone 8-在请求正文中发布Json给出一个例外,但它正在处理“Windows控制台应用程序”。为什么会这样 例外情况: AsyncWaitHandle'((System.Net.Browser.OHWRAsyncResult)asynchronousResult)。AsyncWaitHandle'引发了 “System.NotSupportedException”System.Threading.WaitHandle{System.NotSupportedException}类型的异常 我的密码是hearWindowsPhone8-PostJSON';在请求正文中';破例,json,post,windows-phone-8,Json,Post,Windows Phone 8,Windows Phone 8-在请求正文中发布Json给出一个例外,但它正在处理“Windows控制台应用程序”。为什么会这样 例外情况: AsyncWaitHandle'((System.Net.Browser.OHWRAsyncResult)asynchronousResult)。AsyncWaitHandle'引发了 “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();
}
谢谢 NotSupportedException表示Windows Phone SDK未实现该功能。异常消息将为您提供有关具体未实现的内容的更多信息 在任何情况下,我的建议都是使用System.Net.Http库,这将代码减少到大约三行
我用一台真正的设备解决了这个问题。问题在于'https'连接Windows Phone Emulator对所有请求方法的“https”链接给出了例外情况。感谢您的建议。我试过了,但当我向HttpRequestMessage对象添加内容类型头时,出现了一些问题。添加(“授权”、“基本”+to)//ok request.Content.Headers.ContentType=new MediaTypeHeaderValue(“application/json”);//出了问题,出了什么问题。您是否收到异常,post是否失败?请求没有采用内容类型标题,因此当调试程序跨过该行(Request.content.Headers.ContentType=new MediaTypeHeaderValue(“application/json”);)时,请求毫无例外地失败。那么,我们如何在没有读取设备的情况下测试代码呢?