HttpClient测试

HttpClient测试

using System.Collections.Generic;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Windows;

namespace WpfHttpClient
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private readonly IHttpClientFactory _httpClientFactory;
        HttpClient client;
        public MainWindow(IHttpClientFactory httpClientFactory)
        {
            InitializeComponent();

            _httpClientFactory = httpClientFactory;

            client = _httpClientFactory.CreateClient();
        }

        private async void ButtonGET_Click(object sender, RoutedEventArgs e)
        {
            //发送异步的Get请求
            string html_get_content = await client.GetStringAsync("http://httpbin.org/get");
            ShowInfo(html_get_content);

        }

        private async void ButtonPost1_Click(object sender, RoutedEventArgs e)
        {
            MultipartFormDataContent content = new MultipartFormDataContent();
            content.Headers.Add("UserName", "admin");
            content.Headers.Add("Password", "123");
            //先读取文件
            using (Stream stream = File.OpenRead(@"oldMan.jpg"))
            {
                // 放入流中 上传接口协议名file  文件名
                content.Add(new StreamContent(stream), "oldMan", "oldMan.jpg");
                var respMsg = await client.PostAsync("http://httpbin.org/post", content);
                string html_post_content = await respMsg.Content.ReadAsStringAsync();
                ShowInfo(html_post_content);
            }
        }


        private async void ButtonPost2_Click(object sender, RoutedEventArgs e)
        {

            string json = "{userName:'admin',password:'123'}";
            StringContent content = new StringContent(json);
            //contentype 设置协议类型,必不可少
            content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
            var respMsg = await client.PostAsync("http://httpbin.org/post", content);
            string html_post_content = await respMsg.Content.ReadAsStringAsync();
            ShowInfo(html_post_content);

        }
        private async void ButtonPost3_Click(object sender, RoutedEventArgs e)
        {//POST表单格式
            Dictionary<string, string> nameValues = new Dictionary<string, string>();
            nameValues["userName"] = "admin";
            nameValues["password"] = "8888888";
            FormUrlEncodedContent content = new FormUrlEncodedContent(nameValues);
            //contentype 设置协议类型,必不可少
            content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");
            var respMsg = await client.PostAsync("http://httpbin.org/post", content);
            string html_post_content = await respMsg.Content.ReadAsStringAsync();

            ShowInfo(html_post_content);
        }

        private void ShowInfo(string msg)
        {
            this.txtInfo.Text = msg;
        }

    }
}

/*回显请求中用于以下任何类型的数据
http://httpbin.org/ip返回原始IP。
http://httpbin.org/user-agent返回用户代理。
http://httpbin.org/headers返回标头字典。
http://httpbin.org/get返回GET数据。
http://httpbin.org/post返回POST数据。
http://httpbin.org/put返回PUT数据。
http://httpbin.org/delete返回DELETE数据
http://httpbin.org/gzip返回gzip编码的数据。
http://httpbin.org/status/:code返回给定的HTTP状态代码。
http://httpbin.org/response-headers?key=val返回给定的响应头。
http://httpbin.org/redirect/:n 302重定向n次。
http://httpbin.org/relative-redirect/:n 302相对重定向n次。
http://httpbin.org/cookies返回cookie数据。
http://httpbin.org/cookies/set/:name/:value设置一个简单的cookie。
http://httpbin.org/basic-auth/:user/:passwd挑战HTTP基本认证。
http://httpbin.org/hidden-basic-auth/:user/:passwd 404的BasicAuth。
http://httpbin.org/digest-auth/:qop/:user/:passwd挑战HTTP摘要验证。
http://httpbin.org/stream/:n流n-100行。
http://httpbin.org/delay/:n延迟响应n-10秒。*/

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值