| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- 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
- {
- /// <summary>
- /// http工具
- /// </summary>
- public partial class http_helper
- {
- /// <summary>
- /// 基工具
- /// </summary>
- private static http_helper Net = new http_helper();
- /// <summary>
- /// 基cookie
- /// </summary>
- private CookieContainer cookies = new CookieContainer();
- /// <summary>
- /// post
- /// </summary>
- /// <param name="url"></param>
- /// <param name="content"></param>
- /// <returns></returns>
- public static string Post(string url, string content)
- {
- return Net.HTTP_POST(url, content, "", "");
- }
- /// <summary>
- /// post
- /// </summary>
- /// <param name="url"></param>
- /// <param name="content"></param>
- /// <param name="headTitle"></param>
- /// <param name="headContent"></param>
- /// <returns></returns>
- public static string Post(string url, string content, string headTitle, string headContent)
- {
- return Net.HTTP_POST(url, content, headTitle, headContent);
- }
- /// <summary>
- /// get
- /// </summary>
- /// <param name="url"></param>
- /// <returns></returns>
- public static string Get(string url)
- {
- return Net.HTTP_GET(url, 100, "", "");
- }
- /// <summary>
- /// get
- /// </summary>
- /// <param name="url"></param>
- /// <param name="headTitle"></param>
- /// <param name="headContent"></param>
- /// <returns></returns>
- public static string Get(string url, string headTitle, string headContent)
- {
- return Net.HTTP_GET(url, 100, headTitle, headContent);
- }
- /// <summary>
- /// get
- /// </summary>
- /// <param name="url"></param>
- /// <param name="timeout"></param>
- /// <returns></returns>
- public static string Get(string url, int timeout)
- {
- return Net.HTTP_GET(url, timeout, "", "");
- }
- /// <summary>
- /// delete
- /// </summary>
- /// <param name="url"></param>
- /// <returns></returns>
- public static string Delete(string url)
- {
- return Net.HTTPSDelete(url);
- }
- /// <summary>
- /// post
- /// </summary>
- /// <param name="url"></param>
- /// <param name="Content"></param>
- /// <param name="headTitle"></param>
- /// <param name="headContent"></param>
- /// <returns></returns>
- 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);
- }
- }
- /// <summary>
- /// https
- /// </summary>
- /// <param name="sender"></param>
- /// <param name="certificate"></param>
- /// <param name="chain"></param>
- /// <param name="errors"></param>
- /// <returns></returns>
- public bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
- {
- return true;
- }
- /// <summary>
- /// get
- /// </summary>
- /// <param name="url"></param>
- /// <param name="timeout"></param>
- /// <param name="headTitle"></param>
- /// <param name="headContent"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// delete
- /// </summary>
- /// <param name="url"></param>
- /// <returns></returns>
- 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;
- }
- }
- }
|