zoukankan      html  css  js  c++  java
  • Windows Phone 十六、HttpClient

    HttpClient 对象也可以实现网络请求

    相对于 HttpWebRequest 对象来说,HttpClient 操作更简单,功能更强大

    HttpClient 提供一系列比较简单的API来实现基本的请求

    同时也支持身份验证和异步操作

    注意 Windows Runtime 平台中有两个 HttpClient 类型,调用方式几乎相同,以下内容使用 Windows.Web.Http 中的 HttpClient

    发送数据格式

    HttpFormUrlEncodedContent

    HttpMultipartContent

    HttpMultipartFormDataContent

    HttpBufferContent

    HttpStreamContent

    HttpStringContent

    设置 Cookie:client.DefaultRequestHeaders.Add("Cookie", "cookie_key1=CookieValue1; cookie_key2=CookieValue2;");

    1     <Grid>
    2         <TextBox x:Name="txtUrl"/>
    3         <Button Content="DOWN" Click="Button_Click"/>
    4     </Grid>
     1         protected async override void OnNavigatedTo(NavigationEventArgs e)
     2         {
     3             HttpClient client = new HttpClient();
     4             client.DefaultRequestHeaders.Authorization = new Windows.Web.Http.Headers.HttpCredentialsHeaderValue("Basic", "111:222");
     5             //GET请求
     6             var result = await client.GetStringAsync(new Uri("http://localhost:7080/index.ashx"));
     7 
     8             var dict = new Dictionary<string, string>();
     9             dict.Add("ke1", "val1");
    10             dict.Add("ke2", "val2");
    11             //await client.PostAsync(new Uri("http://localhost:7080/index.ashx"), new HttpFormUrlEncodedContent(dict));
    12             //await client.PostAsync(new Uri("http://localhost:7080/index.ashx"), new HttpStringContent("abc"));
    13 
    14             var file = await ApplicationData.Current.LocalCacheFolder.CreateFileAsync("1.txt", CreationCollisionOption.ReplaceExisting);
    15             await FileIO.AppendTextAsync(file, "abcdefghijklmnopqrstuvwxyz");
    16             var fileStream = await file.OpenAsync(FileAccessMode.Read);
    17             await client.PostAsync(new Uri("http://localhost:7080/index.ashx"), new HttpStreamContent(fileStream));
    18         }
    19 
    20         private async void Button_Click(object sender, RoutedEventArgs e)
    21         {
    22             //定义请求Uri
    23             var requestUri = new Uri(txtUrl.Text);
    24             //创建一个Http请求客户端HttpClient
    25             var client = new HttpClient();
    26             //创建定期监视对象
    27             IProgress<HttpProgress> progress = new Progress<HttpProgress>((p) =>
    28             {
    29                 //此处参数P,可以获取到进度相关信息
    30                 System.Diagnostics.Debug.WriteLine(p.BytesReceived + "/" + p.TotalBytesToReceive);
    31             });
    32             //在异步任务中加入进度监控
    33             HttpResponseMessage response = await client.GetAsync(requestUri).AsTask(progress);
    34         }
  • 相关阅读:
    python之isinstance和issubclass
    python中类的继承
    python中面向对象
    python中常用的内置模块
    Python常用模块
    python中的常用内置模块
    python中的包、模块及导入
    python中的内置函数(二)
    国内7大核心期刊
    PS学习列表
  • 原文地址:https://www.cnblogs.com/includeling/p/4599065.html
Copyright © 2011-2022 走看看