WebH

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;

namespace Common
{
    public class WebH
    {
        WebProxy proxy;
        public string ContentType { get; set; } = "application/x-www-form-urlencoded";
        public string Accept { get; set; } = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
        public string UserAgent { get; set; } = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.152 Safari/537.36";
        public CookieContainer Cookie { get; set; } = new CookieContainer();
        public WebH() { }
        public WebH(WebProxy proxy)
        {
            this.proxy = proxy;
        }
        public WebH(string url, string user, string pwd)
        {
            proxy = new WebProxy()
            {
                Address = new Uri(url),//"http://openproxy..com:8080"
                Credentials = new NetworkCredential(user, pwd),
            };
        }

        public string GetHtml(string url)
        {
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
            HttpWebResponse response = null;
            try
            {
                request.Method = "GET";
                request.Proxy = proxy;
                request.CookieContainer = Cookie;
                request.ContentType = ContentType;
                //request.ServicePoint.ConnectionLimit = 300;
                response = (HttpWebResponse)request.GetResponse();
                Stream responseStream = response.GetResponseStream();
                StreamReader streamReader = new StreamReader(responseStream, Encoding.UTF8);
                return streamReader.ReadToEnd();
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                request.Abort();
                if (response != null)
                    response.Close();
            }
        }

        public string PostData(string url, string param, Action<WebHeaderCollection> action = null)
        {
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
            try
            {
                byte[] bs = Encoding.ASCII.GetBytes(param);
                request.Method = "POST";
                request.Accept = Accept;
                request.UserAgent = UserAgent;
                request.Proxy = proxy;
                request.ContentType = ContentType;
                request.ContentLength = bs.Length;
                request.CookieContainer = Cookie;
                request.KeepAlive = true;
                request.Referer = "https://www3.wipo.int/dasapplicant/en/pages/workbench/applicant.xhtml";
                action?.Invoke(request.Headers);
                using (Stream reqStream = request.GetRequestStream())
                {
                    reqStream.Write(bs, 0, bs.Length);//写数据时开始访问
                }
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    return new StreamReader(response.GetResponseStream(), Encoding.UTF8).ReadToEnd();
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
            finally
            {
                request.Abort();
            }
        }

        public string PostDataAJAX(string url, string param)
        {
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
            try
            {
                byte[] bs = Encoding.ASCII.GetBytes(param);
                request.Method = "POST";
                request.Accept = Accept;
                request.UserAgent = UserAgent;
                request.Proxy = proxy;
                request.ContentType = ContentType;
                request.ContentLength = bs.Length;
                request.CookieContainer = Cookie;
                request.KeepAlive = true;
                //request.Connection = "keep-alive";
                request.Headers.Add("X-Requested-With", "XMLHttpRequest");
                using (Stream reqStream = request.GetRequestStream())
                {
                    reqStream.Write(bs, 0, bs.Length);//写数据时开始访问
                }
                using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                {
                    return new StreamReader(response.GetResponseStream(), Encoding.UTF8).ReadToEnd();
                }
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
            finally
            {
                //request.Abort();
            }
        }
    }
}

 

转载于:https://www.cnblogs.com/lee2011/p/7421641.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值