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#请求发送的更多相关文章

  1. 第三百二十二节,web爬虫,requests请求

    第三百二十二节,web爬虫,requests请求 requests请求,就是用yhthon的requests模块模拟浏览器请求,返回html源码 模拟浏览器请求有两种,一种是不需要用户登录或者验证的请 ...

  2. web爬虫,requests请求

    requests请求,就是用yhthon的requests模块模拟浏览器请求,返回html源码 模拟浏览器请求有两种,一种是不需要用户登录或者验证的请求,一种是需要用户登录或者验证的请求 一.不需要用 ...

  3. 一 web爬虫,requests请求

    requests请求,就是用python的requests模块模拟浏览器请求,返回html源码 模拟浏览器请求有两种,一种是不需要用户登录或者验证的请求,一种是需要用户登录或者验证的请求 一.不需要用 ...

  4. 1、web爬虫,requests请求

    requests请求,就是用python的requests模块模拟浏览器请求,返回html源码 模拟浏览器请求有两种,一种是不需要用户登录或者验证的请求,一种是需要用户登录或者验证的请求 一.不需要用 ...

  5. 利用post请求发送内容进行爬虫

    利用post请求发送内容进行爬虫 import requests url = 'http://www.iqianyue.com/mypost' header = {} header['Accept-L ...

  6. 第三百二十七节,web爬虫讲解2—urllib库爬虫—基础使用—超时设置—自动模拟http请求

    第三百二十七节,web爬虫讲解2—urllib库爬虫 利用python系统自带的urllib库写简单爬虫 urlopen()获取一个URL的html源码read()读出html源码内容decode(& ...

  7. python爬虫---scrapy框架爬取图片,scrapy手动发送请求,发送post请求,提升爬取效率,请求传参(meta),五大核心组件,中间件

    # settings 配置 UA USER_AGENT = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, l ...

  8. Web爬去的C#请求发送

    public class HttpControler { //post请求发送 private Encoding m_Encoding = Encoding.GetEncoding("gb2 ...

  9. 第三百三十三节,web爬虫讲解2—Scrapy框架爬虫—Scrapy模拟浏览器登录—获取Scrapy框架Cookies

    第三百三十三节,web爬虫讲解2—Scrapy框架爬虫—Scrapy模拟浏览器登录 模拟浏览器登录 start_requests()方法,可以返回一个请求给爬虫的起始网站,这个返回的请求相当于star ...

随机推荐

  1. 安全 流程服务器开新机器 内外网 iptables 安全组 用户安全root用户的使用.

    安全    流程服务器开新机器      内外网      iptables   安全组       用户安全root用户的使用.

  2. 面试常问小知识点之Integer

    背景 今天在查看Sonar的时候发现小伙伴在某些场景下如下使用 很明显sonar已经报错了,但是线上应用目前是正常的 问题 事实上经常会有面试的小伙伴或者笔试的小伙伴问这个问题 Integer的一些小 ...

  3. 【BZOJ】1061: [Noi2008]志愿者招募(费用流+数学)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1061 好神的一题! 学会了一种建模方式: 当方程组内的任意变量都在其中两个方程出现且一正一负,可以建 ...

  4. 【BZOJ】1639: [Usaco2007 Mar]Monthly Expense 月度开支(二分)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1639 同tyvj1359,http://www.cnblogs.com/iwtwiioi/p/394 ...

  5. php计算数组相同值出现次数的代码(array_count_values)

    php计算数组相同值出现次数,可以使用php自带函数array_count_values : 说明 array array_count_values ( array $input )array_cou ...

  6. SQL命令优化(积累)

    与数据库交互的基本语言是sql,数据库每次解析和执行sql语句多需要执行很多步骤.以sql server为例,当数据库收到一条查询语句时,语法分析器会扫描sql语句并将其分成逻辑单元(如关键词.表达式 ...

  7. LoadRunner学习---脚本编写(4)(比较重要)

    今天接着来翻译http://www.wilsonmar.com/中关于LoadRunner脚本编写部分,下面该翻译脚本编写中一些比较重要的部分了. Web用户Action 在VuGen中,脚本产生的默 ...

  8. 【NLP+Deep learning】好文

    http://blog.jobbole.com/77709/ 原文出处: http://colah.github.io/posts/2014-07-NLP-RNNs-Representations/

  9. iOS开发之--改变系统导航的颜色,字体,还有返回样式的自定义

    在写项目的工程中,我们可能会遇到各种各样的项目,写的方法也是各有不同,不喜欢自定义的小伙伴也很多, 下面我就记录下系统导航和barbuttonitem的修改系统空间的方法: 1,添加rightbarb ...

  10. NUC972 MDK NON-OS

     NUC972直接可以在BSP包里模板进行编程,烧录用Nu writer  http://www2.keil.com/mdk5/legacy 下载对应的安装包的插件 是直接下载到DDR 里面去运行,所 ...