using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
namespace Shared.Phone.UserCenter
{
///
/// Http访问逻辑
///
public class HdlHttpLogic
{
#region ■ 变量声明___________________________
///
/// Http访问逻辑
///
private static HdlHttpLogic m_Current = null;
///
/// Http访问逻辑
///
public static HdlHttpLogic Current
{
get
{
if (m_Current == null)
{
m_Current = new HdlHttpLogic();
}
return m_Current;
}
}
#endregion
#region ■ 第三方接口访问_____________________
///
/// 第三方接口访问
///
/// 请求Url的完成路径
/// text/html 或者 application/json
/// POST 或者 GET 等等
/// 超时,默认5秒
/// 得到响应的数据
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
}
}