码迷,mamicode.com
首页 > Web开发 > 详细

HttpClientUtil

时间:2018-05-18 19:28:24      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:win   sync   .text   信息   request   async   web   ada   type   

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

namespace Qka.UsersApi.Test
{
    public static class HttpClientUtil
    {
       
        /// <summary>
        /// 获取要提交的信息
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="model"></param>
        /// <returns></returns>
        public static MultipartFormDataContent GetPostModelData<T>(T model)
        {
            var modelType = typeof(T);
            var formData = new MultipartFormDataContent();

            //遍历SendData的所有成员
            foreach (var item in modelType.GetProperties())
            {
                if (item.GetValue(model) == null)
                {
                    continue;

                }
                var content = new StringContent(((string)item.GetValue(model).ToString()));
                formData.Add(content, item.Name);

            }
            return formData;

        }
        public static string PostData<T>(this HttpClient client, string url, T model)
        {

            client.MaxResponseContentBufferSize = 256000;
            client.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Safari/537.36");

            var postModelData = HttpClientUtil.GetPostModelData<T>(model);
            var response = client.PostAsync(url, postModelData).Result;
            return response.Content.ReadAsStringAsync().Result;

        }
        public static string GetData(this HttpClient client, string url, Dictionary<string, string> formData=null)
        {

            client.MaxResponseContentBufferSize = 256000;
            client.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Safari/537.36");
            if (formData == null || formData.Count <= 0)
            {
                return client.GetAsync(url).Result.Content.ReadAsStringAsync().Result;
            }

            var paraData = formData.GetQueryString();
            var questionmark = "?" + paraData;
            
            
            var response = client.GetAsync(url + questionmark ).Result;
            var restr = response.Content.ReadAsStringAsync().Result;
            if(string.IsNullOrEmpty(restr))
            {
                var eeee = 11111;
            }
            return restr;

        }

        public static string PutData<T>(this HttpClient client, string url, T model)
        {

            client.MaxResponseContentBufferSize = 256000;
            client.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Safari/537.36");

            var postModelData = HttpClientUtil.GetPostModelData<T>(model);
            var response = client.PutAsync(url, postModelData).Result;
            return response.Content.ReadAsStringAsync().Result;

        }

        public static string DeleteData(this HttpClient client, string url, Dictionary<string, string> formData)
        {

            client.MaxResponseContentBufferSize = 256000;
            client.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Safari/537.36");
            if (formData == null || formData.Count <= 0)
            {
                return client.GetAsync(url).Result.Content.ReadAsStringAsync().Result;
            }

            var paraData = formData.GetQueryString();

            var response = client.DeleteAsync(url + "?" + paraData).Result;
            return response.Content.ReadAsStringAsync().Result;

        }
        /// <summary>
        /// 组装QueryString的方法
        /// 参数之间用&连接,首位没有符号,如:a=1&b=2&c=3
        /// </summary>
        /// <param name="formData"></param>
        /// <returns></returns>public static string GetQueryString(this Dictionary<string, string> formData)
        {
            if (formData == null || formData.Count == 0)
            {
                return "";
            }
            StringBuilder sb = new StringBuilder();

            var i = 0;
            foreach (var kv in formData)
            { i++;
                sb.AppendFormat("{0}={1}", kv.Key, kv.Value);
                if (i < formData.Count)
                {
                    sb.Append("&");
                }
            }
            return sb.ToString();
        }


    }
}

 

HttpClientUtil

标签:win   sync   .text   信息   request   async   web   ada   type   

原文地址:https://www.cnblogs.com/qiywtc/p/9057451.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!