public static string HttpPost(string posturl, string postData, string contentType = "application/x-www-form-urlencoded")
{
Stream stream = null;
Stream stream2 = null;
StreamReader streamReader = null;
HttpWebResponse httpWebResponse = null;
HttpWebRequest httpWebRequest = null;
Encoding encoding = Encoding.GetEncoding("utf-8");
byte[] bytes = encoding.GetBytes(postData);
try
{
if (posturl.StartsWith("https", StringComparison.OrdinalIgnoreCase))
{
ServicePointManager.ServerCertificateValidationCallback = ((object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) => true);
httpWebRequest = (WebRequest.Create(posturl) as HttpWebRequest);
httpWebRequest.ProtocolVersion = HttpVersion.Version10;
}
else
{
httpWebRequest = (WebRequest.Create(posturl) as HttpWebRequest);
}
CookieContainer cookieContainer2 = httpWebRequest.CookieContainer = new CookieContainer();
httpWebRequest.AllowAutoRedirect = true;
httpWebRequest.Method = "POST";
httpWebRequest.ContentType = contentType;
httpWebRequest.ContentLength = bytes.Length;
stream = httpWebRequest.GetRequestStream();
stream.Write(bytes, 0, bytes.Length);
stream.Close();
httpWebResponse = (httpWebRequest.GetResponse() as HttpWebResponse);
stream2 = httpWebResponse.GetResponseStream();
streamReader = new StreamReader(stream2, encoding);
string result = streamReader.ReadToEnd();
string empty = string.Empty;
return result;
}
catch (Exception ex)
{
string message = ex.Message;
return string.Empty;
}
}
c# HttpPost
最新推荐文章于 2024-05-18 16:36:40 发布