HTTP protocol header information and subject identification

Source: Internet
Author: User

The HTTP protocol header information and the body are separated using a blank line. What is this, a blank line? Simply put, that is, \r\n\r\n.

So the regression of the server data \r\n\r\n Result separation, one is the header information. It is the text of a message.

C # For example, the following code:

Using system;using system.collections.generic;using system.componentmodel;using system.data;using System.Drawing; Using system.linq;using system.text;using system.windows.forms;using system.text.regularexpressions;using System.IO ; namespace socket{public    partial class Form1:form    {public        Form1 ()        {            InitializeComponent ();        }        private void Button1_Click (object sender, EventArgs e)        {            int port = +;            Getsocket GS = new Getsocket ();            string result = Gs. Socketsendreceive ("www.baidu.com", port);            Regex regex = new Regex ("\r\n\r\n");//with CJLOVEFL cut            string[] bit = regex. Split (result);            MessageBox.Show (bit[1]);            StreamWriter SW = new StreamWriter ("D:\\1.txt");            Sw. Write (result);            Sw. Close ();}}    }


Result is the information returned by the server that contains the header and body.

Among the Getsocket classes are as follows:

Using system;using system.text;using system.io;using system.net;using system.net.sockets;using System.Windows.Forms;        Using System.text.regularexpressions;public class getsocket{public 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 was to avoid//a exception that occurs when the host IP Address isn't 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;    }//This method requests the home page content for the specified server.  public string socketsendreceive (string server, int port) {string request = "Get/http/1.0\r\nhost:" + server + "\r\nuser-agent:mozilla/5.0 (Windows NT 6.1;        rv:29.0) gecko/20100101 firefox/29.0\r\nconnection:keep-alive\r\n\r\n ";        byte[] BytesSent = Encoding.ASCII.GetBytes (request);        byte[] bytesreceived = new byte[256];        Create a socket connection with the specified server and port.        Socket s = connectsocket (server, port);        if (s = = null) return ("Connection failed");        Send request to the server.        S.send (bytessent, bytessent.length, 0);        Receive the server home page content.        int bytes = 0;        String page = "Default HTML page on" + server + ": \ r \ n"; The following would block until Te page is transmitted.            do {bytes = s.receive (bytesreceived, bytesreceived.length, 0);        Page =page+ Encoding.ASCII.GetString (bytesreceived, 0, bytes);        } while (bytes > 0);    return page;        }/* public static void Main (string[] args) {string host = "http://www.baidu.com";        int port = 80; if (args. Length = = 0)//If No server name is passed as argument to this program and//use of the current host Nam            e as the default.        Host = Dns.gethostname ();        else host = Args[0];        string result = Socketsendreceive ("www.goodjobs.cn", port);        StreamWriter SW = new StreamWriter ("D:\\1.txt");        string w = ""; Sw.        Write (result); Sw.    Close (); }     * */}


Test success!

Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

HTTP protocol header information and subject identification

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.