C#爬页面总结
1.2 fiddler工具
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Business
{
/// <summary>
/// HttpCallHelper
/// </summary>
public class HttpCallHelper
{
/// <summary>
/// post
/// </summary>
/// <param name="param">param</param>
/// <returns>HttpResultInfo</returns>
public static HttpResultInfo Post(HttpRequestParams param)
{
HttpResultInfo res = null;
try
{
param.RequestEncoding = Encoding.Default;
byte[] bs = param.RequestEncoding.GetBytes(param.Data);
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(param.Url);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
req.ContentLength = bs.Length;
if (!string.IsNullOrEmpty(param.Cookie))
{
req.Headers[HttpRequestHeader.Cookie] = param.Cookie;
}
req.Referer = param.Cookie;
using (Stream reqStream = req.GetRequestStream())
{
reqStream.Write(bs, , bs.Length);
}
string strResponse = string.Empty;
HttpWebResponse httpResponse = (HttpWebResponse)req.GetResponse();
using (Stream responseStream = httpResponse.GetResponseStream())
{
Stream stream = responseStream;
StreamReader streamReader = new StreamReader(stream, param.ResponseEncoding);
strResponse = streamReader.ReadToEnd();
streamReader.Close();
}
string retcookie = req.GetResponse().Headers["Set-Cookie"];
res = new HttpResultInfo()
{
Cookie = retcookie,
StatusCode = httpResponse.StatusCode,
StatusDescription = httpResponse.StatusDescription,
Headers = httpResponse.Headers,
ErrorMsg = string.Empty,
Html = strResponse,
ResponseUrl = httpResponse.ResponseUri,
};
return res;
}
catch (Exception esx)
{
res = new HttpResultInfo()
{
ErrorMsg = esx.Message.ToString(),
};
Console.WriteLine(esx.Message.ToString());
}
return res;
}
/// <summary>
/// Get
/// </summary>
/// <param name="httpParam">httpParam</param>
/// <param name="param">param</param>
/// <returns>结果</returns>
public static HttpResultInfo Get(HttpRequestParams httpParam, Dictionary<string, string> param)
{
StringBuilder sb = new StringBuilder();
foreach (var item in param)
{
sb.AppendFormat("{0}={1}&", item.Key, item.Value);
}
httpParam.Data = sb.ToString();
return Get(httpParam);
}
/// <summary>
/// Get
/// </summary>
/// <param name="param">param</param>
/// <returns>结果</returns>
public static HttpResultInfo Get(HttpRequestParams param)
{
HttpResultInfo ret = null;
try
{
string strResult = string.Empty;
HttpWebRequest httpRequest;
HttpWebResponse httpResponse;
string urlStr = param.Url;
if (!string.IsNullOrEmpty(param.Data))
{
urlStr = string.Format("{0}{1}", param.Url + "?", param.Data);
}
httpRequest = (HttpWebRequest)WebRequest.Create(new Uri(urlStr));
httpRequest.Timeout = param.Timeout;
httpRequest.Method = "Get";
httpRequest.ContentType = param.ContentType;
if (!string.IsNullOrEmpty(param.Cookie))
{
httpRequest.Headers[HttpRequestHeader.Cookie] = param.Cookie;
}
//// 获取提交返回信息
httpResponse = (HttpWebResponse)httpRequest.GetResponse();
string returnStr = string.Empty;
using (Stream st = httpResponse.GetResponseStream())
{
returnStr = new StreamReader(st, param.ResponseEncoding).ReadToEnd();
}
string cookie1 = httpResponse.Headers["Set-Cookie"];
ret = new HttpResultInfo()
{
Cookie = cookie1,
StatusCode = httpResponse.StatusCode,
StatusDescription = httpResponse.StatusDescription,
Headers = httpResponse.Headers,
ErrorMsg = string.Empty,
Html = returnStr,
ResponseUrl = httpResponse.ResponseUri,
};
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString().ToString());
ret = new HttpResultInfo()
{
Html = string.Empty,
ErrorMsg = ex.Message.ToString(),
Cookie = string.Empty,
};
}
return ret;
}
/// <summary>
/// GetQueryString
/// </summary>
/// <param name="param">param</param>
/// <returns>结果</returns>
public static string GetQueryString(Dictionary<string, string> param)
{
StringBuilder sb = new StringBuilder();
foreach (var item in param)
{
sb.AppendFormat("{0}={1}&", item.Key, item.Value);
}
if (sb.Length > )
{
sb = sb.Remove(sb.Length - , );
}
return sb.ToString();
}
}
/// <summary>
/// 请求消息
/// </summary>
public class HttpRequestParams
{
/// <summary>
/// 请求编码
/// </summary>
private Encoding requestEncoding = Encoding.Default;
/// <summary>
/// 响应编码
/// </summary>
private Encoding responseEncoding = Encoding.Default;
/// <summary>
/// 请求超时时间(以毫秒为单位,默认180秒)
/// </summary>
private int timeout = ;
/// <summary>
/// 请求返回类型(默认text/html)
/// </summary>
private string contentType = "text/html";
/// <summary>
/// HttpRequestParams
/// </summary>
public HttpRequestParams()
{
}
/// <summary>
/// 请求地址
/// </summary>
public string Url
{
get;
set;
}
/// <summary>
/// 数据
/// </summary>
public string Data
{
get;
set;
}
/// <summary>
/// Cookie
/// </summary>
public string Cookie
{
get;
set;
} /// <summary>
/// ContentType
/// </summary>
public string ContentType
{
get { return this.contentType; }
set { this.contentType = value; }
}
/// <summary>
/// Referer
/// </summary>
public string Referer
{
get;
set;
}
/// <summary>
/// Timeout
/// </summary>
public int Timeout
{
get { return this.timeout; }
set { this.timeout = value; }
}
/// <summary>
/// RequestEncoding
/// </summary>
public Encoding RequestEncoding
{
get { return this.requestEncoding; }
set
{
if (value == null)
{
throw new Exception("请求编码格式不能设置为空!");
}
this.requestEncoding = value;
}
}
/// <summary>
/// 返回编码
/// </summary>
public Encoding ResponseEncoding
{
get { return this.responseEncoding; }
set
{
if (value == null)
{
throw new Exception("响应编码格式不能设置为空!");
}
this.responseEncoding = value;
}
}
}
/// <summary>
/// 返回消息
/// </summary>
public class HttpResultInfo
{
/// <summary>
/// Html
/// </summary>
public string Html
{
get;
set;
}
/// <summary>
/// Cookie
/// </summary>
public string Cookie
{
get;
set;
}
/// <summary>
/// IsSuccess
/// </summary>
public HttpStatusCode StatusCode
{
get;
set;
}
/// <summary>
/// ErrorMsg
/// </summary>
public string ErrorMsg
{
get;
set;
}
/// <summary>
/// 状态描述
/// </summary>
public string StatusDescription { get; set; }
/// <summary>
/// 响应头
/// </summary>
public WebHeaderCollection Headers { get; set; }
/// <summary>
/// 返回Uri
/// </summary>
public Uri ResponseUrl { get; set; }
}
}
C#爬页面总结的更多相关文章
- python爬取youtube视频 多线程 非中文自动翻译
声明:我写的所有文章都是发在博客园的,我看到其他复制粘贴过去的 连个出处也不写,直接打上自己的水印...真是没的说了. 前言:前段时间搞了一些爬视频的项目,代码都写好了,这里写文章那就在来重新分析一遍 ...
- 【nodejs 爬虫】使用 puppeteer 爬取链家房价信息
使用 puppeteer 爬取链家房价信息 目录 使用 puppeteer 爬取链家房价信息 页面结构 爬虫库 pupeteer 库 实现 打开待爬页面 遍历区级页面 方法一 方法二 遍历街道页面 遍 ...
- python 爬虫 汽车之家车辆参数反爬
水平有限,仅供参考. 如图所示,汽车之家的车辆详情里的数据做了反爬对策,数据被CSS伪类替换. 观察 Sources 发现数据就在当前页面. 发现若干条进行CSS替换的js 继续深入此JS 知道了数据 ...
- webmagic的设计机制及原理-如何开发一个Java爬虫
之前就有网友在博客里留言,觉得webmagic的实现比较有意思,想要借此研究一下爬虫.最近终于集中精力,花了三天时间,终于写完了这篇文章.之前垂直爬虫写了一年多,webmagic框架写了一个多月,这方 ...
- web magic 小结
缘起 写了多年的程序,鲜有产出物,于是最近打算做个不可说的东西来祭奠逝去的青春.数据,是一个程序的起点,我们没有数以亿计的用户,无法让活跃用户给我们产生数据,那就只能去别人的站点上借点数据了.这个功能 ...
- Python3学习笔记2:简易Web爬虫
开发环境 基础语法那章的内容我是在Docker容器中玩的,但是真正做项目的时候,没有IDE的强大辅助功能来协助的话是很累人的一件事.因此从本文中,我选择使用Jetbrain的Pycharm这个IDE来 ...
- selenium设置proxy、headers(phantomjs、Chrome、Firefox)
phantomjs 设置ip 方法1: service_args = [ '--proxy=%s' % ip_html, # 代理 IP:prot (eg:192.168.0.28:808) '--p ...
- Python——day11 函数(对象、名称空间、作用域、嵌套、闭包)
一.函数对象 函数名就是存放了函数的内存地址,存放了内存地址的变量都是对象,即 函数名 就是 函数对象 函数对象的应用 1. 可以直接被引用 fn = cp_fn 2 .可以当作函数参数传递 c ...
- day 9~11 函数
今日内容 '''函数四个组成部分函数名:保存的是函数的地址,是调用函数的依据函数体:就是执行特定功能的代码块函数返回值:代码块执行的结果反馈函数参数:完成功能需要的条件信息1.函数的概念2.函数的定 ...
随机推荐
- 用js获取当前页面的url的相关信息方法
当前页面对应的URL的一些属性: ( http://bbs.xxx.net/forum.php?mod=viewthread&tid=2709692&page=1&extra= ...
- jQuery实现联动下拉列表查询框
<!DOCTaYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org ...
- SQL2008 提示评估期已过的解决方法
提示窗口: 解决步骤: 第一步:进入SQL2008配置工具中的安装中心 第二步:再进入维护界面,选择版本升级 第三步:进入产品密钥,输入密钥 第四步:一直点下一步,直到升级完毕. SQL Server ...
- 详解apache的allow和deny
今天看了一篇关于apache allow,deny的文章收获匪浅,防止被删,我直接摘过来了,原文地址!!! !http://www.cnblogs.com/top5/archive/2009/09/2 ...
- six month dormancy test
source data: accountleg year_month amount acc1A 2010-01 100 acc1A 2010-02 100 acc1 ...
- 有向图的拓扑排序算法JAVA实现
一,问题描述 给定一个有向图G=(V,E),将之进行拓扑排序,如果图有环,则提示异常. 要想实现图的算法,如拓扑排序.最短路径……并运行看输出结果,首先就得构造一个图.由于构造图的方式有很多种,这里假 ...
- ref out 方法参数
ref out 相似 ref和out两个关键字的作用大致相同,但是有一些微妙但是重要的区别. 两者的行为相似到连编译器都认为这两者不能被重载:public void SampleMethod(out ...
- 关于 app测试工具
1. 腾讯的GT 2. testin 云测 3. monkey的自动化测试 4. 纯手工的功能测试.
- ArcGis 中MapControl 框选
void mCtrl_OnMouseDown(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseDownEvent e) ...
- 一。常用UIView的属性和方法
1.frame 控件所在的矩形框的位置和尺寸(以父控件的左上角为坐标原点) 2.bounds 控件 控件所在的矩形框的位置和尺寸(以自己的左上角为坐标原点,所以bounds的x/y一般为0) 3.ce ...