zoukankan      html  css  js  c++  java
  • HttpClient请求发送的几种用法二:

    public class HttpClientHelper
        {
            private static readonly HttpClientHelper _instance = new HttpClientHelper();
            private readonly HttpClient Client = new HttpClient();
            private HttpClientHelper()
            {
               // Client.BaseAddress =new Uri(Options.ServerUrl);
            }

            public static HttpClientHelper Instance
            {
                get { return _instance; }
            }

            public string Post(string processor, Dictionary<string, string> paras)
            {
                var req = new HttpRequestMessage();
                req.Headers.Clear();
                if (processor.Contains("smsGBK.aspx"))
                {
                    //req.Encoding = Encoding.GetEncoding("GBK");
                    req.Headers.Add("Content-Type", "application/x-www-form-urlencoded;charset=GB2312");
                }
                req.Headers.Add("ContentType", "application/x-www-form-urlencoded;charset=utf-8");
                req.Method = HttpMethod.Post;
                req.Content = new FormUrlEncodedContent(paras);
                req.RequestUri = new Uri(Options.ServerUrl+processor);
                var response = Client.SendAsync(req).Result;
                try
                {
                    response.EnsureSuccessStatusCode();
                }
                catch (Exception ex)
                {
                    Trace.TraceError("Error:{0}", ex.Message);
                    return "";
                }
                return response.Content.ReadAsStringAsync().Result;
            }
        }

  • 相关阅读:
    React Native-安卓环境的搭建
    python爬虫学习之日志记录模块
    Python爬虫学习之正则表达式爬取个人博客
    eclipse运行spark程序时日志颜色为黑色的解决办法
    python爬虫学习之爬取全国各省市县级城市邮政编码
    python 字典详细使用
    python爬虫学习之查询IP地址对应的归属地
    python jieba库的基本使用
    Eclipse环境搭建并且运行wordcount程序
    Hadoop2.0伪分布式平台环境搭建
  • 原文地址:https://www.cnblogs.com/rengke2002/p/6964085.html
Copyright © 2011-2022 走看看