New file |
| | |
| | | using System; |
| | | using System.Collections.Generic; |
| | | using System.Net; |
| | | using System.Text; |
| | | |
| | | namespace Shared.Phone.UserCenter |
| | | { |
| | | /// <summary> |
| | | /// Http访问逻辑 |
| | | /// </summary> |
| | | public class HdlHttpLogic |
| | | { |
| | | #region ■ 变量声明___________________________ |
| | | |
| | | /// <summary> |
| | | /// Http访问逻辑 |
| | | /// </summary> |
| | | private static HdlHttpLogic m_Current = null; |
| | | /// <summary> |
| | | /// Http访问逻辑 |
| | | /// </summary> |
| | | public static HdlHttpLogic Current |
| | | { |
| | | get |
| | | { |
| | | if (m_Current == null) |
| | | { |
| | | m_Current = new HdlHttpLogic(); |
| | | } |
| | | return m_Current; |
| | | } |
| | | } |
| | | |
| | | #endregion |
| | | |
| | | #region ■ 第三方接口访问_____________________ |
| | | |
| | | /// <summary> |
| | | /// 第三方接口访问 |
| | | /// </summary> |
| | | /// <param name="requestFullUrl">请求Url的完成路径</param> |
| | | /// <param name="contentType">text/html 或者 application/json</param> |
| | | /// <param name="requestMethod">POST 或者 GET 等等</param> |
| | | /// <param name="timeout">超时,默认5秒</param> |
| | | /// <returns>得到响应的数据</returns> |
| | | public byte[] RequestThridPartyHttps(string requestFullUrl, string contentType = "text/html", string requestMethod = "GET", int timeout = 5) |
| | | { |
| | | try |
| | | { |
| | | //初始化新的webRequst |
| | | //1. 创建httpWebRequest对象 |
| | | var webRequest = (HttpWebRequest)WebRequest.Create(new Uri(requestFullUrl)); |
| | | |
| | | //2. 初始化HttpWebRequest对象 |
| | | webRequest.Method = requestMethod; |
| | | webRequest.Timeout = timeout * 1000; |
| | | //取消使用代理访问 |
| | | webRequest.Proxy = null; |
| | | webRequest.UseDefaultCredentials = false; |
| | | webRequest.ContentType = contentType; |
| | | |
| | | //4. 读取服务器的返回信息 |
| | | var response = (HttpWebResponse)webRequest.GetResponse(); |
| | | using (var stream = response.GetResponseStream()) |
| | | { |
| | | if (stream == null) |
| | | { |
| | | return null; |
| | | } |
| | | var ms = new System.IO.MemoryStream(); |
| | | var bytes = new byte[1024]; |
| | | var len = int.MaxValue; |
| | | while (stream.CanRead && 0 < len) |
| | | { |
| | | len = stream.Read(bytes, 0, bytes.Length); |
| | | ms.Write(bytes, 0, len); |
| | | } |
| | | return ms.ToArray(); |
| | | } |
| | | } |
| | | catch |
| | | { |
| | | return null; |
| | | } |
| | | } |
| | | |
| | | #endregion |
| | | } |
| | | } |