c#后台直接发起http请求
来源:互联网 发布:e63诺基亚怎么下载软件 编辑:程序博客网 时间:2024/06/07 03:23
/// <summary> /// 操作方法 /// </summary> public class GetHttpHelper { private static readonly string DefaultUserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)"; /// <summary> /// Get方法获取Json数据 /// </summary> /// <param name="url"></param> /// <returns></returns> public static string GetHttpResponseJson(string url, IWebProxy webProxy) { string json = string.Empty; Encoding encoding = Encoding.UTF8; HttpWebResponse Response = CreateGetHttpResponse(new HttpGetParametersModel() { Url = url, WebProxy = webProxy }); json = GetStream(Response, encoding); return json; } /// <summary> /// 创建GET方式的HTTP请求 /// </summary> /// <param name="url">请求的URL</param> /// <param name="timeout">请求的超时时间</param> /// <param name="userAgent">请求的客户端浏览器信息,可以为空</param> /// <param name="cookies">随同HTTP请求发送的Cookie信息,如果不需要身份验证可以为空</param> /// <returns></returns> public static HttpWebResponse CreateGetHttpResponse(HttpGetParametersModel getParametersModel) { if (string.IsNullOrEmpty(getParametersModel.Url)) { throw new ArgumentNullException("getParametersModel.Url"); } HttpWebRequest request = WebRequest.Create(getParametersModel.Url) as HttpWebRequest; if (getParametersModel.WebProxy != null) { request.Proxy = getParametersModel.WebProxy; } request.Method = "GET"; request.UserAgent = DefaultUserAgent; if (!string.IsNullOrEmpty(getParametersModel.UserAgent)) { request.UserAgent = getParametersModel.UserAgent; } if (getParametersModel.Timeout.HasValue) { request.Timeout = getParametersModel.Timeout.Value; } if (getParametersModel.Cookies != null) { request.CookieContainer = new CookieContainer(); request.CookieContainer.Add(getParametersModel.Cookies); } return request.GetResponse() as HttpWebResponse; } /// <summary> /// 将response转换成文本 /// </summary> /// <param name="response"></param> /// <param name="encoding"></param> /// <returns></returns> private static string GetStream(HttpWebResponse response, Encoding encoding) { try { if (response.StatusCode == HttpStatusCode.OK) { switch (response.ContentEncoding.ToLower()) { case "gzip": { string result = Decompress(response.GetResponseStream(), encoding); response.Close(); return result; } default: { using (StreamReader sr = new StreamReader(response.GetResponseStream(), encoding)) { string result = sr.ReadToEnd(); sr.Close(); sr.Dispose(); response.Close(); return result; } } } } else { response.Close(); } } catch (Exception e) { throw e; } return ""; } private static string Decompress(Stream stream, Encoding encoding) { byte[] buffer = new byte[100]; //int length = 0; using (GZipStream gz = new GZipStream(stream, CompressionMode.Decompress)) { //GZipStream gzip = new GZipStream(res.GetResponseStream(), CompressionMode.Decompress); using (StreamReader reader = new StreamReader(gz, encoding)) { return reader.ReadToEnd(); } } } } #region GET/POST请求参数模型 /// <summary> /// GET请求参数模型 /// </summary> public class HttpGetParametersModel { /// <summary> /// 请求的URL(GET方式就附加参数) /// </summary> public string Url { get; set; } /// <summary> /// 超时时间 /// </summary> public int? Timeout { get; set; } /// <summary> ///浏览器代理类型 /// </summary> public string UserAgent { get; set; } /// <summary> /// Web请求代理 /// </summary> public IWebProxy WebProxy { get; set; } /// <summary> /// Cookies列表 /// </summary> public CookieCollection Cookies { get; set; } } /// <summary> /// POST请求参数模型 /// </summary> public class HttpPostParametersModel : HttpGetParametersModel { /// <summary> /// POST参数字典 /// </summary> public IDictionary<string, string> DictParameters { get; set; } /// <summary> /// 发送HTTP请求时所用的编码 /// </summary> public Encoding RequestEncoding { get; set; } } #endregion GET/POST请求参数模型
调用方式:
try { string url = " http://music.163.com/api/playlist/detail?id=58451795"; object obj= GetHttpResponseJson(url, null); } catch (Exception e) { }
2 0
- c#后台直接发起http请求
- C# 发起普通http请求
- spring boot RestTemplate 后台发起http请求
- java 发起HTTP请求
- Java发起Http请求
- 使用http发起请求
- nginx发起http请求
- Java发起http 请求
- 发起Http请求
- java 发起http请求
- C#后台发送Http请求
- C#后台发送HTTP请求
- C#的百度地图开发(一)发起HTTP请求
- C#发起一个请求
- java后台发起请求方式
- HttpClientUtil-用于后台发起请求
- iphone发起http请求代码
- 发起https和http请求
- <饮水入城> (神坑爆搜)
- 64-TCP/UDP 混合服务器
- 企业级云管理平台的架构实现与落地实践、趋势分析
- LCS相关问题
- 11款Linux下的数据恢复工具
- c#后台直接发起http请求
- struts2自定义拦截器
- Java并发编程实战
- UIButton 设置阴影效果
- 使用花生壳、路由器、个人电脑进行站点部署并实现外网域名访问图文详解(花生壳设置方法 使用方法 使用教程)
- sphinx 增量索引 实现近实时更新
- 学习笔记| AS入门(二) 简单控件篇(上)
- 线索二叉树(Threaded BinaryTree)
- 学习TP框架(一)