Why python request working but C# request not working?
<p>First, i try to post the script in PostMan tool.</p>

<pre><code>{"AO":"ECHO"}
</code></pre>

<p>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.</p>

<p>==== Python =========</p>

<pre><code>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
</code></pre>

<p>==== C# ============</p>

<pre><code>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);
                }
            }
</code></pre>

<p>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...</p>

<pre><code>            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;
</code></pre>

<p>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.</p>

<p>Python will automatically add Content-Length http header.
<a href="https://docs.python.org/2/library/httplib.html#httpconnection-objects" rel="nofollow">https://docs.python.org/2/library/httplib.html#httpconnection-objects</a></p>

<p>I think you might have to set this header manually in C#.  </p>

<pre><code>httpWebRequest.ContentLength = json.length;
</code></pre>

<p>Depending on the server, you may have to set UserAgent as well.</p>

<pre><code>httpWebRequest.UserAgent=".NET Framework Test Client";
</code></pre>

c# python postman