http協議前序資訊和主體鑒別

來源:互聯網
上載者:User

標籤:

http協議前序資訊和主體是使用一個空行分開。這是什麼空行?簡單的說,那是,\r\n\r\n。

所以會server資料的迴歸\r\n\r\n結果分離,一個是標題資訊。它是一個訊息的文本。

C#例如,下面的代碼:

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 = 80;            GetSocket gs = new GetSocket();            string result = gs.SocketSendReceive("www.baidu.com", port);            Regex regex = new Regex("\r\n\r\n");//以cjlovefl切割            string[] bit = regex.Split(result);            MessageBox.Show(bit[1]);            StreamWriter sw = new StreamWriter("D:\\1.txt");            sw.Write(result);            sw.Close();        }    }}


當中result就是由server返回的包含頭部和本文的資訊。

當中GetSocket類例如以下:

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 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;    }    // 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 will 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,            // use the current host name 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();    }     * */}


測試成功!

著作權聲明:本文博主原創文章,部落格,未經同意不得轉載。

http協議前序資訊和主體鑒別

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.