1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Net; 6 using System.IO; 7 8 namespace LongFor.BLL 9 { 10 /// <summary> 11 /// Http操作类 12 /// </summary> 13 public static class HttpRequestUtil 14 { 15 private static Encoding DEFAULT_ENCODING = Encoding.GetEncoding("utf-8"); 16 private static string ACCEPT = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*"; 17 private static string CONTENT_TYPE = "application/x-www-form-urlencoded;charset=utf-8"; 18 private static string USERAGENT = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; InfoPath.2; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; msn OptimizedIE8;ZHCN)"; 19 // "application/x-www-form-urlencoded;charset=gb2312"; 20 /// <summary> 21 /// 获取网址HTML 22 /// </summary> 23 /// <param name="url">网址 </param> 24 /// <returns> </returns> 25 public static string GetHtmlContent(string url) 26 { 27 return GetHtmlContent(url, DEFAULT_ENCODING); 28 } 29 30 /// <summary> 31 /// 获取网址HTML 32 /// </summary> 33 /// <param name="url">网址 </param> 34 /// <returns> </returns> 35 public static string GetHtmlContent(string url, Encoding encoding) 36 { 37 string html = string.Empty; 38 HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); 39 request.UserAgent = USERAGENT; 40 request.Credentials = CredentialCache.DefaultCredentials; 41 WebResponse response = request.GetResponse(); 42 using (Stream stream = response.GetResponseStream()) 43 { 44 using (StreamReader reader = new StreamReader(stream, encoding)) 45 { 46 html = reader.ReadToEnd(); 47 reader.Close(); 48 } 49 } 50 return html; 51 } 52 53 /// <summary> 54 /// 获取网站cookie 55 /// </summary> 56 /// <param name="url">网址 </param> 57 /// <returns> </returns> 58 public static string GetCookie(string url) 59 { 60 string cookie = string.Empty; 61 WebRequest request = WebRequest.Create(url); 62 request.Credentials = CredentialCache.DefaultCredentials; 63 using (WebResponse response = request.GetResponse()) 64 { 65 cookie = response.Headers.Get("Set-Cookie"); 66 } 67 return cookie; 68 } 69 70 /// <summary> 71 /// Post模式浏览 72 /// </summary> 73 /// <param name="url">网址</param> 74 /// <param name="data">流</param> 75 /// <returns> </returns> 76 public static byte[] Post(string url, byte[] data) 77 { 78 return Post(null, url, data, null); 79 } 80 81 /// <summary> 82 /// Post模式浏览 83 /// </summary> 84 /// <param name="server">服务器地址 </param> 85 /// <param name="url">网址</param> 86 /// <param name="data">流</param> 87 /// <param name="cookieHeader">cookieHeader</param> 88 /// <returns> </returns> 89 public static byte[] Post(string server, string url, byte[] data, string cookieHeader) 90 { 91 if (data == null || data.Length == 0) 92 { 93 throw new ArgumentNullException("data"); 94 } 95 HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url); 96 if (!string.IsNullOrEmpty(cookieHeader)) 97 { 98 if (string.IsNullOrEmpty(server)) 99 { 100 throw new ArgumentNullException("server"); 101 } 102 CookieContainer co = new CookieContainer(); 103 co.SetCookies(new Uri(server), cookieHeader); 104 httpWebRequest.CookieContainer = co; 105 } 106 httpWebRequest.ContentType = CONTENT_TYPE; 107 httpWebRequest.Accept = ACCEPT; 108 httpWebRequest.Referer = server; 109 httpWebRequest.UserAgent = USERAGENT; 110 httpWebRequest.Method = "Post"; 111 httpWebRequest.ContentLength = data.Length; 112 using (Stream stream = httpWebRequest.GetRequestStream()) 113 { 114 stream.Write(data, 0, data.Length); 115 stream.Close(); 116 } 117 using (HttpWebResponse webResponse = (HttpWebResponse)httpWebRequest.GetResponse()) 118 { 119 using (Stream stream = webResponse.GetResponseStream()) 120 { 121 long contentLength = webResponse.ContentLength; 122 byte[] bytes = new byte[contentLength]; 123 bytes = ReadFully(stream); 124 stream.Close(); 125 return bytes; 126 } 127 } 128 } 129 130 private static byte[] ReadFully(Stream stream) 131 { 132 byte[] buffer = new byte[128]; 133 using (MemoryStream ms = new MemoryStream()) 134 { 135 while (true) 136 { 137 int read = stream.Read(buffer, 0, buffer.Length); 138 if (read <= 0) 139 return ms.ToArray(); 140 ms.Write(buffer, 0, read); 141 } 142 } 143 } 144 145 /// <summary> 146 /// Get模式浏览 147 /// </summary> 148 /// <param name="url">Get网址</param> 149 /// <returns> </returns> 150 public static byte[] Get(string url) 151 { 152 return Get(null, url, null); 153 } 154 155 /// <summary> 156 /// Get模式浏览 157 /// </summary> 158 /// <param name="url">Get网址</param> 159 /// <param name="cookieHeader">cookieHeader</param> 160 /// <param name="server">服务器地址 </param> 161 /// <returns> </returns> 162 public static byte[] Get(string server, string url, string cookieHeader) 163 { 164 HttpWebRequest httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url); 165 if (!string.IsNullOrEmpty(cookieHeader)) 166 { 167 if (string.IsNullOrEmpty(server)) 168 { 169 throw new ArgumentNullException("server"); 170 } 171 CookieContainer co = new CookieContainer(); 172 co.SetCookies(new Uri(server), cookieHeader); 173 httpWebRequest.CookieContainer = co; 174 } 175 httpWebRequest.Accept = "*/*"; 176 httpWebRequest.Referer = server; 177 httpWebRequest.UserAgent = USERAGENT; 178 httpWebRequest.Method = "GET"; 179 HttpWebResponse webResponse = (HttpWebResponse)httpWebRequest.GetResponse(); 180 using (Stream stream = webResponse.GetResponseStream()) 181 { 182 long contentLength = webResponse.ContentLength; 183 byte[] bytes = new byte[contentLength]; 184 bytes = ReadFully(stream); 185 stream.Close(); 186 return bytes; 187 } 188 } 189 } 190 }
调用方式:
PostData="queuename=roomsync&body={lengths:2,page:-1,datas:[67763C16-D5C7-4297-B73F-00009BA7D178,066F9157-1E7F-474C-9333-0000CBB2FB43]}";
upRoomUrl="http://192.168.33.166:8080/web/queue";
1 /// <summary> 2 /// 发送更新房间数据服务 3 /// </summary> 4 /// <param name="PostData">需要发送的数据</param> 5 /// <param name="type">数据来源类型</param> 6 public static void SendService(string PostData, string type) 7 { 8 string upRoomUrl = ConfigurationManager.AppSettings.GetValues("upRoomUrl")[0];//计算更新房间数据接口地址 9 if (upRoomUrl != "null") 10 { 11 LogWrape.LoggerInfo(logger, type + ",增量计算房间表数据:" + PostData, DateTime.Now.ToString()); 12 byte[] bt = System.Text.Encoding.UTF8.GetBytes(PostData); 13 string retMsg = System.Text.Encoding.UTF8.GetString(HttpRequestUtil.Post(upRoomUrl, bt)); 14 LogWrape.LoggerInfo(logger, type + ",增量计算房间表数据返回结果:" + retMsg, DateTime.Now.ToString()); 15 } 16 }