http协议区分头信息和正文

http协议中的头信息和正文是采用空行分开,什么是空行呢?简单来说,就是\r\n\r\n。

所以将服务器返回的数据用\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就是由服务器返回的包括头部和正文的信息。

其中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();
    }
     * */
}


测试成功!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值