Http請求基本方法
阿新 • • 發佈:2019-01-28
file ima public != utf cookie post data sharp
1.Http請求基本方法
/// <summary> /// Http請求基本方法 /// </summary> /// <param name="content"></param> /// <param name="url"></param> /// <param name="method"></param> /// <param name="data"></param> /// <param name="referer"></param> /// <param name="contentTp"></param> /// <param name="headers"></param> /// <returns></returns> public bool Request(ref string content, string url, string method, byte[] data, string referer = "", string contentTp = "", NameValueCollection headers = null) { HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url); request.Method = method; request.Timeout = Timeout; request.AllowAutoRedirect = false; request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60"; request.Headers.Add("Accept-Language", "zh-TW,en;q=0.8,zh-CN;q=0.7,zh;q=0.5,zh-HK;q=0.3,en-US;q=0.2"); request.Headers.Add("Accept-Encoding", "gzip,deflate"); /// gzip 如何判斷並且解碼 測試是否可以暫時先註釋 https://www.crifan.com/set_accept_encoding_header_to_gzip_deflate_return_messy_code/ request.AutomaticDecompression = DecompressionMethods.GZip; request.Headers.Add("Cache-Control", "max-age=0"); request.KeepAlive = true; if (Proxy != null) { request.Proxy = Proxy; } if (!string.IsNullOrEmpty(referer)) { request.Referer = referer; } if (headers != null && headers.Count > 0) { foreach (string key in headers.AllKeys) { request.Headers.Add(key, headers[key]); } } if (cookieContainer == null) { cookieContainer = new CookieContainer(); request.CookieContainer = cookieContainer; } else { request.CookieContainer = cookieContainer; } if (method.ToLower() == "post") { request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"; request.Headers.Add("Upgrade-Insecure-Requests", "1"); request.ContentType = contentTp; if (data != null && data.Length > 0) { request.ContentLength = data.Length; using (Stream stream = request.GetRequestStream()) { stream.Write(data, 0, data.Length); } } } else { request.Accept = "text/html,application/xhtml+xm…plication/xml;q=0.9,*/*;q=0.8"; } ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; try { using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { if (headers != null && headers.Count > 0) { foreach (string key in headers.AllKeys) { response.Headers.Add(key, headers[key]); } } if (response.StatusCode == HttpStatusCode.OK || response.StatusCode == HttpStatusCode.RedirectMethod || response.StatusCode == HttpStatusCode.Found || response.StatusCode == HttpStatusCode.Moved || response.StatusCode == HttpStatusCode.Redirect) { if (response.StatusCode == HttpStatusCode.Found || response.StatusCode == HttpStatusCode.RedirectMethod || response.StatusCode == HttpStatusCode.Moved || response.StatusCode == HttpStatusCode.Redirect) { string redirectUrl = GetRedirectUrl(url, response.Headers["Location"]); return Request(ref content, redirectUrl, "GET", null, url, contentTp); } Stream stream = response.GetResponseStream(); StreamReader streamReader = new StreamReader(stream, defaultEncoding); content = streamReader.ReadToEnd(); streamReader.Close(); stream.Close(); response.Close(); return true; } else { return false; } } } catch (WebException e) { var res = (HttpWebResponse)e.Response; StreamReader sr = new StreamReader(res.GetResponseStream()); var strHtml = sr.ReadToEnd(); if (e.Response != null) { using (Stream respStream = e.Response.GetResponseStream()) { StreamReader streamReader = new StreamReader(respStream, Encoding.UTF8); content = streamReader.ReadToEnd(); streamReader.Close(); respStream.Close(); e.Response.Close(); } } return false; } catch (Exception e) { content = e.Message; return false; } }
獲取跳轉url:
/// <summary> /// 跳轉時,獲取跳轉URL /// </summary> /// <param name="url"></param> /// <param name="location"></param> /// <returns></returns> public static string GetRedirectUrl(string url, string location) { try { if (location.StartsWith("https://") || location.StartsWith("http://")) return location; string newUrl = url; if (location.StartsWith("/")) { newUrl = url.Substring(0, url.Substring(8).IndexOf("/") + 8); return newUrl + location; } else if (location.StartsWith("../")) { while (location.StartsWith("../")) { if (newUrl.IndexOf("/") > 0) { newUrl = newUrl.Substring(0, newUrl.LastIndexOf("/")); } location = location.Substring(3); } return newUrl + "/" + location; } else if (location.StartsWith("./")) { if (newUrl.IndexOf("/") > 0) { newUrl = newUrl.Substring(0, newUrl.LastIndexOf("/")); } location = location.Substring(2); return newUrl + "/" + location; } else { if (newUrl.IndexOf("/") > 0) { newUrl = newUrl.Substring(0, newUrl.LastIndexOf("/")); } return newUrl + "/" + location; } } catch { return url; } }
2.帶文件的form提交
/// <summary> /// 帶文件的Form提交 /// </summary> /// <param name="content"></param> /// <param name="url"></param> /// <param name="formData"></param> /// <param name="referer"></param> /// <returns></returns> public bool FileRequest(ref string content, string url, List<FormItem> formData, string referer, string lang = "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2") { try { string boundary = "---------------------------255132704032490"; //+ DateTime.Now.Ticks.ToString(); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.KeepAlive = true; request.CookieContainer = cookieContainer; request.ContentType = "multipart/form-data; boundary=" + boundary; request.Method = "POST"; request.Timeout = Timeout; request.AllowAutoRedirect = false; request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:58.0) Gecko/20100101 Firefox/58.0"; request.Headers.Add("Accept-Language", lang); request.Headers.Add("Accept-Encoding", "deflate, br"); request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"; request.Headers.Add("Upgrade-Insecure-Requests", "1"); if (!string.IsNullOrEmpty(referer)) request.Referer = referer; if (Proxy != null) { request.Proxy = Proxy; } ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; byte[] bdBytes = Encoding.UTF8.GetBytes("--" + boundary + "--\r\n"); byte[] brBytes = Encoding.UTF8.GetBytes("\r\n"); long length = bdBytes.Length; MemoryStream mStream = new MemoryStream(); foreach (var item in formData) { StringBuilder sb = new StringBuilder(); if (item.IsFile) { if (string.IsNullOrWhiteSpace(item.FilePath)) { sb.Append($"--{boundary}\r\n"); sb.Append($"Content-Disposition: form-data; name=\"{item.Name}\"; filename=\"{item.FileName}\"\r\n"); sb.Append($"Content-Type: {item.ContentTp}"); sb.Append("\r\n"); sb.Append("\r\n"); sb.Append("\r\n"); } else { if (string.IsNullOrWhiteSpace(item.FileName)) { sb.Append("--"); sb.Append(boundary); sb.Append("\r\n"); sb.Append("Content-Disposition: form-data; name=\""); sb.Append(item.Name); sb.Append("\"; filename=\""); sb.Append(System.IO.Path.GetFileName(item.FilePath)); sb.Append("\""); sb.Append("\r\n"); sb.Append("Content-Type: "); sb.Append(item.ContentTp); sb.Append("\r\n"); sb.Append("\r\n"); } else { sb.Append("--"); sb.Append(boundary); sb.Append("\r\n"); sb.Append("Content-Disposition: form-data; name=\""); sb.Append(item.Name); sb.Append("\"; filename=\""); sb.Append(item.FileName); sb.Append("\""); sb.Append("\r\n"); sb.Append("Content-Type: "); sb.Append(item.ContentTp); sb.Append("\r\n"); sb.Append("\r\n"); } } } else { sb.Append("--"); sb.Append(boundary); sb.Append("\r\n"); sb.Append("Content-Disposition: form-data; name=\""); sb.Append(item.Name); sb.Append("\""); sb.Append("\r\n"); sb.Append("\r\n"); sb.Append(item.Value); sb.Append("\r\n"); } string formTxt = sb.ToString(); byte[] data = Encoding.UTF8.GetBytes(formTxt); mStream.Write(data, 0, data.Length); length += data.Length; if (item.IsFile) { if (!string.IsNullOrWhiteSpace(item.FilePath)) { FileStream stream = new FileStream(item.FilePath, FileMode.Open, FileAccess.Read); length += stream.Length; byte[] buffer = new Byte[checked((uint)Math.Min(4096, (int)stream.Length))]; int nRead = 0; while ((nRead = stream.Read(buffer, 0, buffer.Length)) != 0) { mStream.Write(buffer, 0, nRead); } mStream.Write(brBytes, 0, brBytes.Length); length += brBytes.Length; } } } /// 循環結束,需要增加結束標記 長度之前已經默認 mStream.Write(bdBytes, 0, bdBytes.Length); mStream.Flush(); request.ContentLength = length; long n = mStream.Length; using (Stream stream = request.GetRequestStream()) { /// 回頭需要校驗代碼 byte[] data = mStream.GetBuffer(); stream.Write(data, 0, (int)mStream.Length); } HttpWebResponse res; try { using (HttpWebResponse response = (HttpWebResponse)request.GetResponse()) { response.Headers.Add("Accept-Language", lang); string redirectUrl = GetRedirectUrl(url, response.Headers["Location"]); /// 判斷是否跳轉 if (response.StatusCode == HttpStatusCode.Found || response.StatusCode == HttpStatusCode.RedirectMethod || response.StatusCode == HttpStatusCode.Moved || response.StatusCode == HttpStatusCode.Redirect) { var headers = new NameValueCollection(); headers.Add("Accept-Language", lang); return Get(ref content, redirectUrl, null, referer, headers); } else { Stream stream = response.GetResponseStream(); StreamReader streamReader = new StreamReader(stream, System.Text.Encoding.UTF8); content = streamReader.ReadToEnd(); streamReader.Close(); stream.Close(); } response.Close(); return true; } } catch (WebException ex) { res = (HttpWebResponse)ex.Response; StreamReader sr = new StreamReader(res.GetResponseStream()); var strHtml = sr.ReadToEnd(); //MainForm.Instance.Console("表單提交:" + e.Message); return false; } } catch { //MainForm.Instance.Console("表單提交:" + e.Message); return false; } }
3.HTTP POST方式請求數據(帶圖片)
/// <summary> /// HTTP POST方式請求數據(帶圖片) /// </summary> /// <param name="url">URL</param> /// <param name="param">POST的數據</param> /// <param name="fileByte">圖片Byte</param> /// <returns></returns> public string Post(string url, IDictionary<object, object> param, byte[] fileByte) { try { string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x"); byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n"); HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url); wr.ContentType = "multipart/form-data; boundary=" + boundary; wr.UserAgent = "RK_C# 1.2"; wr.Method = "POST"; //wr.Timeout = 150000; //wr.KeepAlive = true; //wr.Credentials = System.Net.CredentialCache.DefaultCredentials; Stream rs = null; try { rs = wr.GetRequestStream(); } catch { return "無法連接.請檢查網絡."; } string responseStr = null; string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}"; foreach (string key in param.Keys) { rs.Write(boundarybytes, 0, boundarybytes.Length); string formitem = string.Format(formdataTemplate, key, param[key]); byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem); rs.Write(formitembytes, 0, formitembytes.Length); } rs.Write(boundarybytes, 0, boundarybytes.Length); string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n"; string header = string.Format(headerTemplate, "image", "i.gif", "image/gif");//image/jpeg byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header); rs.Write(headerbytes, 0, headerbytes.Length); rs.Write(fileByte, 0, fileByte.Length); byte[] trailer = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--"); rs.Write(trailer, 0, trailer.Length); rs.Close(); WebResponse wresp = null; try { wresp = wr.GetResponse(); Stream stream2 = wresp.GetResponseStream(); StreamReader reader2 = new StreamReader(stream2); responseStr = reader2.ReadToEnd(); reader2.Close(); stream2.Close(); reader2.Dispose(); stream2.Dispose(); } catch { throw; } finally { if (wresp != null) { wresp.Close(); wresp = null; } wr.Abort(); wr = null; } return responseStr; } catch { //MainForm.Instance.Console("Ruokuai:" + e.Message); return ""; } }
Http請求基本方法