博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
记录HttpWebRequest辅助类
阅读量:5173 次
发布时间:2019-06-13

本文共 3695 字,大约阅读时间需要 12 分钟。

最近因为工作关系,需要通过HttpWebRequest去请求API,所有就写了个简单的辅助public sealed class HttpRequestHelper

{        private static string USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.124 Safari/537.36";        private static string ACCEPT = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";        private static string CONTENT_TYPE = "application/x-www-form-urlencoded; encoding=UTF-8"; ///         /// 单例        ///         private static volatile HttpRequestHelper instance;        private static object syncRoot = new object();        private HttpRequestHelper() { }        public static HttpRequestHelper Instance        {            get            {                if (instance == null)                {                    lock (syncRoot)                    {                        if (instance == null)                        {                            instance = new HttpRequestHelper();                        }                    }                }                return instance;            }        }        ///         /// Get请求        ///         /// url参数        /// 
json格式结果
public string Get(string URL) { string ret = string.Empty; URL = URL.Replace("ACCESS_TOKEN", WxTokenHelper.Instance.Token); var request = CreateRequest("GET", URL); var response = request.GetResponse(); Stream responseStream = response.GetResponseStream(); StreamReader readStream = new StreamReader(responseStream, Encoding.UTF8); ret = readStream.ReadToEnd(); readStream.Close(); return ret; } public string GetToken(string URL) { string ret = string.Empty; var request = CreateRequest("GET", URL); var response = request.GetResponse(); Stream responseStream = response.GetResponseStream(); StreamReader readStream = new StreamReader(responseStream, Encoding.UTF8); ret = readStream.ReadToEnd(); readStream.Close(); return ret; } /// /// Post请求 /// /// 请求的URL /// json格式参数 ///
json格式结果
public string Post(string URL, string jsonData) { string ret = string.Empty; URL = URL.Replace("ACCESS_TOKEN", WxTokenHelper.Instance.Token); var request = CreateRequest("POST", URL); if (!string.IsNullOrWhiteSpace(jsonData)) { byte[] data = Encoding.UTF8.GetBytes(jsonData); request.ContentLength = data.Length; using (var requestStream = request.GetRequestStream()) { requestStream.Write(data, 0, data.Length); }; var response = request.GetResponse() as HttpWebResponse; using (var responseStream = response.GetResponseStream()) { using (var readStream = new StreamReader(responseStream, Encoding.UTF8)) { ret = readStream.ReadToEnd(); } } } return ret; } private HttpWebRequest CreateRequest(string method, string URL) { HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL); request.Accept = ACCEPT; request.UserAgent = USER_AGENT; request.Method = method; request.ContentType = CONTENT_TYPE;      request.Proxy = null; return request; } }

 

转载于:https://www.cnblogs.com/mszhangxuefei/p/worknotes-11.html

你可能感兴趣的文章
北斗授时系统介绍及应用
查看>>
bzoj1009矩阵快速面+kmp
查看>>
isinstance
查看>>
CentOS克隆机器步骤,图文教程
查看>>
JwtTokenUtil
查看>>
得到缩写的类名
查看>>
Android--->ExpandableListView的基本使用
查看>>
Dragon Balls[HDU3635]
查看>>
[hihocoder][Offer收割]编程练习赛62
查看>>
IRM and the Object Model
查看>>
CString成员函数详解[转]
查看>>
实验一
查看>>
转:Javascript中document.execCommand()的用法
查看>>
django -过滤器的使用
查看>>
模拟摩天轮的效果
查看>>
Github 开源项目(一)websocketd (实战:实时监控服务器内存信息)
查看>>
ubuntu12.04安装Oracle11g,64位系统
查看>>
iOS 跳转到设置界面
查看>>
JAXB和XStream比较
查看>>
关于在MFC中将窗口最小化到托盘
查看>>