Method of C Simulating http Request Based on socket

  • 2021-07-01 08:07:33
  • OfStack

This article illustrates how C # simulates http requests based on socket. Share it for your reference. The specific implementation method is as follows:


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  Impersonation client socket Connect 
  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
  #region  Requested main method  request  Yes http The header of the request, which can be obtained by using the packet grabbing tool ,server You can make the domain name or the ip Address, port http Agreement 1 Like is 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");
    //  Send 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 1 There must be, or there may be no data after receiving it 
   return sb.ToString();
  }
  static void Main(string[] args)
  {
   string getStrs=HeadlerInit();
   string getHtml = HttpHelper.SocketSendReceive(getStrs, "www.baidu.com", 80);
   Console.WriteLine(getHtml);
  }
}

I hope this article is helpful to everyone's C # programming.


Related articles: