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, , 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, , 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();
}
}
}
}

WebH的更多相关文章

  1. Python初学者之网络爬虫

    声明:本文内容和涉及到的代码仅限于个人学习,任何人不得作为商业用途. 本文将介绍我最近在学习Python过程中写的一个爬虫程序,将力争做到不需要有任何Python基础的程序员都能读懂.读者也可以先跳到 ...

  2. UIWebView中加载HTML的Table,td设置百分比宽度并且宽度不被里面的内容撑开

    正常情况下,iOS使用WebView加载HTML的Table时,为了让Table适应屏幕宽度,会使用百分比设置td的宽度,但是由于td中的内容过多,td会被撑开,导致整个Table的宽度会变宽,超出屏 ...

  3. C# 经典入门15章-TextBoxControl

    第一步:设计界面如下:

  4. 内网jenkins如何配置gitlab自动拉取代码打包

    在全局工具配置中添加git安装目录的配置 http://10.2.1.92:8080/jenkins/configureTools/git1.8.3.1/usr/bin/git 打开系统设置配置git ...

  5. Shiro:初识Shiro及简单尝试

    Shiro 一.什么是Shiro Apache Shiro是Java的一个安全(权限)框架 作用:认证.授权.加密.会话管理.与web集成.缓存等 下载地址:http://shiro.apache.o ...

  6. FreeMarker分页组件监听器

    分页组件监听器 /*  * project名:    * 包     名: com.companyName.dhm.iepgm.common.taglib  * 文 件名: PaginatedList ...

随机推荐

  1. Docker查看运行中容器并进入容器

    一.简述 Docker查看运行中容器并进入容器. 二.方法 $ sudo docker ps $ sudo docker exec -it 775c7c9ee1e1 /bin/bash 将黄色文字替换 ...

  2. FileClassify文件日期分类工具

    FileClassify是一款免费的文件按日期分类工具,能够根据文件修改日期,将文件移动或复制到对应的目录中 如果对您有较大的帮助,欢迎捐赠我们,我们对您表示衷心的感谢! 1.输入文件夹和输出文件可以 ...

  3. kafka创建topic异常

    问题描述: kafak运行在weblogic账户下,jdk1.8,当在root账户下创建topic(当前账户下的jdk1.6)导致创建topic失败 ./bin/kafka-topics.sh --c ...

  4. m3u8转码

    ffmpeg -i input.mp4 -c:v libx264 -c:a aac -strict -2 -f hls -hls_list_size 0 -hls_time 5 output.m3u8

  5. Docker Mongo数据库开启用户认证

    一.启动mongo容器的几种方式 #简化版 docker run --name mongo1 -p 21117:27017 -d mongo --noprealloc --smallfiles #自定 ...

  6. R语言︱处理缺失数据&&异常值检验、离群点分析、异常值处理

    在数据挖掘的过程中,数据预处理占到了整个过程的60% 脏数据:指一般不符合要求,以及不能直接进行相应分析的数据 脏数据包括:缺失值.异常值.不一致的值.重复数据及含有特殊符号(如#.¥.*)的数据 数 ...

  7. 【PHP】解析PHP的GD库

    官方文档:http://php.net/manual/en/book.image.php 1.GD库简介 PHP可以创建和操作多种不同格式的图像文件.PHP提供了一些内置的图像信息函数,也可以使用GD ...

  8. 让rpc支持双向通信

    rpc采用了C/S模型,不支持双向通信:client只能远程调用server端的RPC接口,但client端则没有RPC供server端调用,这意味着,client端能够主动与server端通信,但s ...

  9. VMware下centOS yum报错cannot find a valid baseurl or repo:base 解决方法

    *无法联网的明显表现会有: 1.yum install出现 Error: cannot find a valid baseurl or repo:base 2.ping host会提示unknown ...

  10. OpenCV自带dnn的Example研究(4)— openpose

    这个博客系列,简单来说,今天我们就是要研究 https://docs.opencv.org/master/examples.html下的 6个文件,看看在最新的OpenCV中,它们是如何发挥作用的. ...