设为首页 加入收藏

TOP

socket实现WordPress博客自动发文系列之登录 (一)
2014-11-23 22:30:33 来源: 作者: 【 】 浏览:1
Tags:socket 实现 WordPress 博客 自动 发文 系列 登录

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

公司需求以后要能从自己的文章资源平台,选择文章发布到wordpress站群,所以需要一个自动发布文章的小功能。工作之余发布到园子里和大家分享下。

之前尝试用httpwebrequest 对象方式去实现但发现有cookies接收不全的现象,所以改用socket 模拟http post请求去实现,代码写的很乱,只是初步的探索,也参考了很多园子里前辈的代码。小小AD下:
.net技术研究QQ群( 41050480)
合肥软件开发技术联盟(31065717)
非常渴望和大家一起交流!

\

主要几个方法:

1 ///


2 /// 带上cookies 获取需要登录验证的页面
3 ///

4 /// 请求的URL
5 /// cookies字符串
6 /// 页面编码
7 ///
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 ///


2 ///
3 ///

4 /// 登录地址
5 /// 发送的字符串
6 /// 网页编码
7 ///
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, P
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C语言学习趣事_数据结构_经典命题.. 下一篇lookupedit的一般用法

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: