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. JAVA获取程序(打成jar或classpath)所在目录

    一.简述 JAVA获取程序(打成jar或classpath)所在目录. 二.代码 package dearcloud.utils.context; import dearcloud.utils.Str ...

  2. iOS 线上版本图片资源格式的问题导致的闪退

    链接:https://www.jianshu.com/p/6492779cb89e來源:简书 导致这种问题的原因是:在 Xcode 8 中,当你资源文件中[含有16位图]或者[图片显示模式γ值为'P3 ...

  3. Jackson 解析json数据之忽略解析字段注解@JsonIgnoreProperties

    转自:http://blog.csdn.net/ngl272/article/details/70217104 以前解析json用的惯的就是Google的gson了,用惯了基本就用它了,一直也没发现什 ...

  4. CSS-2

    day 39 学习链接:https://www.cnblogs.com/yuanchenqi/articles/5977825.html 4  文本属性 font-size: 10px; text-a ...

  5. FILESTREAM feature can't be enabled if you use cluster shared volumes

    Create a SQL Cluster instance. Create Cluster Shared Volume Please note. No Share storage is added i ...

  6. Pandas基础(十一)时间序列

    1. pandas时间序列:时间索引 2. pandas时间序列数据结构 2.1 定期序列 3. 频率和偏移 4. 重采样,转移,加窗口 4.1 重采样及频率转换 4.2 时间移动 4.3 滚动窗口 ...

  7. 构建基于Suricata+Splunk的IDS入侵检测系统

    一.什么是IDS和IPS? IDS(Intrusion Detection Systems):入侵检测系统,是一种网络安全设备或应用软件,可以依照一定的安全策略,对网络.系统的运行状况进行监视,尽可能 ...

  8. 【iCore4 双核心板_ARM】例程二十六:LWIP_MODBUS_TCP实验——电源监控

    实验现象: 核心代码: int main(void) { system_clock.initialize(); led.initialize(); adc.initialize(); delay.in ...

  9. layui form表单自定义sm格式

    1. 新建以下sm样式,保存为layform_sm.css文件名,然后导入到layui.css的后面. .layui-input-sm,.layui-select-sm,.layui-textarea ...

  10. hdoj:2050

    #include <iostream> using namespace std; int main() { int T; while (cin >> T) { ; i < ...