小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

c#模擬http post 帶cookie-程序開(kāi)發(fā)-紅黑聯(lián)盟

 大卷風(fēng) 2010-10-06
c#模擬http post 帶cookie
文章錄入:王子    責(zé)任編輯:dingkai1983  132

【字體:

下面的代碼是自動(dòng)向cnblogs中的小組發(fā)帖.........注意小組ID,主題ID,小組類(lèi)型
首先采用firebug分析到發(fā)帖時(shí)的post地址以及參數(shù),其中在headers中包含了cookies,把cookies復(fù)制下來(lái)放到d:\\cookie.txt中,以分號(hào)隔開(kāi)(a=b;c=d;),這些cookie中包含有身份驗(yàn)證信息
之后讀取文件d:\\data.txt,每1024個(gè)字節(jié)為一帖自動(dòng)發(fā)送
 
代碼
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(
"讀取文件錯(cuò)誤:" + 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();
                        //這個(gè)地方自己搞定吧,簡(jiǎn)單地寫(xiě)了一下成功與否

                        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(
"讀取文件錯(cuò)誤:" + e.Message);

                    
return;

                }






            }

            
catch (Exception ex)
            {
                Console.WriteLine(ex.Message 
+ "----" + ex.Source);

            }


            Console.WriteLine(
"over!");
            Console.ReadLine();
        }


    }
}

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類(lèi)似文章 更多