您的位置:首页 > 理论基础 > 计算机网络

wp7使用POST请求Asp.net网页时,HttpWebRequest的BeginGetResponse方法抛出NotSupportException异常的解决方法

2011-11-10 08:37 1576 查看
使用情景如下:

UI线程中
{
HttpWebRequest wr =
(HttpWebRequest)WebRequest.Create("http://www.exzample.com/exzample.aspx");
wr.Method = "POST";
// 必须要指定ContentType,否则服务器有可能返回Page Not Found
wr.ContentType = "application/x-www-form-urlencoded";
wr.BeginGetRequestStream(new AsyncCallback(GetRequestStream_Completed), wr);
}

工作线程中
{
public void GetRequestStream_Completed(IAsyncResult ar)
{
HttpWebRequest wr = ar.AsyncState as HttpWebRequest;
Stream s = wr.EndGetRequestStream(ar);
s.Position = 0;
s.Write(new Byte[]{ 1, 2, 3 }, 0, 3); // 写入需要的字节流
// 如果不调用此函数的话,下面的BeginGetResponse将抛出NotSupportException异常,
// 但是在桌面版的Silvalight没有这个要求
s.Close();
wr.BeginGetResponse(new AsyncCallback(GetResponse_Completed), wr);
}

public void GetResponse_Completed(IAsyncResult ar)
{
HttpWebRequest wr = ar.AsyncState as HttpWebRequest;
Stream s = wr.EndGetResponse(ar);
s.Position = 0;
Byte[] buffer = new Byte[s.Length];
s.Read(buffer, 0, s.Length);
s.Close();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐