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 ...
随机推荐
- mysql中创建用户和赋权限
mysql命令行用的不多,大部分使用工具类替代,所以这里记录下命令行模式下创建用户和赋予权限的命令,不用每次麻烦百度. 1. 创建oozie用户,%符号表示仅限于远程登录 create user 'o ...
- grafana里prometheus查询语法
名称 描述 label_values(label) 返回label每个指标中的标签值列表. label_values(metric, label) 返回label指定度量标准中的标签值列表. metr ...
- boost 互斥体和锁
1.共享资源是一个自动锁住的房间,互斥体是钥匙,进入房间必须取钥匙,离开房间应该还钥匙.这就对应着互斥体的lock(取钥匙)和unlock(还钥匙). 2.考虑下面的场景:还钥匙的时候出现异常,会发生 ...
- linux -- Ubuntu14.04及之后版本重启网卡不生效
Ubuntu14.04修改配置,重启网卡没有生效,出现如下问题: service networking restart //重启网络服务 stop: Job failed while stopping ...
- 【BZOJ】1068: [SCOI2007]压缩(dp)
http://www.lydsy.com/JudgeOnline/problem.php?id=1068 发现如果只设一维的话无法转移 那么我们开第二维,发现对于前i个来说,如果确定了M在哪里,第i个 ...
- 【BZOJ】1681: [Usaco2005 Mar]Checking an Alibi 不在场的证明(spfa)
http://www.lydsy.com/JudgeOnline/problem.php?id=1681 太裸了.. #include <cstdio> #include <cstr ...
- jQuery编程中的一些核心方法简介
调用 jQuery 对象的方法很简单: $('h1').remove(); 大多数 jQuery 方法都是像上面这样被调用的,这些方法都位于 $.fn 命名空间内,这些方法称为 jQuery 对象方法 ...
- django admin后台css样式丢失
尼玛 坑爹啊 怎么光秃秃的,跟人家的不一样啊 打开firebug 发现报错,找不到css 通过google找到原因,是因为admin所需的js ,css等静态文件虽然都在django的安装目录内,但是 ...
- 简单配置Nginx 指向本地端口,并开启SSL
简单配置Nginx 指向本地端口,并开启SSL,如果要开启SSL,必须使用域名去申请SSL key,一般是两个文件,一般是要收费的. # 在/etc/nginx/nginx.conf 的文件中有下面一 ...
- Angular2 HttpClient (一)
@angular/common/http 中的 HttpClient 类为 Angular 应用程序提供了一个简化的 API 来实现 HTTP 客户端功能.它基于浏览器提供的 XMLHttpReque ...