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

记录HttpWebRequest辅助类

时间:2014-10-13 21:40:47      阅读:238      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   io   os   ar   for   sp   

最近因为工作关系,需要通过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";
/// <summary> /// 单例 /// </summary> 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; } } /// <summary> /// Get请求 /// </summary> /// <param name="queryString">url参数</param> /// <returns>json格式结果</returns> 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; } /// <summary> /// Post请求 /// </summary> /// <param name="URL">请求的URL</param> /// <param name="jsonData">json格式参数</param> /// <returns>json格式结果</returns> 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; return request; } }

 

记录HttpWebRequest辅助类

标签:style   blog   http   color   io   os   ar   for   sp   

原文地址:http://www.cnblogs.com/mszhangxuefei/p/worknotes-11.html

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