HDL Home App 第二版本 旧平台金堂用 正在使用
黄学彪
2020-12-14 e90209beae6a4e822cecb18e6889f8bda23f630e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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
    }
}