Request Web Page in c# spoofing the Host

后端 未结 9 1296
攒了一身酷
攒了一身酷 2020-12-01 06:10

I need to create a request for a web page delivered to our web sites, but I need to be able to set the host header information too. I have tried this using HttpWebRequest,

9条回答
  •  [愿得一人]
    2020-12-01 06:17

    I have managed to find out a more long winded route by using sockets. I found the answer in the MSDN page for IPEndPoint:

    string getString = "GET /path/mypage.htm HTTP/1.1\r\nHost: www.mysite.mobi\r\nConnection: Close\r\n\r\n";
    Encoding ASCII = Encoding.ASCII;
    Byte[] byteGetString = ASCII.GetBytes(getString);
    Byte[] receiveByte = new Byte[256];
    Socket socket = null;
    String strPage = null;
    try
    {
        IPEndPoint ip = new IPEndPoint(IPAddress.Parse("10.23.1.93"), 80);
        socket = new Socket(ip.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
        socket.Connect(ip);
    }
    catch (SocketException ex)
    {
        Console.WriteLine("Source:" + ex.Source);
        Console.WriteLine("Message:" + ex.Message);
    }
    socket.Send(byteGetString, byteGetString.Length, 0);
    Int32 bytes = socket.Receive(receiveByte, receiveByte.Length, 0);
    strPage = strPage + ASCII.GetString(receiveByte, 0, bytes);
    
    while (bytes > 0)
    {
        bytes = socket.Receive(receiveByte, receiveByte.Length, 0);
        strPage = strPage + ASCII.GetString(receiveByte, 0, bytes);
    }
    socket.Close();
    

提交回复
热议问题