zoukankan      html  css  js  c++  java
  • windows 8 httpclient 联网方式

    基本技术点    自己记录用于复习

    开头顺便记下  解析 xml的 正则表达式

                    XElement element = XElement.Parse(s);
                    OutputList.ItemsSource =
                        (from c in element.Elements("item")
                         select c.Attribute("name").Value
                        );

    很简单  from  和select  语句 很方便

    windows 8 中 使用  httpclient  代替  windows  phone 中的   httpwebclient   , 使用方法 也有些不同 。

    下面是windows 8种使用  httpclient的各种方式 

    a  以get的方式 对服务器发起请求   获得string

        HttpResponseMessage response = await httpClient.GetAsync(resourceUri);    // await关键字    异步加载      *  resourceUri 是请求的Url

         string  statues_string  =   ((int)response.StatusCode) + " " + response.ReasonPhrase + Environment.NewLine;    //  服务器返回  请求的  状态

         responseBodyAsText = await response.Content.ReadAsStringAsync();             //将 请求返回的 柳 转化为  string    

    b  以get的方式  对服务器发去请求    接受返回来的流

           string resourceAddress =  http://www.baidu.com";

           HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, resourceAddress);                              

           HttpResponseMessage response = await httpClient.SendAsync(request,            HttpCompletionOption.ResponseHeadersRead);

           OutputField.Text = response.StatusCode + " " + response.ReasonPhrase + Environment.NewLine;

           StringBuilder responseBody = new StringBuilder();                

           using (Stream responseStream = await response.Content.ReadAsStreamAsync())

         {

                        byte[] responseBytes = new byte[responseStream.Length];
                       read = await responseStream.ReadAsync(responseBytes, 0, responseBytes.Length);

         }

     c    以post的方式  上传 string 

                    HttpResponseMessage response = await httpClient.PostAsync(resourceAddress,                     new StringContent(RequestBodyField.Text));

                         catch (HttpRequestException hre)             {                 rootPage.NotifyUser("Error", NotifyType.ErrorMessage);                 OutputField.Text = hre.ToString();             }         

                         catch (TaskCanceledException)             {                 rootPage.NotifyUser("Request canceled.", NotifyType.ErrorMessage);             }            

                         finally             {                 Helpers.ScenarioCompleted(StartButton, CancelButton);             }             //各种异常处理

    d   以post的方式 上传流数据

                byte[] subData = new byte[size];            

                 for (int i = 0; i < subData.Length; i++)           

                 {                 subData[i] = 64;              }

                Stream stream=(Stream)new MemoryStream(subData);

                StreamContent streamContent = new StreamContent(stream);

                 string resourceAddress =“http://www.baidu.com”;
                HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, resourceAddress);

                  request.Content = streamContent;
                 request.Headers.TransferEncodingChunked = true; // 假设我们不知道内容的长度

                 HttpResponseMessage response = await httpClient.SendAsync(request);       //发送数据  

               若消息返回来后  数据都在response中 

  • 相关阅读:
    java多线程基础(一)
    重构总体思路
    【Gearman学习笔记】分布式处理入门
    virtualbox安装提示出现严重错误解决办法
    驱动程序vmci.sys版本不正确。请尝试重新安装 VMware
    Gearman任务分布系统部署windows平台_使用Cygwin
    Fatal error: Class 'GearmanClient' not found解决方法
    header('Content-type:text/html;charset = utf-8');出现中文乱码
    heredoc和nowdoc的区别
    SELECT INTO 和 INSERT INTO SELECT 两种表复制语句
  • 原文地址:https://www.cnblogs.com/zey23/p/2762909.html
Copyright © 2011-2022 走看看