Logging on to the WordPress blog automatic publishing series through socket

Source: Internet
Author: User
Tags wordpress blog

If you have no time to write an article in detail, simply record it and share it with you. This method can be extended to the second kill. It only requires different analysis for different websites.

The company needs to be able to choose to publish articles to the wordpress site group from its own article Resource Platform, so it needs a small function to automatically publish articles. I will share my work with you in the garden.

Previously, I tried to use the httpwebrequest object method, but found that cookies were not fully received. So I used socket to simulate http post requests. The code was messy, but it was just a preliminary exploration, also refer to the code of many predecessors in the garden. Under Small AD:
. Net Technology Research QQ group (41050480)
Hefei software development technology League (31065717)
I am very eager to communicate with you!

 

 

Main Methods:

1 /// <summary>
2 // use cookies to obtain the page for Logon Verification
3 /// </summary>
4 /// <param name = "url"> request URL </param>
5 // <param name = "cookies"> cookie string </param>
6 /// <param name = "encoding"> page Code </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; // used to save the obtained HTML code
12 IPHostEntry gist = Dns. GetHostEntry (URI. Host); // obtain the IP address of the current URL
13 IPAddress ip = gist. AddressList [0]; // extract the ip address
14 IPEndPoint ipEnd = new IPEndPoint (ip, 80); // encapsulate the ip address and port
15 Socket socket = new Socket (AddressFamily. InterNetwork, SocketType. Stream, ProtocolType. Tcp); // instantiate the Stock
16 try
17 {
18 socket. Connect (ipEnd );
19} // Automatic loop capture connection
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 ";
28 byte [] MS = UTF8Encoding. GetEncoding (encoding). GetBytes (sendString); // convert the header to a byte form
29 socket. Send (MS); // Send
30 int recv =-1; // defines the length of the accepted data
31 byte [] data = new byte [1024]; // used to save the received data
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}
 

Httpwebrequest is used before post login in socket mode, but cannot log on at all times because cookies are not accepted completely.

 

1 /// <summary>
2 ///
3 /// </summary>
4 // <param name = "postURL"> logon address </param>
5 /// <param name = "postString"> sent string </param>
6 /// <param name = "encoding"> webpage encoding </param>
7 /// <returns> </returns>
8 public string PostData (string postURL, string postString, string encoding)
9 {
10 string strHTML = ""; // used to save the obtained HTML code
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 ";
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 = "failed to link host ";
29}
30 s. Send (ByteGet, ByteGet. Length, SocketFlags. None );
31 strHTML = Recv (s, Encoding. GetEncoding (encoding ));
32 return strHTML;
33}
 

Handle cookies and redirection issues

/// <Summary>
/// Extract cookies from the returned source code and redirect from 301 or 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 ();
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.