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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: tp5index.php怎么放外面,tp
- 下一篇: php 数组 指针,php之数组指针详解