c# 模拟http post 带cookie
下面的代码是自动向cnblogs中的小组发帖.........注意小组ID,主题ID,小组类型
首先采用firebug分析到发帖时的post地址以及参数,其中在headers中包含了cookies,把cookies复制下来放到d:\\cookie.txt中,以分号隔开(a=b;c=d;),这些cookie中包含有身份验证信息
之后读取文件d:\\data.txt,每1024个字节为一帖自动发送
代码
Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/
-->using System;
using System.Collections.Generic;
using System.Web;
using System.Xml;
using System.IO;
using System.Text;
using System.Collections;
using System.Net;
using System.Threading;
namespace Haier1
{
class Program
{
public static Hashtable getCookieMap()
{
string sPath = "d:\\cookie.txt";
Hashtable maps = new Hashtable();
FileStream fs = new FileStream(sPath, FileMode.Open);
StreamReader rf = new StreamReader(fs, System.Text.Encoding.GetEncoding("gb2312"));
string hm = "";
try
{
do
{
hm = rf.ReadLine(); } while (!rf.EndOfStream);
Console.WriteLine(hm);
String[] s1 = hm.Split(';');
// Console.Write(s1.Length);
for (int i = 0; i < s1.Length; i++)
{
int pos = s1[i].IndexOf('=');
String value = s1[i].Substring(pos + 1);
String name = s1[i].Substring(0, pos);
name = name.Trim();
//Console.WriteLine(name + ":" +value);
maps.Add(name, value);
}
}
catch (Exception e)
{
Console.WriteLine("读取文件错误:" + e.Message);
return null;
}
fs.Close();
rf.Close();
return maps;
}
public static bool test(string str, Hashtable maps)
{
bool ok = false;
string content = "{\"threadId\": \"39369\", \"groupId\": \"101419\", \"groupType\": \"3\", \"title\": \"code\", \"content\": \"" + str + "\"}";
//Console.WriteLine(content);
string url = "http://home.cnblogs.com/WebService/GroupService.asmx/AddThreadComment";
string host = "http://home.cnblogs.com";
try
{
byte[] bs = Encoding.ASCII.GetBytes(content);
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
req.Method = "POST";
req.ContentType = "application/json;charset=utf-8";
req.ContentLength = bs.Length;
CookieContainer cc = new CookieContainer(); cc.Add(new Uri(host), new Cookie("cnzz_a1708446", maps["cnzz_a1708446"].ToString()));
cc.Add(new Uri(host), new Cookie("ASP.NET_SessionId", maps["ASP.NET_SessionId"].ToString()));
cc.Add(new Uri(host), new Cookie(".DottextCookie", maps[".DottextCookie"].ToString()));
req.CookieContainer = cc;
using (Stream reqStream = req.GetRequestStream())
{
reqStream.Write(bs, 0, bs.Length); }
StringBuilder sb = new StringBuilder("");
using (WebResponse wr = req.GetResponse())
{
System.IO.Stream respStream = wr.GetResponseStream();
System.IO.StreamReader reader = new System.IO.StreamReader(respStream, System.Text.Encoding.GetEncoding("gb2312"));
// int h = 0;
string t = "";
do
{
t = reader.ReadLine();
//这个地方自己搞定吧,简单地写了一下成功与否
ok = true;
} while (!reader.EndOfStream);
}
return ok;
}
catch (Exception ex)
{
Console.WriteLine("异常在getPostRespone:" + ex.Source + ":" + ex.Message);
return ok;
}
} static void Main(string[] args)
{ int maxByte=1024;
bool isDebug=false;
if(args.Length>=2){ maxByte = Int32.Parse(args[0]);
if (args[1] == "debug")
isDebug = true;
} Hashtable maps = getCookieMap();
try
{
string sPath = "d:\\data.txt";
FileStream fs = new FileStream(sPath, FileMode.Open);
StreamReader rf = new StreamReader(fs, System.Text.Encoding.GetEncoding("gb2312"));
string postStr = "";
string temp;
try
{
int i = 0;
do
{
temp = rf.ReadLine(); postStr += temp; }
while (!rf.EndOfStream);
int len = postStr.Length; for(i=0;i<len/maxByte+1;i++)
{
if (i * maxByte + maxByte >= len)
{
//Console.WriteLine(postStr.Substring(i * maxByte, len - i * maxByte));
if(test(i+":"+postStr.Substring(i * maxByte, len - i * maxByte),maps))
Console.WriteLine("post ok:"+i);
if (isDebug) Console.ReadLine();
}
else {
//Console.WriteLine(postStr.Substring(i * maxByte, maxByte));
if(test(i+":"+postStr.Substring(i * maxByte, maxByte),maps)){
Console.WriteLine("post ok:"+i);
if (isDebug) Console.ReadLine();
} }
} }
catch (Exception e)
{
Console.WriteLine("读取文件错误:" + e.Message);
return;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message + "----" + ex.Source);
} Console.WriteLine("over!");
Console.ReadLine();
} }
}
c# 模拟http post 带cookie的更多相关文章
- PostMan 使用Interceptor 发送带cookie的请求一直loading
问题 最近要写一个爬虫(虽然是第一次写),于是就用了Chrome上非常方便一个插件,PostMan,但是由于chrome安全的限制,发不出带cookie和带有自定义头部标签的请求. 百度一番后得如果想 ...
- HTTPClient实现免登陆请求(带cookie请求)
背景: 使用httpClient请求某登录型网站,模拟一个操作,一般步骤一个httpclient模式登录->httpClient模拟操作: 此时发现,每次操作都需要进行一次登录,极其浪费时间,是 ...
- postman发送带cookie的http请求
1:需求:测试接口的访问权限,对于某些接口A可以访问,B不能访问. 2:问题:对于get请求很简单,登录之后,直接使用浏览器访问就可以: 对于post请求的怎么测试呢?前提是需要登录态,才能访问接口. ...
- HttpHelps类,用来实现Http访问,Post或者Get方式的,直接访问,带Cookie的,带证书的等方式,可以设置代理
原文地址:http://blog.csdn.net/cdefg198/article/details/8315438 万能框架:http://www.sufeinet.com/forum.php?mo ...
- HttpHelpers类普通GET和POST方式,带Cookie和带证书验证模式
HttpHelpers类普通GET和POST方式,带Cookie和带证书验证模式 参考路径:https://www.cnblogs.com/splendidme/archive/2011/09/14/ ...
- postman 请求带cookie
以亚马逊为例,我抓包随便看一个返回是json数据格式的一个接口,比如随便点一个,我的订单 随便找一条,然后复制url过滤 右键,copy下url 将url放入filter过滤: 在postman里面, ...
- 【NOI2013模拟】坑带的树(仙人球的同构+圆方树乱搞+计数+HASH)
[NOI2013模拟]坑带的树 题意: 求\(n\)个点,\(m\)条边的同构仙人球个数. \(n\le 1000\) 这是一道怎么看怎么不可做的题. 这种题,肯定是圆方树啦~ 好,那么首先转为广义圆 ...
- QtQuick大坑笔记之Http的Get与Post操作(带cookie)
前言 最近在为单位做一个简单的手机App,基于Qt技术栈的选择了QtQuick来开发.不得不说QtQucik开发的确舒服,很多东西都不用写就可以只用,UI定义起来也比较自由.但是本人想通过cookie ...
- urllib2 post请求方式,带cookie,添加请求头
#encoding = utf-8 import urllib2import urllib url = 'http://httpbin.org/post'data={"name": ...
随机推荐
- Struts2文件下载浅析
Struts2极大的简化了文件上传和下载,本文将主要介绍一下Struts2文件下载的实现1.功能主要是,在下载页面点击下载后,则下载相应的文件 2.代码部分jsp页面downloadPage:< ...
- 在Mac OS X中使用VIM开发STM32(2)
本文原创于http://www.cnblogs.com/humaoxiao,非法转载者请自重! 在我先前的博文⎣在Mac OS X中使用VIM开发STM32(1)⎤中,我们安装完成了MACVIM,这一 ...
- struct timespec 和 struct timeval
time()提供了秒级的精确度 . 1.头文件 <time.h> 2.函数原型 time_t time(time_t * timer) 函数返回从TC1970-1-1 0:0:0开始到现在 ...
- 解决laravel中环境配置不起作用的方法
博客已经迁移到www.imyzf.com,本站不再更新,请谅解! laravel有个环境配置选项很好用,在bootstrap/start.php中,曾经百度到这里面加入域名,就可以自动选择环境 $en ...
- dataTable 禁止排序
$("#id").DataTable({ "ordering": false, // 禁止排序 });
- PHP 向 MySql 中数据修改操作时,只对数字操作有效,非数字操作无效,怎么办?
问题描述: 用PHP向MySql数据库中修改数据,实现增删改(数据库能正确连接) 经测试,代码只能对数字进行正常的增删改操作,非数字操作无效 但要在课程名称中输入中文,应该如果修改呢? 存 ...
- linux awk命令学习
. awk的运行过程 ) awk_script的组成: ① awk_script可以由一条或多条awk_cmd组成,两条awk_cmd之间一般以NEWLINE分隔 ② awk_cmd由两部分组成: a ...
- 【加密】RSA加密之算法
RSA公钥加密算法是1977年由Ron Rivest.Adi Shamirh和LenAdleman在(美国麻省理工学院)开发的. RSA算法是一种非对称密码算法,所谓非对称,就是指该算法需要一对密钥, ...
- 3、MapReduce
MapReduce是一种处理海量数据的并行编程模型和计算框架,用于对大数据及的并行计算. 1.MapReduce基础 1)MapReduce处理数据集过程,如下图: a)Map阶段: MapReduc ...
- Substring的简单使用
string myString = "测试一下函数Substring()是怎么用的"; //Substring()在C#中有两个重载函数 //分别如下示例 //如果参数为一个长整数 ...