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 ...
随机推荐
- shell编程学习笔记(四):Shell中转义字符的输出
通过echo可以输出字符串,下面看一下怎么输出特殊转义字符,首先我先列出来echo的转义字符: \\ 输入\ \a 输出警告音 \b 退格,即向左删除一个字符 \c 取消输出行末的换行符,和-n选项一 ...
- [Python设计模式] 第22章 手机型号&软件版本——桥接模式
github地址:https://github.com/cheesezh/python_design_patterns 紧耦合程序演化 题目1 编程模拟以下情景,有一个N品牌手机,在上边玩一个小游戏. ...
- Ubuntu 16.04 升级git
To update git on Ubuntu 12.04 just follow this line of commands: sudo apt-get install python-softwar ...
- 解析 .Net Core 注入——注册服务
在学习 Asp.Net Core 的过程中,注入可以说是无处不在,对于 .Net Core 来说,它是独立的一个程序集,没有复杂的依赖项和配置文件,所以对于学习 Asp.Net Core 源码的朋友来 ...
- 关于go语言的测试相关内容笔记
其实之前对于测试自己一直比较弱,不管是python的还是go的,关于测试这块并没有非常注重,这次就好好整理一下关于go的测试 单元测试 Go程序主要包含三类测试: 功能测试(test).基准测试(be ...
- 我的IntelliJ IDEA 设置
1.关闭代码折叠 2.设置代码格式 3.函数参数提醒
- dajpper使用教程
1.表结构 2.程序对应的实体类 3.基本操作 3.1 插入 123456789 public int Insert(Person person, string _ConnString){ using ...
- Troubleshooting Scheduler Autotask Issues (Doc ID 1561498.1)
In this Document Purpose Troubleshooting Steps References APPLIES TO: Oracle Database - Enterp ...
- 理解、学习与使用 JAVA 中的 OPTIONAL<转>
从 Java 8 引入的一个很有趣的特性是 Optional 类.Optional 类主要解决的问题是臭名昭著的空指针异常(NullPointerException) —— 每个 Java 程序员都 ...
- ExtJS6 根据Value设置单元格颜色
renderer : function(value, meta) { if(parseInt(value) > 0) { meta.style = ""; } else { ...