[转载]socket 实现WordPress博客自动发文系列 之 登录 – 啊峰 – 博客园.
没有时间详细的写文章了,就随便记录并且分享一下。该方法能扩展到秒杀器哦。只是针对不同的网站 需要不同的分析而已。
公司需求以后要能从自己的文章资源平台,选择文章发布到wordpress站群,所以需要一个自动发布文章的小功能。工作之余发布到园子里和大家分享下。
之前尝试用httpwebrequest 对象方式去实现但发现有cookies接收不全的现象,所以改用socket 模拟http post请求去实现,代码写的很乱,只是初步的探索,也参考了很多园子里前辈的代码。小小AD下:
.net技术研究QQ群( 41050480)
合肥软件开发技术联盟(31065717)
非常渴望和大家一起交流!
主要几个方法:
/// /// 带上cookies 获取需要登录验证的页面 /// /// <span> </span>请求的URL /// cookies字符串 /// 页面编码 /// public string GetPage(string url, string cookies, string encoding) { Uri URI = new Uri(url); string strHTML = string.Empty;//用来保存获得的HTML代码 IPHostEntry gist = Dns.GetHostEntry(URI.Host);//获得当前URL的IP地址 IPAddress ip = gist.AddressList[0];//提取IP地址 IPEndPoint ipEnd = new IPEndPoint(ip, 80);//封装IP地址和端口 Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//实例化Stock try { socket.Connect(ipEnd); }//自动循环捕捉连接 catch { } string sendString = "GET " + URI.PathAndQuery + " HTTP/1.1\r\n"; sendString += "Connection:close\r\n"; sendString += "Content-Type: application/x-www-form-urlencoded\r\n"; sendString += "Host:" + URI.Host + "\r\n"; if (!string.IsNullOrEmpty(cookies)) sendString += "Cookie:" + cookies + "\r\n\r\n"; byte[] ms = UTF8Encoding.GetEncoding(encoding).GetBytes(sendString);//将头部转换成byte形式 socket.Send(ms);//发送 int recv = -1;//定义接受数据长度 byte[] data = new byte[1024];//用来保存接收数据 do { recv = socket.Receive(data); strHTML += Encoding.GetEncoding(encoding).GetString(data, 0, recv); } while (recv != 0); return strHTML; }
socket方式post 登录 之前用httpwebrequest方式 但始终登录不了,原因是cookies接受不全,就改用socket方式 自行处理cookies
/// /// /// /// <span> </span>登录地址 /// 发送的字符串 /// 网页编码 /// public string PostData(string postURL,string postString, string encoding) { string strHTML = "";//用来保存获得的HTML代码 Uri URI = new Uri(postURL); string sendString; sendString = "POST {0} HTTP/1.1\r\n"; sendString += "Host: {1}\r\n"; sendString += "User-Agent:Mozilla/5.0 (Windows NT 6.1; rv:5.0) Gecko/20100101 Firefox/5.0\r\n"; sendString += "Content-Type:application/x-www-form-urlencoded\r\n"; sendString += "Content-Length:{2}\r\n"; sendString += "Connection:close\r\n\r\n"; sendString += "{3}\r\n"; sendString = string.Format(sendString, URI.PathAndQuery, URI.Host, postString.Length, postString); Byte[] ByteGet = Encoding.GetEncoding(encoding).GetBytes(sendString); IPAddress hostadd = Dns.GetHostEntry(URI.Host).AddressList[0]; IPEndPoint EPhost = new IPEndPoint(hostadd, 80); Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); s.Connect(EPhost); if (!s.Connected) { strHTML = "链接主机失败"; } s.Send(ByteGet, ByteGet.Length, SocketFlags.None); strHTML = Recv(s, Encoding.GetEncoding(encoding)); return strHTML; }
处理cookies以及重定向问题
/// /// 从返回的源代码中提取cookies 以及301或302跳转 /// /// /// /// 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环境