zoukankan      html  css  js  c++  java
  • socket 实现WordPress博客自动发文系列 之 登录

    没有时间详细的写文章了,就随便记录并且分享一下。该方法能扩展到秒杀器哦。只是针对不同的网站 需要不同的分析而已。

    公司需求以后要能从自己的文章资源平台,选择文章发布到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();
            }    

    项目打包下载 vs2010环境

    /Files/chjf2008/AutoPostSolution.rar

  • 相关阅读:
    JS/JQuery获取当前元素的上一个/下一个兄弟级元素等元素的方法
    目前流行前端几大UI框架
    C#客户端嵌入Chrome浏览器的实现
    ARM编译中的RO、RW和ZI DATA区段
    如何成为优秀的技术Leader?做到这三点就够了
    VMware虚拟机中的CentOS安装Nginx后本机无法访问的解决办法
    Centos下安装nginx步骤解析
    centos修改、保存文件的详细步骤
    configure: error: C preprocessor "arm-linux-gnueabihf-g++" fails sanity check
    有关fgetc配合feof逐行读取文件最后一行读取两遍的错觉?
  • 原文地址:https://www.cnblogs.com/chjf2008/p/2117270.html
Copyright © 2011-2022 走看看