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

使用情景如下:

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();  
	}
} 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值