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, , 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的更多相关文章
- Python初学者之网络爬虫
声明:本文内容和涉及到的代码仅限于个人学习,任何人不得作为商业用途. 本文将介绍我最近在学习Python过程中写的一个爬虫程序,将力争做到不需要有任何Python基础的程序员都能读懂.读者也可以先跳到 ...
- UIWebView中加载HTML的Table,td设置百分比宽度并且宽度不被里面的内容撑开
正常情况下,iOS使用WebView加载HTML的Table时,为了让Table适应屏幕宽度,会使用百分比设置td的宽度,但是由于td中的内容过多,td会被撑开,导致整个Table的宽度会变宽,超出屏 ...
- C# 经典入门15章-TextBoxControl
第一步:设计界面如下:
- 内网jenkins如何配置gitlab自动拉取代码打包
在全局工具配置中添加git安装目录的配置 http://10.2.1.92:8080/jenkins/configureTools/git1.8.3.1/usr/bin/git 打开系统设置配置git ...
- Shiro:初识Shiro及简单尝试
Shiro 一.什么是Shiro Apache Shiro是Java的一个安全(权限)框架 作用:认证.授权.加密.会话管理.与web集成.缓存等 下载地址:http://shiro.apache.o ...
- FreeMarker分页组件监听器
分页组件监听器 /* * project名: * 包 名: com.companyName.dhm.iepgm.common.taglib * 文 件名: PaginatedList ...
随机推荐
- python接口自动化测试(八)-unittest-生成测试报告
用例的管理问题解决了后,接下来要考虑的就是报告我问题了,这里生成测试报告主要用到 HTMLTestRunner.py 这个模块,下面简单介绍一下如何使用: 一.下载HTMLTestRunner下载: ...
- RGBA alpha 透明度混合算法实现和测试
目录 1.算法叙述 1.1.透明度混合算法1 1.3.简易Alpha混合算法 2.算法实现代码和测试 2.1.透明度混合算法1实现代码 2.1.AlphaBlend算法实现代码 2.3.测试截图 2. ...
- centos7下docker二进制安装
问题描述: 内网离线环境下,安装docker.二进制来的还是快点,或者内网搭建yum的epel仓库! 环境: centos7.x docker-18.9.0 x86_64 static //二进制 ...
- Redis 学习之路 (011) - redis 多数据库
一台服务器上都快开启200个redis实例了,看着就崩溃了.这么做无非就是想让不同类型的数据属于不同的应用程序而彼此分开. 那么,redis有没有什么方法使不同的应用程序数据彼此分开同时又存储在相同的 ...
- java写桌面程序
一:使用java swing开发窗口程序 简述: 1.文章内容主要是使用java swing类库开发一个小的窗口程序,然后使用exe4j发布成exe可以安装的程序,让初学者对使用java来做pc软件开 ...
- Mathematica新特性Inactive, 求解复杂微分方程
Inactive阻止函数的计算, 求解微分方程有奇效 Block[{Integrate = Inactive[Integrate]}, DSolve[((H - h0)^(7/5) P0 (T - c ...
- 在 WPF 中如何在控件上屏蔽系统默认的触摸长按事件
来源:https://stackoverflow.com/questions/5962108/disable-a-right-click-press-and-hold-in-wpf-applicati ...
- 如何在phpstorm中查看yaf框架源码
1.到github下载yaf框架的doc 下载链接 https://github.com/elad-yosifon/php-yaf-doc/archive/master.zip 2.解压zip包 3. ...
- RSA 分段加解密【解决“不正确的长度”的异常】
RSA 是常用的非对称加密算法.最近使用时却出现了“不正确的长度”的异常,研究发现是由于待加密的数据超长所致. .NET Framework 中提供的 RSA 算法规定: 待加密的字节数不能超过密钥的 ...
- bash计算上下行数据差值
for i in {1..60000}; do echo "`date +'%F %T'` `df /dev/md0 | grep 'data1'` "; sleep 1; don ...