zoukankan      html  css  js  c++  java
  • HttpHelper

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Net;
    using System.IO;
    using System.Text.RegularExpressions;
    using System.Windows.Forms;

    namespace ShanXiDianXianWeb
    {
    public class HttpHelper
    {
    public static CookieContainer CookieContainers = new CookieContainer();

    public static string FireFoxAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.23) Gecko/20110920 Firefox/3.6.23";
    public static string IE7 = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; InfoPath.2; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET4.0C; .NET4.0E)";
    public static string IE = " Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E) ";
    /// <summary>
    ///
    /// </summary>
    /// <param name="url"></param>
    /// <param name="method">"POST" or "GET"</param>
    /// <param name="data">when the method is "POST", the data will send to web server, if the method is "GET", the data should be string.empty</param>
    /// <returns></returns>
    public static string GetResponse(string url, string method, string data)
    {
    try
    {
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
    req.KeepAlive = true;
    req.Method = method.ToUpper();
    req.AllowAutoRedirect = true;
    req.CookieContainer = CookieContainers;
    req.ContentType = "application/x-www-form-urlencoded";

    req.UserAgent = IE;
    // req.UserAgent = FireFoxAgent;
    req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";

    req.Timeout = 50000;

    if (method.ToUpper() == "POST" && data != null)
    {
    ASCIIEncoding encoding = new ASCIIEncoding();
    byte[] postBytes = encoding.GetBytes(data); ;
    req.ContentLength = postBytes.Length;
    Stream st = req.GetRequestStream();
    st.Write(postBytes, 0, postBytes.Length);
    st.Close();
    }
    //http证书
    //System.Net.ServicePointManager.ServerCertificateValidationCallback += (se, cert, chain, sslerror) =>
    //{
    // return true;
    //};

    Encoding myEncoding = Encoding.GetEncoding("gb2312");

    HttpWebResponse res = (HttpWebResponse)req.GetResponse();
    Stream resst = res.GetResponseStream();
    StreamReader sr = new StreamReader(resst, myEncoding);
    string str = sr.ReadToEnd();

    return str;
    }
    catch (Exception)
    {
    return string.Empty;
    }
    }

    public static string GetResponse(string url, string method, string data, ref CookieContainer cookiecontainer)
    {
    try
    {
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
    req.KeepAlive = true;
    req.Method = method.ToUpper();
    req.AllowAutoRedirect = true;
    if (cookiecontainer == null || cookiecontainer.Count == 0)
    {
    cookiecontainer = new CookieContainer();
    }
    req.CookieContainer = cookiecontainer;
    req.ContentType = "application/x-www-form-urlencoded";

    req.UserAgent = IE;
    // req.UserAgent = FireFoxAgent;
    req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
    req.Timeout = 60000;

    if (method.ToUpper() == "POST" && data != null)
    {
    ASCIIEncoding encoding = new ASCIIEncoding();
    byte[] postBytes = encoding.GetBytes(data); ;
    req.ContentLength = postBytes.Length;
    Stream st = req.GetRequestStream();
    st.Write(postBytes, 0, postBytes.Length);
    st.Close();
    }
    //http证书
    //System.Net.ServicePointManager.ServerCertificateValidationCallback += (se, cert, chain, sslerror) =>
    //{
    // return true;
    //};

    Encoding myEncoding = Encoding.GetEncoding("utf-8");

    HttpWebResponse res = (HttpWebResponse)req.GetResponse();
    Stream resst = res.GetResponseStream();
    StreamReader sr = new StreamReader(resst, myEncoding);
    string str = sr.ReadToEnd();

    return str;
    }
    catch (Exception ex)
    {
    return string.Empty;
    }
    }

    /// <summary>
    /// 获得图片流
    /// </summary>
    /// <param name="url"></param>
    /// <param name="cookiecontainer"></param>
    /// <returns></returns>
    public static Stream GetResponseImage(string url, ref CookieContainer cookiecontainer)
    {
    Stream resst = null;
    try
    {
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
    req.KeepAlive = true;
    req.Method = "GET";
    req.AllowAutoRedirect = true;
    //req.CookieContainer = cookieContainer;
    if (cookiecontainer == null || cookiecontainer.Count == 0)
    {
    cookiecontainer = new CookieContainer();
    }
    req.CookieContainer = cookiecontainer;
    req.ContentType = "application/x-www-form-urlencoded";

    req.UserAgent = IE7;
    req.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
    req.Timeout = 50000;

    System.Net.ServicePointManager.ServerCertificateValidationCallback += (se, cert, chain, sslerror) =>
    {
    return true;
    };

    Encoding myEncoding = Encoding.GetEncoding("UTF-8");

    HttpWebResponse res = (HttpWebResponse)req.GetResponse();
    resst = res.GetResponseStream();

    return resst;
    }
    catch
    {
    return null;
    }
    }

    private string GetPageStringbyWebBrowser(string url)
    {
    if (url.Equals("about:blank")) return null; ;
    if (!url.StartsWith("http://") && !url.StartsWith("https://")) { url = "http://" + url; }

    WebBrowser myWB = new WebBrowser();
    myWB.ScrollBarsEnabled = false;
    myWB.Navigate(url);
    while (myWB.ReadyState != WebBrowserReadyState.Complete)
    {
    System.Windows.Forms.Application.DoEvents();
    }
    if (myWB != null)
    {
    System.IO.StreamReader getReader = null;
    try
    {
    getReader = new System.IO.StreamReader(myWB.DocumentStream, System.Text.Encoding.GetEncoding(myWB.Document.Encoding));
    string gethtml = getReader.ReadToEnd();
    //codel = gethtml.Substring(1000,);
    // Image i = Image.FromStream(gethtml);
    MessageBox.Show(gethtml);

    return gethtml;
    }
    catch { return null; }
    finally
    {
    if (getReader != null) { getReader.Close(); }
    myWB.Dispose();
    }
    }
    return null;
    }
    public static string GetRegexString(string pattern, string source)
    {
    Regex r = new Regex(pattern);
    MatchCollection mc = r.Matches(source);
    return mc[0].Groups[1].Value;
    }

    public static string[] GetRegexStrings(string pattern, string source)
    {
    Regex r = new Regex(pattern);
    MatchCollection mcs = r.Matches(source);

    string[] ret = new string[mcs.Count];

    for (int i = 0; i < mcs.Count; i++)
    ret[i] = mcs[i].Groups[1].Value;

    return ret;
    }
    }
    }

  • 相关阅读:
    SkyWalking结合Logback获取全局唯一标识 trace-id 记录到日志中
    Mysql数据库优化技术
    MySQL中集合的差的运算方法
    深入理解Java ClassLoader及在 JavaAgent 中的应用
    自制吸锡带
    Ubuntu下双显示器设定
    ffmpeg 命令的使用
    ifeq ifneq ifdef ifndef
    字符对齐
    ruby on rails使用gmail的smtp发送邮件
  • 原文地址:https://www.cnblogs.com/feifeikui/p/4529387.html
Copyright © 2011-2022 走看看