using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Threading;
using System.Drawing.Imaging;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
namespace HXX.Scanner.Biz
{
///
/// http工具
///
public partial class http_helper
{
///
/// 基工具
///
private static http_helper Net = new http_helper();
///
/// 基cookie
///
private CookieContainer cookies = new CookieContainer();
///
/// post
///
///
///
///
public static string Post(string url, string content)
{
return Net.HTTP_POST(url, content, "", "");
}
///
/// post
///
///
///
///
///
///
public static string Post(string url, string content, string headTitle, string headContent)
{
return Net.HTTP_POST(url, content, headTitle, headContent);
}
///
/// get
///
///
///
public static string Get(string url)
{
return Net.HTTP_GET(url, 100, "", "");
}
///
/// get
///
///
///
///
///
public static string Get(string url, string headTitle, string headContent)
{
return Net.HTTP_GET(url, 100, headTitle, headContent);
}
///
/// get
///
///
///
///
public static string Get(string url, int timeout)
{
return Net.HTTP_GET(url, timeout, "", "");
}
///
/// delete
///
///
///
public static string Delete(string url)
{
return Net.HTTPSDelete(url);
}
///
/// post
///
///
///
///
///
///
public string HTTP_POST(string url, string Content, string headTitle, string headContent)
{
try
{
//ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
//ServicePointManager.ServerCertificateValidationCallback =
// new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);
byte[] bContent = Encoding.UTF8.GetBytes(Content);
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "POST";
req.ContentLength = bContent.Length;
string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
//req.ContentType = "text/xml";
req.ContentType = "application/json;charset=utf-8";
//req.ContentType = "application/x-www-form-urlencoded";
//req.ContentType = "multipart/form-data; boundary=" + boundary;
//req.CookieContainer = cookies;
//req.ProtocolVersion = HttpVersion.Version10;
//var nn = req.ProtocolVersion;
if (headTitle.Length > 0)
{
req.Headers.Add(headTitle, headContent);
}
string xx = cookies.GetCookieHeader(new Uri(url));
using (Stream st = req.GetRequestStream())
{
st.Write(bContent.ToArray(), 0, bContent.Length);
}
HttpWebResponse myResponse = (HttpWebResponse)req.GetResponse();
Stream resSt = myResponse.GetResponseStream();
StreamReader sr = new StreamReader(resSt, Encoding.UTF8);
string xxx = sr.ReadToEnd();
sr.Close();
return xxx;
}
catch (Exception ee)
{
string msg = ee.Message + Environment.NewLine + Environment.NewLine;
msg += "URL:" + url + Environment.NewLine + "Method:POST" + Environment.NewLine +
"Content:" + Content + Environment.NewLine;
System.Net.WebException webEE = ee as System.Net.WebException;
if (webEE != null && webEE.Response != null)
{
using (System.IO.Stream st = webEE.Response.GetResponseStream())
{
using (System.IO.StreamReader sr = new System.IO.StreamReader(st))
{
string webmsg = sr.ReadToEnd();
msg += Environment.NewLine + Environment.NewLine + webmsg;
}
}
}
throw new Exception(msg);
}
}
///
/// https
///
///
///
///
///
///
public bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
{
return true;
}
///
/// get
///
///
///
///
///
///
public string HTTP_GET(string url, int timeout, string headTitle, string headContent)
{
//ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
//ServicePointManager.ServerCertificateValidationCallback =
// new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "GET";
req.ContentLength = 0;
//req.ContentType = "application/json";
req.ContentType = "application/x-www-form-urlencoded";
req.CookieContainer = cookies;
if (headTitle.Length > 0)
{
req.Headers.Add(headTitle, headContent);
}
req.Timeout = timeout * 1000;
HttpWebResponse myResponse = (HttpWebResponse)req.GetResponse();
Stream resSt = myResponse.GetResponseStream();
StreamReader sr = new StreamReader(resSt);
string xxx = sr.ReadToEnd();
sr.Close();
return xxx;
}
///
/// delete
///
///
///
public string HTTPSDelete(string url)
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;
ServicePointManager.ServerCertificateValidationCallback =
new System.Net.Security.RemoteCertificateValidationCallback(CheckValidationResult);
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "DELETE";
req.ContentLength = 0;
req.CookieContainer = cookies;
string xxx = string.Empty;
using (HttpWebResponse response = req.GetResponse() as HttpWebResponse)
{
if (response.StatusCode == HttpStatusCode.OK)
{
HttpWebResponse myResponse = (HttpWebResponse)req.GetResponse();
Stream resSt = myResponse.GetResponseStream();
StreamReader sr = new StreamReader(resSt);
xxx = sr.ReadToEnd();
sr.Close();
}
}
return xxx;
}
}
}