http 프로토콜 헤더 정보와 본문

4195 단어
http 프로토콜의 헤더 정보와 본문은 빈 줄로 나뉘는데, 무엇이 빈 줄입니까?간단히 말하면\r\r입니다.
그래서 서버에서 되돌아오는 데이터를\r\r로 분리한 결과 하나는 헤더 정보이고 하나는 본문 정보이다.
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
\r
");// 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
Host: " + server + "\r
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:29.0) Gecko/20100101 Firefox/29.0\r
Connection: keep-alive\r
\r
"; 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
"; // 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(); } * */ }

테스트 성공!

좋은 웹페이지 즐겨찾기