你的位置:首页 > 操作系统

[操作系统]Windows phone开发 网络编程之HttpWebRequest


HttpWebRequest和WebClient的区别
1,HttpWebRequest是个抽象类,所以无法new的,需要调用HttpWebRequest.Create();
2,其Method指定了请求类型,这里用的GET,还有POST;也可以指定ConentType;
3,其请求的Uri必须是绝对地址;
4,其请求是异步回调方式的,从BeginGetResponse开始,并通过AsyncCallback指定回调方法;
5,WebClient方式使用基于事件的异步编程模型,在HTTP响应返回时引发的WebClient回调是在UI线程中调用的,因此可用于更新UI元素的性。HttpWebRequest是基于后台进程运行的,回调不是UI线程,所以不能直接对UI进行操作,通常使用Dispatcher.BeginInvoke()跟界面进行通讯。


HTTP中GET请求与POST请求的区别:
1.GET请求是从服务器上获取数据                              POST请求是向服务器传送数据
2.GET请求在通过URL提交数据,数据在URL中可以看到         POST请求数据是通过写入数据流的方式提交
3.GET请求提交的数据最多只能1024个字节                     POST请求提交的数据没有限制

 

HttpWebRequest类实现GET请求

1.通过HttpWebRequest.CreateHttp()方法可以创建一个HttpWebRequest对象;

2.设置请求的参数(如设置请求方式为GET);

3.发起异步响应请求:BeginGetGetResponse;

4.结束异步响应EndGetResponse;

5.获取请求的内容;

6.对请求进行异常处理。

通过下面的例子增加对HttpWebRequest类的GET请求方式的理解:

 1 public void httpWebRequestGet() 2  {  3 try{ 4   String uri = "http://www.baidu.com/ ";        5   WebRequest request = HttpWebRequest.CreateHttp(new Uri(uri)); //创建WebRequest对象        6   request.Method = "GET";  //设置请求方式为GET 7   request.BeginGetResponse(responseStreamCallback, request); //发起GetResponse请求,返回应答请求异步操作的状态 8   } 9  catch(WebException e)10   {11   outputBlock.Text += "\n出现异常请求失败!\n";12   outputBlock.Text += "异常信息: ";13   outputBlock.Text += e.Message;14   outputBlock.Text += "\nStatus: ";15   outputBlock.Text += e.Status;16   }17  catch(Exception e)18   {19   outputBlock.Text += "\n出现异常请求失败!\n";20   outputBlock.Text += "\n异常信息: ";21   outputBlock.Text += e.Message;22   }23  }24 25 private void responseCallback(IAsyncResult result)26 {  27 try{      28   HttpWebRequest request = (HttpWebRequest)result.AsyncState;  //获取异步操作返回的的信息 29   WebResponse response = request.EndGetResponse(result);30   using (Stream stream = response.GetResponseStream()) //获取请求返回内容31   using(StreamReader read = new StreamReader(stream)) 32   {33    string text = read.ReadToEnd();34    Dispatcher.BeginInvoke(() =>    //使用Dispatcher.BeginInvoke()更新UI信息35     {36     textBlock1.Text = text;37      });38     }39 40    }41    //异常处理42  catch(WebException e)43  {44   outputBlock.Text += "\n出现异常请求失败!\n";45   outputBlock.Text += "异常信息: ";46   outputBlock.Text += e.Message;47   outputBlock.Text += "\nStatus: ";48   outputBlock.Text += e.Status;49  }50  catch(Exception e)51  {52   outputBlock.Text += "\n出现异常请求失败!\n";53   outputBlock.Text += "\n异常信息: ";54   outputBlock.Text += e.Message;55  }56 }

View Code

 

HttpWebRequest类实现POST请求

1.通过HttpWebRequest.CreateHttp()方法可以创建一个HttpWebRequest对象;

2.设置请求的参数(如设置请求方式为POST)

3.发起获取要发送的数据流请求:BeginGetRequestStreamGetResponseStream请求;

4.结束异步的数据流请求:EndGetRequestStream;

5.把要传递的数据写入数据流;

6.发起异步响应请求:BeginGetGetResponse;

7.结束异步响应EndGetResponse ;

8.获取请求的内容;

9.对请求进行异常处理

通过下面的例子增加对HttpWebRequest类的POST请求方式的理解:

 1 public void httpWebRequestPost() 2  {  3 try{ 4   String uri = "http://www.baidu.com/ ";        5   WebRequest request = HttpWebRequest.CreateHttp(new Uri(uri)); //创建WebRequest对象        6   request.Method = "POST";  //设置请求方式为POST 7   request.BeginGetRequestStream(responseStreamCallback, request); //发起GetResponseStream请求,返回应答请求异步操作的状态 8   } 9  catch(WebException e)10   {11   outputBlock.Text += "\n出现异常请求失败!\n";12   outputBlock.Text += "异常信息: ";13   outputBlock.Text += e.Message;14   outputBlock.Text += "\nStatus: ";15   outputBlock.Text += e.Status;16   }17  catch(Exception e)18   {19   outputBlock.Text += "\n出现异常请求失败!\n";20   outputBlock.Text += "\n异常信息: ";21   outputBlock.Text += e.Message;22   }23  }24 //发起获取发送数据流的请求的响应25 26 peivate void responseStreamCallback(IAsyncResult result)27 {28  HttpWebRequest request = (HttpWebRequest)result.AsyncState;  //获取异步操作返回的的信息  29  Stream stream = "hello world!";30  byte[] data = Encoding.UTF8.GetBytes(postString);31  stream.Write(data,0,data.Length);32  stream.Close();33  request.BeginGetResponse(responseCallback, request);34 }35 36 private void responseCallback(IAsyncResult result)37 { 38  try{      39     HttpWebRequest request = (HttpWebRequest)result.AsyncState;  //获取异步操作返回的的信息  40     WebResponse response = (WebResponse)request.EndGetResponse(result); 41     using (Stream stream = response.GetResponseStream()) 42     using ( StreamReader read = new StreamReader(stream)43     {44       string text = read.ReadToEnd();45       Dispatcher.BeginInvoke(() =>    //使用Dispatcher.BeginInvoke()更新UI信息46        {47        textBlock.Text = text;48        });49      }50 51     }52    //异常处理53  catch(WebException e)54  {55   outputBlock.Text += "\n出现异常请求失败!\n";56   outputBlock.Text += "异常信息: ";57   outputBlock.Text += e.Message;58   outputBlock.Text += "\nStatus: ";59   outputBlock.Text += e.Status;60  }61  catch(Exception e)62  {63   outputBlock.Text += "\n出现异常请求失败!\n";64   outputBlock.Text += "\n异常信息: ";65   outputBlock.Text += e.Message;66  }67 }

View Code

 

文章为作者原创,转载请标明出处,谢谢  http://www.cnblogs.com/spilledlight/p/4929418.html