public class HttpHelper
{
public String ErrorReturn { get; private set; } = "HttpHelper access error!";
private CookieContainer Cookie = new CookieContainer();
public string HttpWebRequestPost(string url, string param)
{
CookieContainer cookie = new CookieContainer();
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
Encoding encoding = Encoding.UTF8;
byte[] byteArray = Encoding.ASCII.GetBytes(param);
string responseData = String.Empty;
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
using (var streamWriter = new StreamWriter(req.GetRequestStream()))
{
streamWriter.Write(param);
streamWriter.Flush();
streamWriter.Close();
}
using (HttpWebResponse response = (HttpWebResponse)req.GetResponse())
{
using (StreamReader reader = new StreamReader(response.GetResponseStream(), encoding))
{
responseData = reader.ReadToEnd();
}
return responseData;
}
}
public string HttpGet(string Url, string postDataStr)
{
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url + (postDataStr == "" ? "" : "?") + postDataStr);
request.Method = "GET";
request.ContentType = "text/html;charset=UTF-8";
request.CookieContainer = Cookie;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);
Stream myResponseStream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
string retString = myStreamReader.ReadToEnd();
myStreamReader.Close();
myResponseStream.Close();
return retString;
}
catch (Exception ex)
{
}
return ErrorReturn;
}
public bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
{
return true;
}
}