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 ...
随机推荐
- JSONObject、JSONArray、Map、JavaBean的相互转换
1,JSONObject json对象,就是一个键对应一个值,使用的是大括号{ },如:{key:value} 2,JSONArray json数组,使用中括号[ ],只不过数组里面的项也是json键 ...
- Linux Crontab及使用salt进行管理
一.引言: 最近无意之间看到salt有一个cron的模块,今天就在这里介绍linux crontab以及通过salt的cron对crontab的管理. 二.Linux crontab的介绍: cron ...
- VS2017打开VS2010项目报 “找不到*.xaml”错误
VS2017打开VS2010项目报 “找不到*.xaml”错误.详细如下: 未处理System.IO.IOExceptionMessage: “System.IO.IOException”类型的未经处 ...
- 微信小程序--相关资料
微信小程序Demo https://github.com/zce/weapp-demojustjavac/awesomewechatweapp: 微信小程序开发资源汇总 https://gith ...
- Effective Java 第三版——88. 防御性地编写READOBJECT方法
Tips 书中的源代码地址:https://github.com/jbloch/effective-java-3e-source-code 注意,书中的有些代码里方法是基于Java 9 API中的,所 ...
- 让Elasticsearch集群冷热分离、读写分离【转】
转自:https://blog.csdn.net/jiao_fuyou/article/details/50511255 根据Elasticsearch中文社区<ES冷热分离(读写分离) hot ...
- SQL Server 2016新特性:Temporal Table
什么是系统版本的Temporal Table 系统版本的Temporal Table是可以保存历史修改数据并且可以简单的指定时间分析的用户表. 这个Temporal Table就是系统版本的Tempo ...
- Asp.Net WebApi上传图片
webapi using System; using System.Collections; using System.Collections.Generic; using System.Diagno ...
- docker save提示no space left on device错误
使用df -h看了看,硬盘的确是够用的,于是排除了是硬盘容量的问题. 再细看错误提示: 目录是/var/lib/docker/tmp/docker-export-xxxx/xxxxx,猜测是docke ...
- 如何配置JVM系统属性及获取方式System.getProperty("pname")
https://www.cnblogs.com/keyi/p/7721893.html