If you send requests via HttpWebRequest, you need to separately enter the necessary parameters "Host", "KeepAlive", "UserAgent" ..... such a header may be> 15.

And when executing HttpWebRequest, all these headers (parameters) are not always in the right sequence, and some may even be skipped.

Is it possible to send a GET or POST request, specifying the RAW request itself instead of all these parameters (headers)? With the order I need. And just the way I want to see him.

    1 answer 1

    You can use lower-level classes and send whatever you want:

    var client = new TcpClient("host", 80); var stream = client.GetStream(); 

    If TLS connection:

     var client = new TcpClient("host", 443); var stream = new SslStream(client.GetStream()); 

    And then we send something:

     string request = "GET /folder/1.html HTTP/1.0" + Environment.NewLine + "Host: " + "host" + Environment.NewLine + "Your-header: " + "value" + Environment.NewLine + Environment.NewLine; // или вы можете взять raw байты заголовка откуда-то еще // если есть имя ресурса и raw заголовки, нужно будет склеить байты первой строки с "GET/POST" и байты заголовоков // (не забыв что в конце должен быть перенос строки 2 раза) byte[] requestBytes = Encoding.ASCII.GetBytes(request); stream.Write(requestBytes, 0, requestBytes.Length); 

    And we read the answer:

     var reader = new StreamReader(stream, Encoding.UTF8); // подставьте сюда нужную кодировку var response = sr.ReadToEnd(); // здесь будут и заголовки и тело // закрываем стрим и TcpClient 

    But the response headers will have to be parsed by yourself.

    • I recommend the xNet library. It already has a pretty good implementation of HTTP based on TCP, on the one hand. On the other hand, there are artificial limitations in the library itself, but they can be removed if you make changes to the code — and it is open and easy to assemble. - SmInc