2

First, i try to post the script in PostMan tool.

{"AO":"ECHO"}

It working fine. Then i'm writing this request in C# but it not working. And more i wrote the request again in Python, and it working well. But my project is in Microsoft C#. I dont want to run script Python in C# at all.

==== Python =========

import httplib
import json
import sys

data = '{"AO":"ECHO"}'
headers = {"Content-Type": "application/json", "Connection": "Keep-Alive" }
conn = httplib.HTTPConnection("http://10.10.10.1",1040)
conn.request("POST", "/guardian", data, headers)
response = conn.getresponse()

print response.status, response.reason
print response.msg

==== C# ============

var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://10.10.10.1:1040/guardian");

            httpWebRequest.Method = "POST";

            httpWebRequest.ContentType = "application/json";
            using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
            {

                string json = "{\"AO\":\"ECHO\"}";

                streamWriter.Write(json);
                streamWriter.Flush();
                streamWriter.Close();

                var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    var result = streamReader.ReadToEnd();
                    Console.WriteLine(result);
                }
            }

I try to put "ContentLength" but it still timeout exception. And i try to using RestSharp, it's not timeout but return null. Any one please help...

            var client = new RestClient("http://10.10.10.1:1040/guardian");
            var request = new RestRequest();

            request.Method = Method.POST;
            request.AddHeader("Content-Type", "application/json");
            request.Parameters.Clear();
            request.RequestFormat = DataFormat.Json;
            request.AddBody(new { AO = "ECHO" });

            var response = client.Execute(request);
            var content = response.Content;

Please help me, I dont understand why it working fine in python. But why it not working in C#. I try to find many request in C# but it got error exception with timeout.

  • Is your problem similar like this [here](http://stackoverflow.com/questions/1789627/how-to-change-the-timeout-on-a-net-webclient-object) ? – shanmugharaj Sep 07 '16 at 07:58
  • Dear @Shankfk, There are not the same of my problem. It testing on my network server. It take time just 3 second to execute this. And it working very well in python script as detail above. But in C# it not working at all, it return error exception timeout. However i try to change timeout. –  Sep 07 '16 at 08:43
  • Do Python and C# use the same encoding? – Bernhard Hiller Sep 07 '16 at 08:57
  • I'm new with python, that make me stuck with it –  Sep 07 '16 at 09:22
  • Can you post the server code? Or make the server accessible from the internet and post the URL? – Veener Sep 12 '16 at 02:02

1 Answers1

0

Python will automatically add Content-Length http header. https://docs.python.org/2/library/httplib.html#httpconnection-objects

I think you might have to set this header manually in C#.

httpWebRequest.ContentLength = json.length;

Depending on the server, you may have to set UserAgent as well.

httpWebRequest.UserAgent=".NET Framework Test Client";
Veener
  • 4,771
  • 2
  • 29
  • 37
  • Hi Veener, It's still got the same error occurs. Seem not work with that way. –  Sep 09 '16 at 08:37
  • At this point I would use fiddler or Wireshark to inspect the http traffic and compare the request and response going to the server between the two languages. – Veener Sep 09 '16 at 11:34