阅读背景:

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

来源:互联网 

使用情景如下:

UI线程中
{  
	HttpWebRequest wr = 
		(HttpWebRequest)WebRequest.Create("https://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();  
	}
} UI线程中
{  
	HttpWebRequest wr = 
		



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: