• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#Post方式传输报文,和处理响应

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
 1   public string DoPost(string url, string data)
 2         {
 3             HttpWebRequest req = GetWebRequest(url, "POST");
 4             byte[] postData = Encoding.UTF8.GetBytes(data);
 5             Stream reqStream = req.GetRequestStream();
 6             reqStream.Write(postData, 0, postData.Length);
 7             reqStream.Close();
 8             HttpWebResponse rsp = (HttpWebResponse)req.GetResponse();
 9             Encoding encoding = Encoding.GetEncoding(rsp.CharacterSet);
10             return GetResponseAsString(rsp, encoding);
11         }
12 
13         public HttpWebRequest GetWebRequest(string url, string method)
14         {
15             HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
16             req.ServicePoint.Expect100Continue = false;
17             req.ContentType = "application/x-www-form-urlencoded;charset=utf-8";
18             req.ContentType = "text/json";
19             req.Method = method;
20             req.KeepAlive = true;
21             req.UserAgent = "mysoft";
22             req.Timeout = 1000000;
23             req.Proxy = null;
24             return req;
25         }
26 
27         public string GetResponseAsString(HttpWebResponse rsp, Encoding encoding)
28         {
29             StringBuilder result = new StringBuilder();
30             Stream stream = null;
31             StreamReader reader = null;
32             try
33             {
34                 // 以字符流的方式读取HTTP响应
35                 stream = rsp.GetResponseStream();
36                 reader = new StreamReader(stream, encoding);
37                 // 每次读取不大于256个字符,并写入字符串
38                 char[] buffer = new char[256];
39                 int readBytes = 0;
40                 while ((readBytes = reader.Read(buffer, 0, buffer.Length)) > 0)
41                 {
42                     result.Append(buffer, 0, readBytes);
43                 }
44             }
45             finally
46             {
47                 // 释放资源
48                 if (reader != null) reader.Close();
49                 if (stream != null) stream.Close();
50                 if (rsp != null) rsp.Close();
51             }
52 
53             return result.ToString();
54         }

 

 

一个帮助类

result = _httpHelper.Url(url).Content(msg).Post();

using System.Net.Http;
using System.Net.Http.Headers;

namespace Common
{
    public  class HttpHelper
   {
        private readonly HttpClient _httpClient;
        private string _url;
        private string _content;
 
       public HttpHelper()
       {
           _httpClient = new HttpClient();
           _httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        }
        public HttpHelper Url(string url)
       {
           _url = url;
           return this;
       }
        public HttpHelper Content(string content)
        {
            _content = content;
            return this;
        }
        public HttpHelper HeaderKeyValue(string key,string value)
        {
            if (_httpClient.DefaultRequestHeaders.Contains(key))
                _httpClient.DefaultRequestHeaders.Remove(key);
            _httpClient.DefaultRequestHeaders.Add(key, value);
            return this;
        }
        public string Post()
        {
            var content = new StringContent(_content);
            var response = _httpClient.PostAsync(_url,content).Result;
            return response.IsSuccessStatusCode ? response.Content.ReadAsStringAsync().Result : null;
        }
        public string Get()
        {
            var response = _httpClient.GetAsync(_url).Result;
            return response.IsSuccessStatusCode ? response.Content.ReadAsStringAsync().Result : null;
        }

    }
}

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
[导入]C#,webBrowser,登录,数据填充,模拟点击链接,运行JS函数发布时间:2022-07-13
下一篇:
C#TextBoxCtrl+A全选发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap