當前位置:成語大全網 - 新華字典 - c#winform使用post方法定點傳送用戶名和密碼到指定url網頁中的

c#winform使用post方法定點傳送用戶名和密碼到指定url網頁中的

/// <summary>

/// 創建POST方式的HTTP請求

/// </summary>

/// <param name="url">請求的URL</param>

/// <param name="parameters">隨同請求POST的參數名稱及參數值字典</param>

/// <param name="timeout">請求的超時時間</param>

/// <param name="userAgent">請求的客戶端瀏覽器信息,可以為空</param>

/// <param name="requestEncoding">發送HTTP請求時所用的編碼</param>

/// <param name="cookies">隨同HTTP請求發送的Cookie信息,如果不需要身份驗證可以為空</param>

/// <returns></returns>

public static HttpWebResponse CreatePostHttpResponse(string url, IDictionary<string, string> parameters, int? timeout, string userAgent, Encoding requestEncoding, CookieContainer cookies)

{

if (string.IsNullOrEmpty(url))

{

throw new ArgumentNullException("url");

}

if (requestEncoding == null)

{

throw new ArgumentNullException("requestEncoding");

}

HttpWebRequest request = null;

//如果是發送HTTPS請求

if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))

{

ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);

request = WebRequest.Create(url) as HttpWebRequest;

request.ProtocolVersion = HttpVersion.Version10;

}

else

{

request = WebRequest.Create(url) as HttpWebRequest;

}

request.Method = "POST";

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

if (!string.IsNullOrEmpty(userAgent))

{

request.UserAgent = userAgent;

}

else

{

request.UserAgent = DefaultUserAgent;

}

if (timeout.HasValue)

{

request.Timeout = timeout.Value;

}

if (cookies != null)

{

request.CookieContainer = cookies;

//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 = requestEncoding.GetBytes(buffer.ToString());

using (Stream stream = request.GetRequestStream())

{

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

}

}

return request.GetResponse() as HttpWebResponse;

}

private static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)

{

return true; //總是接受

}

}