public class HttpHelper { public static string httpGet(string url) { HttpWebRequest request = null; request = WebRequest.Create(url) as HttpWebRequest; request.Method = "GET"; HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream myResponseStream = response.GetResponseStream(); StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8")); string retString = myStreamReader.ReadToEnd(); myStreamReader.Close(); myStreamReader = null; myResponseStream.Close(); myResponseStream.Dispose(); myResponseStream = null; request.Abort(); request = null; return retString; } private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) { return true; //總是接受 } /// <summary> /// Post提交 /// </summary> /// <param name="Url"></param> /// <param name="postDataStr"></param> /// <returns></returns> public static string httpPost(string Url, string postDataStr) { return httpPost(Url, postDataStr, "txt", "", "utf-8"); } public static string httpPost(string Url) { return httpPost(Url, "", "txt", "", "utf-8"); } public static string httpPost1(string Url) { return httpPost(Url, "", "html", "", "utf-8"); } public static string httpPost2(string Url, string postDataStr) { return httpPost(Url, postDataStr, "html", "", "utf-8"); } public static string httpPost3(string Url, string postDataStr) { return httpPost(Url, postDataStr, "textjson", "", "utf-8"); } public static string httpPostByJson(string Url, string postDataStr) { return httpPost(Url, postDataStr, "json", "", "utf-8"); } public static string httpPost(string Url, string postDataStr, string postType, string cacert, string chartset) //post讀取 { //發送 System.GC.Collect();//系統回收垃圾 if (Url.Contains("https://")) { ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult); } HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url); request.Timeout = 1000 * 20; request.Method = "POST"; //以下加速請求 //request.KeepAlive = false;//請求完關閉 //request.ServicePoint.Expect100Continue = false; //request.ServicePoint.UseNagleAlgorithm = false; //request.ServicePoint.ConnectionLimit = 65500; //request.AllowWriteStreamBuffering = false; request.Proxy = null; request.AllowAutoRedirect = true; //以上加速請求 if (postType == "txt") { request.ContentType = "application/x-www-form-urlencoded"; } else if (postType == "json") { request.ContentType = "application/json"; } else if(postType == "html") { request.ContentType = "text/html"; } else if (postType == "textjson") { request.ContentType = "text/json"; } if (cacert != "") { X509Certificate cert = new System.Security.Cryptography.X509Certificates.X509Certificate(cacert, ""); request.ImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;//設定驗證回調(總是同意) request.ClientCertificates.Add(cert);//把證書添加進http請求中 } try { byte[] payload = System.Text.Encoding.UTF8.GetBytes(postDataStr); request.ContentLength = payload.Length; request.ServicePoint.Expect100Continue = false; request.GetRequestStream().Write(payload, 0, payload.Length); //響應接收 //HttpWebResponse response = (HttpWebResponse)request.GetResponse(); HttpWebResponse response; try { response = (HttpWebResponse)request.GetResponse(); } catch (WebException ex) { response = (HttpWebResponse)ex.Response; } Stream myResponseStream = response.GetResponseStream(); StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding(chartset)); string retString = myStreamReader.ReadToEnd(); response.Close(); myStreamReader.Close(); myResponseStream.Close(); myResponseStream.Dispose(); response = null; myStreamReader = null; myResponseStream = null; request.Abort(); request = null; return retString; } catch (Exception ex) { request.Abort(); request = null; Console.WriteLine(ex.Message); //Message ms = new Message("網絡超時!"); return ""; } } /// <summary> /// Post方式請求接口 /// </summary> /// <param name="action">請求的方法名</param> /// <param name="dic">請求發送的數據</param> /// <returns></returns> public static string HttpPost(string action, Dictionary<string, string> dic) { //此處換為自己的請求url string url = action; string result = string.Empty; HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); req.Method = "POST"; req.ContentType = "application/x-www-form-urlencoded"; #region 添加Post 參數 StringBuilder builder = new StringBuilder(); int i = 0; foreach (var item in dic) { if (i > 0) builder.Append("&"); builder.AppendFormat("{0}={1}", item.Key, item.Value); i++; } byte[] data = Encoding.UTF8.GetBytes(builder.ToString()); req.ContentLength = data.Length; using (Stream reqStream = req.GetRequestStream()) { reqStream.Write(data, 0, data.Length); reqStream.Close(); } #endregion HttpWebResponse resp = (HttpWebResponse)req.GetResponse(); Stream stream = resp.GetResponseStream(); //獲取響應內容 using (StreamReader reader = new StreamReader(stream, Encoding.UTF8)) { result = reader.ReadToEnd(); } return result; } public static string PostDataNew(string url, string infor) { string result = ""; try { HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; request.Method = "POST"; request.KeepAlive = true; request.AllowAutoRedirect = false; request.ContentType = "application/x-www-form-urlencoded"; byte[] postdatabtyes = Encoding.UTF8.GetBytes(infor); request.ContentLength = postdatabtyes.Length; request.ServicePoint.Expect100Continue = false;//這個在Post的時候,一定要加上,如果服務器返回錯誤,他還會繼續再去請求,不會使用之前的錯誤數據,做返回數據 Stream requeststream = request.GetRequestStream(); requeststream.Write(postdatabtyes, 0, postdatabtyes.Length); requeststream.Close(); using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) { StreamReader sr2 = new StreamReader(response.GetResponseStream(), Encoding.UTF8); string respsr = sr2.ReadToEnd(); result = respsr; } } catch (Exception ex) { result = ex.Message; } return result; } public static string RequestData(string POSTURL, string PostData) { //發送請求的數據 WebRequest myHttpWebRequest = WebRequest.Create(POSTURL); myHttpWebRequest.Method = "POST"; UTF8Encoding encoding = new UTF8Encoding(); byte[] byte1 = encoding.GetBytes(PostData); myHttpWebRequest.ContentType = "application/x-www-form-urlencoded"; myHttpWebRequest.ContentLength = byte1.Length; Stream newStream = myHttpWebRequest.GetRequestStream(); newStream.Write(byte1, 0, byte1.Length); newStream.Close(); //發送成功后接收返回的XML信息 HttpWebResponse response = (HttpWebResponse)myHttpWebRequest.GetResponse(); string lcHtml = string.Empty; Encoding enc = Encoding.GetEncoding("UTF-8"); Stream stream = response.GetResponseStream(); StreamReader streamReader = new StreamReader(stream, enc); lcHtml = streamReader.ReadToEnd(); return lcHtml; } }