PostDirectoryFiles//HttpWebRequest,HttpWebResponse 本是古典 何须时尚 2022-07-16 08:50 162阅读 0赞 using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Net; namespace PostDirectoryFiles//HttpWebRequest,HttpWebResponse \{ class Program \{ private static CookieContainer cookiecontainer=new CookieContainer(); private static Uri loginUrl = new Uri(@"[http://.net//message/login/login\_option.php][http_.net_message_login_login_option.php]"); static int inited = 0; private static Uri postDataUrl = new Uri(@"[http://.net//message/user/add\_option.php][http_.net_message_user_add_option.php]"); private static String ReadFile(string FilePath) \{ String strFileContent = System.IO.File.ReadAllText(FilePath); return strFileContent; \} static void Main(string\[\] args) \{ if (args.Length != 1) \{ Console.WriteLine("PostDirectoryFiles targetDirectory"); return; \} String strDirectoryPath = args\[0\]; DirectoryInfo theFolder = new DirectoryInfo(strDirectoryPath);//@"F:\\a\\" if (theFolder.Exists) \{ FileInfo\[\] fileInfo = theFolder.GetFiles(); foreach (FileInfo NextFile in fileInfo) //遍历文件 \{ //Console.WriteLine(NextFile.Name); String filePath; if ('\\\\' == strDirectoryPath\[strDirectoryPath.Length - 1\]) \{ filePath = strDirectoryPath + NextFile.Name; \} else \{ filePath = strDirectoryPath + "\\\\" + NextFile.Name; \} String strFileContent = ReadFile(filePath); Console.WriteLine("\{0\}:\\n\{1\}", NextFile.Name, strFileContent); PostFile(NextFile.Name,strFileContent); \} \} \} private static void PostFile(string filename, string strFileContent) \{ string postData=null; if (inited == 0) \{ postData = string.Format("name=www&pwd=www&Submit=%CC%E1%BD%BB"); inited += 1; \} else \{ postData = string.Format("mestitle=\{0\}&mesbody=\{1\}&Submit=%CC%E1%BD%BB", filename, System.Web.HttpUtility.UrlEncode(strFileContent)); \} //这是cnblogs提交时的一些参数 ASCIIEncoding encoding = new ASCIIEncoding(); byte\[\] data = encoding.GetBytes(postData); HttpWebRequest request = null; if (inited == 1) \{ request = (HttpWebRequest)WebRequest.Create(loginUrl); inited += 1; \} else \{ request = (HttpWebRequest)WebRequest.Create(postDataUrl); \} request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded";// request.ContentLength = data.Length; request.KeepAlive = true; request.CookieContainer = cookiecontainer; //返回的cookie会附加在这个容器里面 //发送数据 Stream newStream = request.GetRequestStream(); newStream.Write(data, 0, data.Length); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); newStream = response.GetResponseStream(); StreamReader reader = new StreamReader(newStream, Encoding.UTF8); string text = reader.ReadToEnd(); if (text.Contains("<script")) \{ Console.WriteLine(filename); \} else \{ Console.WriteLine(filename+" ......"); Console.WriteLine(text); \} reader.Close(); newStream.Close(); if (inited == 2) \{ inited += 1; PostFile(filename, strFileContent); \} \} \} \} [http_.net_message_login_login_option.php]: http://.net//message/login/login_option.php [http_.net_message_user_add_option.php]: http://.net//message/user/add_option.php
还没有评论,来说两句吧...