Web爬虫的C#请求发送
public class HttpControler
{
//post请求发送
private Encoding m_Encoding = Encoding.GetEncoding("gb2312");
public string Request(string strUrl,string postStr)
{
HttpWebRequest tHWRq = (HttpWebRequest)HttpWebRequest.Create(strUrl);
tHWRq.CookieContainer = new CookieContainer();
CookieContainer cookie = tHWRq.CookieContainer;//如果用不到Cookie,删去即可
//以下是发送的http头,随便加,其中referer挺重要的,有些网站会根据这个来反盗链
tHWRq.Referer = "http://www.cninfo.com.cn/cninfo-new/announcement/show";
tHWRq.Accept = "application/json, text/javascript, */*; q=0.01";
tHWRq.Headers["Accept-Language"] = "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3";
//tHWRq.Headers["Accept-Charset"] = "GBK,utf-8;q=0.7,*;q=0.3";
tHWRq.Headers["Accept-Encoding"] = "gzip, deflate";
tHWRq.UserAgent = "User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0";
tHWRq.KeepAlive = true;
//上面的http头看情况而定,但是下面俩必须加
tHWRq.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
tHWRq.Method = "POST";
tHWRq.Timeout = * ; Encoding encoding = Encoding.UTF8;//根据网站的编码自定义 byte[] postData = encoding.GetBytes(postStr);//postDataStr即为发送的数据,格式还是和上次说的一样 try
{
tHWRq.ContentLength = postData.Length;
Stream requestStream = tHWRq.GetRequestStream();
requestStream.Write(postData, , postData.Length);
requestStream.Close();
using (HttpWebResponse tHWRp = (HttpWebResponse)tHWRq.GetResponse())
{
using (Stream tStreamRp = tHWRp.GetResponseStream())
{
using (StreamReader tSR = new StreamReader(tStreamRp, m_Encoding))
{
string result = tSR.ReadToEnd();
tHWRq.Abort();
return result;//请求响应后返回的内容
}
}
}
}
catch (Exception e)
{
try
{
tHWRq.Abort();
}
catch (Exception err)
{
throw err;
}
return "NoUrl";
} } //Get请求发送
public bool RequestCode(string strUrl,string path)
{
HttpWebRequest tHWRq = (HttpWebRequest)HttpWebRequest.Create(strUrl);
tHWRq.CookieContainer = new CookieContainer();
CookieContainer cookie = tHWRq.CookieContainer;//如果用不到Cookie,删去即可
//以下是发送的http头,随便加,其中referer挺重要的,有些网站会根据这个来反盗链
tHWRq.Referer = "http://www.cninfo.com.cn/cninfo-new/announcement/show";
tHWRq.Accept = "application/json, text/javascript, */*; q=0.01";
tHWRq.Headers["Accept-Language"] = "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3";
tHWRq.Headers["Accept-Charset"] = "GBK,utf-8;q=0.7,*;q=0.3";
tHWRq.UserAgent = "User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0";
tHWRq.KeepAlive = true;
//上面的http头看情况而定,但是下面俩必须加
tHWRq.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
tHWRq.Method = "GET";
tHWRq.Timeout = * ;
string result = null;
try
{
using (HttpWebResponse tHWRp = (HttpWebResponse)tHWRq.GetResponse())
{
using (Stream tStreamRp = tHWRp.GetResponseStream())
{
using (StreamReader tSR = new StreamReader(tStreamRp))
{
result = tSR.ReadToEnd();
}
}
}
//正则表达式过滤想要的内容
string patternCode = "\"code\":\"\\d{6,}\"";
List<string> lstCode = new List<string>();
Regex rgxUrl = new Regex(patternCode, RegexOptions.IgnoreCase);
MatchCollection matches = rgxUrl.Matches(result);
if (matches.Count > )
{
foreach (Match matPage in matches)
{
string codeItem = matPage.Value;
if (!string.IsNullOrEmpty(codeItem))
{
string code = codeItem.Substring(codeItem.IndexOf(":") + );
lstCode.Add(code);
}
}
} using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write))
{
using (StreamWriter sw = new StreamWriter(fs))
{
foreach (string code in lstCode)
{
sw.WriteLine(code);
} }
}
tHWRq.Abort();
return true;
}
catch (Exception e)
{
try
{
tHWRq.Abort();
}
catch (Exception err)
{
throw err;
}
return false;
}
}
}
Web爬虫的C#请求发送的更多相关文章
- 第三百二十二节,web爬虫,requests请求
第三百二十二节,web爬虫,requests请求 requests请求,就是用yhthon的requests模块模拟浏览器请求,返回html源码 模拟浏览器请求有两种,一种是不需要用户登录或者验证的请 ...
- web爬虫,requests请求
requests请求,就是用yhthon的requests模块模拟浏览器请求,返回html源码 模拟浏览器请求有两种,一种是不需要用户登录或者验证的请求,一种是需要用户登录或者验证的请求 一.不需要用 ...
- 一 web爬虫,requests请求
requests请求,就是用python的requests模块模拟浏览器请求,返回html源码 模拟浏览器请求有两种,一种是不需要用户登录或者验证的请求,一种是需要用户登录或者验证的请求 一.不需要用 ...
- 1、web爬虫,requests请求
requests请求,就是用python的requests模块模拟浏览器请求,返回html源码 模拟浏览器请求有两种,一种是不需要用户登录或者验证的请求,一种是需要用户登录或者验证的请求 一.不需要用 ...
- 利用post请求发送内容进行爬虫
利用post请求发送内容进行爬虫 import requests url = 'http://www.iqianyue.com/mypost' header = {} header['Accept-L ...
- 第三百二十七节,web爬虫讲解2—urllib库爬虫—基础使用—超时设置—自动模拟http请求
第三百二十七节,web爬虫讲解2—urllib库爬虫 利用python系统自带的urllib库写简单爬虫 urlopen()获取一个URL的html源码read()读出html源码内容decode(& ...
- python爬虫---scrapy框架爬取图片,scrapy手动发送请求,发送post请求,提升爬取效率,请求传参(meta),五大核心组件,中间件
# settings 配置 UA USER_AGENT = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, l ...
- Web爬去的C#请求发送
public class HttpControler { //post请求发送 private Encoding m_Encoding = Encoding.GetEncoding("gb2 ...
- 第三百三十三节,web爬虫讲解2—Scrapy框架爬虫—Scrapy模拟浏览器登录—获取Scrapy框架Cookies
第三百三十三节,web爬虫讲解2—Scrapy框架爬虫—Scrapy模拟浏览器登录 模拟浏览器登录 start_requests()方法,可以返回一个请求给爬虫的起始网站,这个返回的请求相当于star ...
随机推荐
- gcc 编译流程分析
//test.c #include<stdio.h> int main() { ,y=; printf("x=%d y=%d\n",x,y); ; } 1:预处理阶段, ...
- jQuery开发中容易忽视的错误
1.引用jQuery库文件的<script>标签,必须放在引用自定义脚本文件的<script>标签之前,否则,就会发生找不到对象:最好在<head>元素中,把引入样 ...
- Fiddler是最强大最好用的Web调试工具之一--网站抓包分析
Fiddler 教程 Fiddler是最强大最好用的Web调试工具之一,它能记录所有客户端和服务器的http和https请求,允许你监视,设置断点,甚至修改输入输出数据. 使用Fiddler无论对开发 ...
- 邂逅明下(巴什博弈+hdu2897)
H - 邂逅明下 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Sta ...
- leetcode:Pascal's Triangle
一. 题目 经典题目,杨辉三角,输入行数.生成杨辉三角的数组. 二. 分析 首先,我们知道有例如以下规律: 1.每一行的第一个数和最后一个数都为1 2.中间的数是上面数和上面数左边的数 ...
- Java凝视模板
设置凝视模板的入口: Window->Preference->Java->Code Style->Code Template 然后展开Comments节点就是全部需设置凝 ...
- ThinkPHP项目笔记之RBAC(权限)上篇
当理清这5个表的关系,接下来,就是功能介绍了.
- Java8:使用 Optional 处理 null
写过 Java 程序的同学,一般都遇到过 NullPointerException :) —— 为了不抛出这个异常,我们便会写如下的代码: User user = getUserById(id); i ...
- linux 远端执行shell脚本 批量结束各个远端节点进程
#以下是一个本地的shell脚本,用于同时重启远端多台服务器tomcat服务 #!/bin/sh# ancyshi 重新启动节点tomcat服务function restartNodeTomcatSe ...
- WinForm------如何打开子窗体的同时关闭父窗体
方法: 如何打开子窗体的同时关闭父窗体 this.Hide(); new Frm_Management().ShowDialog(); this.Close();