程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> socket實現WordPress博客自動發文系列之登錄

socket實現WordPress博客自動發文系列之登錄

編輯:關於C語言

沒有時間詳細的寫文章了,就隨便記錄並且分享一下。該方法能擴展到秒殺器哦。只是針對不同的網站 需要不同的分析而已。

公司需求以後要能從自己的文章資源平台,選擇文章發布到wordpress站群,所以需要一個自動發布文章的小功能。工作之余發布到園子裡和大家分享下。

之前嘗試用httpwebrequest 對象方式去實現但發現有cookies接收不全的現象,所以改用socket 模擬http post請求去實現,代碼寫的很亂,只是初步的探索,也參考了很多園子裡前輩的代碼。小小AD下:
.net技術研究QQ群(  41050480)
合肥軟件開發技術聯盟(31065717)
非常渴望和大家一起交流!

\

 

 

主要幾個方法:

 1 /// <summary>
 2         /// 帶上cookies 獲取需要登錄驗證的頁面
 3         /// </summary>
 4         /// <param name="url">請求的URL</param>
 5         /// <param name="cookies">cookies字符串</param>
 6         /// <param name="encoding">頁面編碼</param>
 7         /// <returns></returns>
 8         public string GetPage(string url, string cookies, string encoding)
 9         {
10             Uri URI = new Uri(url);
11             string strHTML = string.Empty;//用來保存獲得的HTML代碼
12             IPHostEntry gist = Dns.GetHostEntry(URI.Host);//獲得當前URL的IP地址
13             IPAddress ip = gist.AddressList[0];//提取IP地址
14             IPEndPoint ipEnd = new IPEndPoint(ip, 80);//封裝IP地址和端口
15             Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//實例化Stock
16             try
17             {
18                 socket.Connect(ipEnd);
19             }//自動循環捕捉連接
20             catch
21             { }
22             string sendString = "GET " + URI.PathAndQuery + " HTTP/1.1\r\n";
23             sendString += "Connection:close\r\n";
24             sendString += "Content-Type: application/x-www-form-urlencoded\r\n";
25             sendString += "Host:" + URI.Host + "\r\n";
26             if (!string.IsNullOrEmpty(cookies))
27                 sendString += "Cookie:" + cookies + "\r\n\r\n";
28             byte[] ms = UTF8Encoding.GetEncoding(encoding).GetBytes(sendString);//將頭部轉換成byte形式
29             socket.Send(ms);//發送
30             int recv = -1;//定義接受數據長度
31             byte[] data = new byte[1024];//用來保存接收數據
32             do
33             {
34                 recv = socket.Receive(data);
35                 strHTML += Encoding.GetEncoding(encoding).GetString(data, 0, recv);
36             } while (recv != 0);
37             return strHTML;
38         }
 

socket方式post 登錄 之前用httpwebrequest方式 但始終登錄不了,原因是cookies接受不全,就改用socket方式 自行處理cookies

 

 1 /// <summary>
 2         ///
 3         /// </summary>
 4         /// <param name="postURL">登錄地址</param>
 5         /// <param name="postString">發送的字符串</param>
 6         /// <param name="encoding">網頁編碼</param>
 7         /// <returns></returns>
 8         public string PostData(string postURL,string postString, string encoding)
 9         {
10             string strHTML = "";//用來保存獲得的HTML代碼
11             Uri URI = new Uri(postURL);
12             string sendString;
13             sendString = "POST {0} HTTP/1.1\r\n";
14             sendString += "Host: {1}\r\n";
15             sendString += "User-Agent:Mozilla/5.0 (Windows NT 6.1; rv:5.0) Gecko/20100101 Firefox/5.0\r\n";
16             sendString += "Content-Type:application/x-www-form-urlencoded\r\n";
17             sendString += "Content-Length:{2}\r\n";
18             sendString += "Connection:close\r\n\r\n";
19             sendString += "{3}\r\n";
20             sendString = string.Format(sendString, URI.PathAndQuery, URI.Host, postString.Length, postString);
21             Byte[] ByteGet = Encoding.GetEncoding(encoding).GetBytes(sendString);
22             IPAddress hostadd = Dns.GetHostEntry(URI.Host).AddressList[0];
23             IPEndPoint EPhost = new IPEndPoint(hostadd, 80);
24             Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
25             s.Connect(EPhost);
26             if (!s.Connected)
27             {
28                 strHTML = "鏈接主機失敗";
29             }
30             s.Send(ByteGet, ByteGet.Length, SocketFlags.None);
31             strHTML = Recv(s, Encoding.GetEncoding(encoding));
32             return strHTML;
33         }
 

處理cookies以及重定向問題

    /// <summary>
        /// 從返回的源代碼中提取cookies 以及301或302跳轉
         /// </summary>
         /// <param name="s"></param>
         /// <param name="location"></param>
         /// <returns></returns>
                                                         
        public string GetCookies(string html, out string location)
        {
            StringBuilder sbCookies = new StringBuilder();
            location = string.Empty;
            string[] arr = html.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
            foreach (string str in arr)
            {
                if (str.StartsWith("Set-Cookie: "))
                {
                    int intStart = str.IndexOf(";");
                    string strCookie = str.Substring(12, intStart - 11);
                    sbCookies.Append(strCookie);
                }
                if (str.StartsWith("Location:"))
                {
                    location = str.Substring(10);
                }
            }
            return sbCookies.ToString();
        }   

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved