C # Use Socket to log on to WordPress source code

Source: Internet
Author: User

1 class loginwp 2 {3 public string PostData (string postURL, string postString, string encoding) 4 {5 string strHTML = ""; // Save the obtained HTML code 6 Uri URI = new Uri (postURL); 7 string sendString; 8 sendString = "POST {0} HTTP/1.1 \ r \ n "; 9 sendString + = "Host: {1} \ r \ n"; 10 sendString + = "User-Agent: Mozilla/5.0 (Windows NT 6.1; rv: 5.0) gecko/20100101 Firefox/5.0 \ r \ n "; 11 sendString + =" Content-Type: applicat Ion/x-www-form-urlencoded \ r \ n "; 12 sendString + =" Content-Length: {2} \ r \ n "; 13 sendString + =" Connection: close \ r \ n "; 14 sendString + =" Cookie: wordpress_test_cookie = WP + Cookie + check \ r \ n "; 15 sendString + = "{3} \ r \ n"; 16 sendString = string. format (sendString, URI. pathAndQuery, URI. host, postString. length, postString); 17 Byte [] ByteGet = Encoding. getEncoding (encoding ). getBytes (sendString); 18 IPAdd Ress hostadd = Dns. getHostEntry (URI. host ). addressList [0]; 19 IPEndPoint EPhost = new IPEndPoint (hostadd, 80); 20 Socket s = new Socket (AddressFamily. interNetwork, SocketType. stream, ProtocolType. tcp); 21 s. connect (EPhost); 22 if (! S. connected) 23 {24 strHTML = "failed to connect to the Host"; 25} 26 s. send (ByteGet, ByteGet. length, SocketFlags. none); 27 strHTML = Recv (s, Encoding. getEncoding (encoding); 28 return strHTML; 29} 30 31 public static String Recv (Socket sock, Encoding encode) 32 {33 Byte [] buffer = new Byte [1024]; 34 StringBuilder sb = new StringBuilder (); 35 36 Thread. sleep (50); // tune 37 Int32 len = sock according to the page response time. receive (buffer); 38 Sb. append (encode. getString (buffer, 0, len); 39 40 while (sock. available> 0) 41 {42 Thread. sleep (300); // You can also fine-tune 43 Array. clear (buffer, 0, buffer. length); 44 len = sock. receive (buffer); 45 sb. append (encode. getString (buffer, 0, len); 46 string ss = encode. getString (buffer, 0, len); 47} 48 sock. close (); 49 return sb. toString (); 50} 51 52 // <summary> 53 // extract cookies from the returned source code and redirect 301 or 302 to 54 /// </Summary> 55 // <param name = "s"> </param> 56 // <param name = "location"> </param> 57 /// <returns> </returns> 58 public string GetCookies (string html, out string location) 59 {60 StringBuilder sbCookies = new StringBuilder (); 61 location = string. empty; 62 string [] arr = html. split (new string [] {"\ r \ n"}, StringSplitOptions. removeEmptyEntries); 63 foreach (string str in arr) 64 {65 if (str. star TsWith ("Set-Cookie:") 66 {67 int intStart = str. indexOf (";"); 68 string strCookie = str. substring (12, intStart-11); 69 sbCookies. append (strCookie); 70} 71 if (str. startsWith ("Location:") 72 {73 location = str. substring (10); 74} 75} 76 return sbCookies. toString (); 77} 78 79 // <summary> 80 // bring cookies to get the page 81 that requires logon verification /// </summary> 82 // <param name = "url "> request URL </param> 83 // <param Name = "cookies"> cookie string </param> 84 // <param name = "encoding"> page Code </param> 85 // <returns> </returns> 86 public string GetPage (string url, string cookies, string encoding) 87 {88 Uri URI = new Uri (url); 89 string strHTML = string. empty; // used to save the obtained HTML code 90 IPHostEntry gist = Dns. getHostEntry (URI. host); // obtain the ip address of the current url 91 IPAddress ip = gist. addressList [0]; // extract IP address 92 IPEndPoint ipEnd = new IPEndPoi Nt (ip, 80); // encapsulate the ip address and port 93 Socket socket = new Socket (AddressFamily. interNetwork, SocketType. stream, ProtocolType. tcp); // instantiate Stock 94 try 95 {96 socket. connect (ipEnd); 97} // Automatic loop capture connection 98 catch 99 {} 100 string sendString = "GET" + URI. pathAndQuery + "HTTP/1.1 \ r \ n"; 101 sendString + = "Connection: close \ r \ n"; 102 sendString + = "Content-Type: application/x-www-form-urlencoded \ r \ n "; 103 sendString + =" Hos T: "+ URI. Host +" \ r \ n "; 104 if (! String. isNullOrEmpty (cookies) 105 sendString + = "Cookie:" + cookies + "\ r \ n"; 106 byte [] MS = UTF8Encoding. getEncoding (encoding ). getBytes (sendString); // convert the header to a byte 107 socket. send (MS); // Send 108 int recv =-1; // define accept data Length 109 byte [] data = new byte [1024]; // used to save the received data 110 do111 {112 recv = socket. receive (data); 113 strHTML + = Encoding. getEncoding (encoding ). getString (data, 0, recv); 114} while (recv! = 0); 115 return strHTML; 116} 117}

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.