3

I am asking this question primarily to learn. First I tried to make a regular post request and I got this error: C#: Handling WebClient "protocol violation". Then I tried to make a raw connection to understand what was going on and end up being curios.

Anyways here is my question:

I am using fiddler to capture the post request that I am trying to replicate. Here is how the request looks like when capturing it on fiddler:

enter image description here

Note on top I have the request and on the bottom I have the response. Also see how my response is 200 OK

Another proof that this is working is when I run this curl command: enter image description here Note how I get a response.

Now I want to do the same thing with c# here is my code:

        // connect to device
        TcpClient client = new TcpClient();
        client.Connect(System.Net.IPAddress.Parse("170.55.7.163"), 80);

        // create post data to be sent
        string postDataAsString = @"POST http://170.55.7.163/cgi-bin/api.values.post HTTP/1.1" + Environment.NewLine +
            "Host: 170.55.7.163" + Environment.NewLine +
            "Content-Length: 25" + Environment.NewLine +
            Environment.NewLine +
            Environment.NewLine +
            "P270=2&sid=3dc7588be24f";

        byte[] postDataBinary = System.Text.Encoding.UTF8.GetBytes(postDataAsString);

        // make post request
        client.Client.Send(postDataBinary);

        // get response
        byte[] bytes = new byte[1024];
        int lengthOfResponse = client.Client.Receive(bytes);

        var resp = System.Text.Encoding.UTF8.GetString(bytes, 0, lengthOfResponse);

        // I get a bad request in here why!? 

I do not get a 200 response why? What can I be doing wrong :/ . I will like to learn what I am doing wrong.

1 Answer 1

4

After wasting 4 hours of being so curios I found the problem. I changed:

string postDataAsString = @"POST http://170.55.7.163/cgi-bin/api.values.post HTTP/1.1" + Environment.NewLine +
        "Host: 170.55.7.163" + Environment.NewLine +
        "Content-Length: 25" + Environment.NewLine +
        Environment.NewLine +
        Environment.NewLine +
        "P270=2&sid=3dc7588be24f";

TO

string postDataAsString = @"POST /cgi-bin/api.values.post HTTP/1.1" + Environment.NewLine +
            "Host: 170.55.7.163" + Environment.NewLine +
            "Content-Length: 25" + Environment.NewLine +
            Environment.NewLine +
            Environment.NewLine +
            "P270=2&sid=6aec588bfe62";

Removing http://170.55.7.163 from the first line fixed the problem. I think there was nothing wrong with the code the problem is the http server running on 170.55.7.163.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.