日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

php httphelper,C#的HttpHelper类post ,get

發布時間:2025/3/15 C# 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php httphelper,C#的HttpHelper类post ,get 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

HeaderList = new Dictionary();

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(requestURL);

request.KeepAlive = true;

HttpWebResponse reponse = null;

Stream stream = null;

string strResponse = string.Empty;

try

{

//URI scheme(抽象標識符體系): 若為"https"則需加載證書并驗證

if (request.RequestUri.Scheme == "https")

{

#region 加載證書

//掛接驗證服務端證書的回調

ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(RemoteCertificateValidationCallback);

X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);

store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);

//獲取本地主機名稱作為證書查找的參數

string findValue = Dns.GetHostName();

X509Certificate2Collection _certsCollection = store.Certificates.Find(X509FindType.FindByIssuerName, findValue, false);

X509Certificate2 x509c = null;

// MessageBox.Show("證書個數: " + _certsCollection.Count);

//strResponse += "證書個數: " + _certsCollection.Count + "\n\n";

if (_certsCollection.Count > 0)

{

//MessageBox.Show("有證書");

x509c = _certsCollection[0];

request.ClientCertificates.Add(x509c);

}

else

{

//MessageBox.Show("沒有證書");

}

#endregion

}

request.Timeout = 30000;

request.UserAgent = DefaultUserAgent;

//request.Referer = "www.baidu.com";

request.Method = "GET";

request.Accept = "text/html, application/xhtml+xml, */*";

request.ContentType = "application/x-www-form-urlencoded";

request.AllowAutoRedirect = false;

request.Headers.GetValues("Location");

reponse = (HttpWebResponse)request.GetResponse();

//foreach (string HeaderKey in reponse.Headers)

//{

// strResponse += HeaderKey + ": " + reponse.Headers[HeaderKey] + "\n";

//}

//strResponse += "\n\n";

//MessageBox.Show("StatusCode = " + reponse.StatusCode);

//strResponse += "StatusCode = " + reponse.StatusCode + "\n\n";

if (reponse.StatusCode == HttpStatusCode.OK)

{

stream = reponse.GetResponseStream();

StreamReader myStreamReader = new StreamReader(stream, Encoding.UTF8);

strResponse += myStreamReader.ReadToEnd();

myStreamReader.Close();

stream.Close();

}

}

catch (WebException ex)

{

//MessageBox.Show("異常1----" + ex);

throw new WebException(ex.Message, ex.InnerException);

}

catch (Exception ex)

{

//MessageBox.Show("異常2----" + ex);

throw new Exception(ex.Message, ex.InnerException);

}

finally

{

if (stream != null) { stream.Close(); }

if (reponse != null) { reponse.Close(); reponse = null; }

if (request != null) { request = null; }

}

//如果未收到負載均衡系統返回報文響應,拋出異常信息。

if (string.IsNullOrEmpty(strResponse))

{

throw new Exception("警告:服務請求失敗,負載均衡系統無響應,請確認服務請求已正確配置!");

}

//返回信息

return strResponse;

}

public static bool RemoteCertificateValidationCallback(Object sender,

X509Certificate certificate,

X509Chain chain,

SslPolicyErrors sslPolicyErrors)

{

return true;

}

public static string post(string requestURL, IDictionaryparameters, CookieCollection cookies) {

string strResponse = string.Empty;

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(requestURL);

request.KeepAlive = true;

HttpWebResponse reponse = null;

Stream stream = null;

try

{

//URI scheme(抽象標識符體系): 若為"https"則需加載證書并驗證

if (request.RequestUri.Scheme == "https")

{

#region 加載證書

//掛接驗證服務端證書的回調

ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(RemoteCertificateValidationCallback);

X509Store store = new X509Store(StoreName.My, StoreLocation.CurrentUser);

store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);

//獲取本地主機名稱作為證書查找的參數

string findValue = Dns.GetHostName();

X509Certificate2Collection _certsCollection = store.Certificates.Find(X509FindType.FindByIssuerName, findValue, false);

X509Certificate2 x509c = null;

// MessageBox.Show("證書個數: " + _certsCollection.Count);

//strResponse += "證書個數: " + _certsCollection.Count + "\n\n";

if (_certsCollection.Count > 0)

{

//MessageBox.Show("有證書");

x509c = _certsCollection[0];

request.ClientCertificates.Add(x509c);

}

else

{

//MessageBox.Show("沒有證書");

}

#endregion

}

request.Timeout = 30000;

request.UserAgent = DefaultUserAgent;

//request.Referer = "www.baidu.com";

request.Method = "POST";

request.Accept = "text/html, application/xhtml+xml, */*";

request.ContentType = "application/x-www-form-urlencoded";

request.AllowAutoRedirect = false;

request.Headers.GetValues("Location");

if (cookies != null)

{

request.CookieContainer = new CookieContainer();

request.CookieContainer.Add(cookies);

}

//發送POST數據

if (!(parameters == null || parameters.Count == 0))

{

StringBuilder buffer = new StringBuilder();

int i = 0;

foreach (string key in parameters.Keys)

{

if (i > 0)

{

buffer.AppendFormat("&{0}={1}", key, parameters[key]);

}

else

{

buffer.AppendFormat("{0}={1}", key, parameters[key]);

i++;

}

}

byte[] data = Encoding.ASCII.GetBytes(buffer.ToString());

using (stream = request.GetRequestStream())

{

stream.Write(data, 0, data.Length);

}

}

string[] values = request.Headers.GetValues("Content-Type");

reponse = (HttpWebResponse)request.GetResponse();

if (reponse.StatusCode == HttpStatusCode.OK)

{

StreamReader myStreamReader = new StreamReader(stream, Encoding.UTF8);

strResponse += myStreamReader.ReadToEnd();

myStreamReader.Close();

stream.Close();

}

}

catch (WebException ex)

{

//MessageBox.Show("異常1----" + ex);

throw new WebException(ex.Message, ex.InnerException);

}

catch (Exception ex)

{

//MessageBox.Show("異常2----" + ex);

throw new Exception(ex.Message, ex.InnerException);

}

finally

{

if (stream != null) { stream.Close(); }

if (reponse != null) { reponse.Close(); reponse = null; }

if (request != null) { request = null; }

}

//如果未收到負載均衡系統返回報文響應,拋出異常信息。

if (string.IsNullOrEmpty(strResponse))

{

throw new Exception("警告:服務請求失敗,負載均衡系統無響應,請確認服務請求已正確配置!");

}

return strResponse;

}

}

}

總結

以上是生活随笔為你收集整理的php httphelper,C#的HttpHelper类post ,get的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。