Search code examples
c#tcpclientnetworkstream

HTTP Error 400. The request verb is invalid using TcpClient


I'm using Visual Studio 2017 and VC# and trying to connect to a server machine

If I use a web browser with this url:

http://win8pc:6062/lookup/1

I receive a response like this:

<Response>
     <Error>Transaction 1 is found.</Error>
</Response>

So the server is working correctly.

But when I try to connect from a windows.forms app doing this:

string server = "win8pc";
int iport = 6062;
try
{
    TcpClient client = new TcpClient(server, iport);

    // Translate the passed message into ASCII and store it as a Byte array.
    string message = "http://win8pc:6062/lookup/1";
    Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);

    // Get a client stream for reading and writing.
    NetworkStream stream = client.GetStream();

    // Send the message to the connected TcpServer. 
    stream.Write(data, 0, data.Length);

    // Receive the TcpServer.response.
    // Buffer to store the response bytes.
    data = new Byte[1024];

    // String to store the response ASCII representation.
    String responseData = String.Empty;

    // variable to store bytes received.
    Int32 bytes = 0;

    // Read the first batch of the TcpServer response bytes.
    do
    {
        bytes = stream.Read(data, 0, data.Length);
        if (bytes > 0)
            responseData += System.Text.Encoding.ASCII.GetString(data, 0, bytes);
    } while (bytes > 0);

    // Close everything.
    stream.Close();
    client.Close();
}
catch (ArgumentNullException e)
{
    Console.WriteLine("ArgumentNullException: {0}", e);
}
catch (SocketException e)
{
    Console.WriteLine("SocketException: {0}", e);
}

The response I get in responseData is:

HTTP/1.1 400 Bad Request
Content-Type: text/html; charset=us-ascii
Server: Microsoft-HTTPAPI/2.0
Date: Thu, 26 Apr 2018 23:02:25 GMT
Connection: close
Content-Length: 326

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
<HTML><HEAD><TITLE>Bad Request</TITLE>
<META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD>
<BODY><h2>Bad Request - Invalid Verb</h2>
<hr><p>HTTP Error 400. The request verb is invalid.</p>
</BODY></HTML>

What am I missing?

Regards rubenc


Solution

  • As the server is telling you with its "Bad Request" response code, you are not sending a proper HTTP request. A typical HTTP GET request looks like this:

    GET /url HTTP/1.1
    Host: www.servername.com
    Accept: image/gif, image/jpeg, */*
    Accept-Language: en-us
    Accept-Encoding: gzip, deflate
    User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)
    

    Each line above should end with a carriage return and linefeed pair (\r\n) and the entire request should end with a blank line (\r\n).

    That said, you almost certainly should not be coding this yourself unless it is purely a learning exercise. Instead, take advantage of the built-in WebRequest API or something similar. In this day and age HTTP is a "first-class citizen" so to speak in many programming environments including C#/.NET.