songjun
2024-09-04 cc908053e0b5075fbfd27350b6da4b39bca9e550
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Web;
 
namespace BatchService.Framework.Utility
{
    public class HttpHelper
    {
        public static string PostHttp(string url, string body)
        {
            return PostHttp(url, body, "application/x-www-form-urlencoded");
        }
 
        //body是要传递的参数,格式"roleId=1&uid=2"
        //post的cotentType填写:
        //"application/x-www-form-urlencoded"
        //soap填写:"text/xml; charset=utf-8"
        public static string PostHttp(string url, string body, string contentType, string token = "")
        {
            LogHelper.Debug(url);
            HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
            httpWebRequest.ContentType = contentType;
            httpWebRequest.Method = "POST";
            DateTime date1 = DateTime.Now;
            httpWebRequest.Timeout = 20000;
            byte[] btBodys = Encoding.UTF8.GetBytes(body);
            httpWebRequest.ContentLength = btBodys.Length;
 
            httpWebRequest.Headers.Add("Token", token);
 
            Stream newStream = httpWebRequest.GetRequestStream();
            newStream.Write(btBodys, 0, btBodys.Length);
            newStream.Close();
            HttpWebResponse httpWebResponse;
            DateTime date2 = DateTime.Now;
            try
            {
                httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            }
            catch (WebException ex)
            {
                LogHelper.Error(ex.ToString());
                httpWebResponse = (HttpWebResponse)ex.Response;
                StreamReader sr = new StreamReader(httpWebResponse.GetResponseStream());
                var strHtml = sr.ReadToEnd();
                LogHelper.Error(strHtml);
                throw ex;
            }
            StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream());
            string responseContent = streamReader.ReadToEnd();
 
            httpWebResponse.Close();
            streamReader.Close();
            httpWebRequest.Abort();
            httpWebResponse.Close();
 
            return responseContent;
        }
 
 
 
        public static string GetHttp(string url, HttpContext httpContext)
        {
            string queryString = "?";
 
            //foreach (string key in httpContext.Request.QueryString.AllKeys)
            //{
            //    queryString += key + "=" + httpContext.Request.QueryString[key] + "&";
            //}
 
            queryString = queryString.Substring(0, queryString.Length - 1);
 
            HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(url + queryString);
 
            //httpWebRequest.ContentType = "application/json";
            httpWebRequest.Method = "GET";
            httpWebRequest.Timeout = 20000;
            httpWebRequest.ContentType = "application/x-www-form-urlencoded;charset=utf-8";
            string responseContent = "";
            using (WebResponse wr = httpWebRequest.GetResponse())
            {
                Stream stream = wr.GetResponseStream();
                byte[] buf = new byte[1024];
                while (true)
                {
                    int len = stream.Read(buf, 0, buf.Length);
                    if (len <= 0)//[2011-8-22]  修改len < 0 =》 len <= 0 解决死循环
                        break;
                    responseContent += Encoding.UTF8.GetString(buf, 0, len);
                }
            }
            //HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            //StreamReader streamReader = new StreamReader(httpWebResponse.GetResponseStream());
            //string responseContent = streamReader.ReadToEnd();
            //httpWebResponse.Close(); 
            //streamReader.Close();
 
            return responseContent;
        }
        public static MemoryStream GetImage(string url)
        {
            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
            myRequest.Method = "GET";
            MemoryStream ms = null;
            HttpWebResponse myResponse = null;
            try
            {
                myResponse = (HttpWebResponse)myRequest.GetResponse();
                StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
                Stream stream = myResponse.GetResponseStream();
 
                #region 保存下载图片
 
 
                Byte[] buffer = new Byte[myResponse.ContentLength];
                int offset = 0, actuallyRead = 0;
                do
                {
                    actuallyRead = stream.Read(buffer, offset, buffer.Length - offset);
                    offset += actuallyRead;
                }
                while (actuallyRead > 0);
                ms = new MemoryStream(buffer);
                byte[] buffurPic = ms.ToArray();
                //System.IO.File.WriteAllBytes(pathName, buffurPic);
                #endregion
 
                myResponse.Close();
                stream.Close();
                //fileStream.Close();
            }
            //异常请求  
            catch (WebException ex)
            {
                LogHelper.Error(ex.ToString());
            }
            return ms;
        }
 
        public static string GetIPAddress()
        {
            if (HttpContext.Current == null)
            {
                return "";
            }
            string userHostAddress = HttpContext.Current.Request.UserHostAddress;
 
            if (string.IsNullOrEmpty(userHostAddress))
            {
                userHostAddress = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
            }
 
            //最后判断获取是否成功,并检查IP地址的格式(检查其格式非常重要)
            if (!string.IsNullOrEmpty(userHostAddress) && IsIP(userHostAddress))
            {
                return userHostAddress;
            }
            return "127.0.0.1";
        }
        /// <summary>
        /// 检查IP地址格式
        /// </summary>
        /// <param name="ip"></param>
        /// <returns></returns>
        public static bool IsIP(string ip)
        {
            return System.Text.RegularExpressions.Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
        }
    }
}