Use socket to simulate http requests and socket to simulate requests

Source: Internet
Author: User

Use socket to simulate http requests and socket to simulate requests

Using System; using System. collections. generic; using System. linq; using System. net; using System. net. sockets; using System. text; using System. threading. tasks; class HttpHelper {# region simulate client socket connection private static Socket ConnectSocket (string server, int port) {Socket s = null; IPHostEntry hostEntry = null; // Get host related information. hostEntry = Dns. getHostEntry (server); // Loop through the AddressList to obtain the supported AddressFamily. this is to avoid // an exception that occurs when the host IP Address is not compatible with the address family // (typical in the IPv6 case ). foreach (IPAddress address in hostEntry. addressList) {IPEndPoint ipe = new IPEndPoint (address, port); Socket tempSocket = new Socket (ipe. addressFamily, SocketType. stream, ProtocolType. tcp); tempSocket. connect (ipe); if (tempSocket. connected) {s = tempSocket; break;} else {continue ;}}return s ;}# endregion # The main method of the region request is the header of the http request, you can use the packet capture tool to obtain the domain name or IP address of the server. The port http protocol is generally 80 public static string SocketSendReceive (string request, string server, int port) {try {Byte [] bytesSent = Encoding. ASCII. getBytes (request); Byte [] bytesReceived = new Byte [655350]; // create a connection Socket s = ConnectSocket (server, port); if (s = null) return ("Connection failed"); // sends the content. s. send (bytesSent, bytesSent. length, 0); // Receive the server home page content. int bytes = 0; string page = "Default HTML page on" + server + ": \ r \ n"; // accept the returned content. do {bytes = s. receive (bytesReceived, bytesReceived. length, 0); page = page + Encoding. UTF8.GetString (bytesReceived, 0, bytes);} while (bytes> 0); return page;} catch {return string. empty ;}# endregion}

  

Using System; using System. collections. generic; using System. IO; using System. linq; using System. text; using System. threading. tasks; using System. reflection; class Program {public static string HeadlerInit () {StringBuilder sb = new StringBuilder (); sb. appendLine ("GET http://www.baidu.com/ HTTP/1.1 "); sb. appendLine ("Host: www.baidu.com"); sb. appendLine ("Connection: keep-alive"); sb. appendLine ("Accept: text/html, application/xhtml + xml, application/xml; q = 0.9, image/webp, */*; q = 0.8"); sb. appendLine ("User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.90 Safari/537.36"); sb. appendLine ("Accept-Encoding: deflate, sdch"); sb. appendLine ("Accept-Language: zh-CN, zh; q = 0.8"); sb. appendLine ("\ r \ n"); // this must be done. Otherwise, no data may return sb. toString ();} static void Main (string [] args) {string getStrs = HeadlerInit (); string getHtml = HttpHelper. socketSendReceive (getStrs, "www.baidu.com", 80); Console. writeLine (getHtml );}}

  

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.