打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
c#模拟http post 带cookie-程序开发-红黑联盟
c#模拟http post 带cookie
文章录入:王子    责任编辑:dingkai1983  132
【字体:
下面的代码是自动向cnblogs中的小组发帖.........注意小组ID,主题ID,小组类型
首先采用firebug分析到发帖时的post地址以及参数,其中在headers中包含了cookies,把cookies复制下来放到d:\\cookie.txt中,以分号隔开(a=b;c=d;),这些cookie中包含有身份验证信息
之后读取文件d:\\data.txt,每1024个字节为一帖自动发送
代码 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();
}
}
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
c#文件操作
C#读取文本文件和C# 写文本文件
写一个简易web服务器、ASP.NET核心知识(4)
统计一个大文本行数的几种方法以及效率统计(一)
C# WebRequest发起Http Post请求模拟登陆并cookie处理示例
如何逐行读取文本文件 - .NET技术 / C#
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服