Home > Back-end >  TcpClient 505 HTTP Version Not Supported
TcpClient 505 HTTP Version Not Supported

Time:10-24

var client = new TcpClient("www.example.com", 80);
var message = @"GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: close\r\n\r\n";
var stream = client.GetStream();
var data = Encoding.ASCII.GetBytes(message);
await stream.WriteAsync(data, 0, data.Length);
data = new byte[256];
var bytes = await stream.ReadAsync(data, 0, data.Length);
var responseData = Encoding.ASCII.GetString(data, 0, bytes);

First line of response: HTTP/1.0 505 HTTP Version Not Supported

Note: Using HttpClient/HttpWebRequest is not an option for my use case. It's a long story beyond the scope of this specific question.

Edit: I also tried

var message = @"GET / HTTP/1.0\r\nHost: www.example.com\r\nConnection: close\r\n\r\n";

CodePudding user response:

Compare what you are sending:
enter image description here

Against what you want to send:
enter image description here

Which boils down to incorrectly assigning message a string literal. You need a regular string instead:

var message = "GET / HTTP/1.1\r\nHost: www.example.com\r\nConnection: close\r\n\r\n";

As otherwise, what you send is actually wrong: GET / HTTP/1.1\\r\\nHost: example.com\\r\\nConnection: close\\r\\n\\r\\n

  • Related