从python HTTP请求转换为C#HTTP请求

从python HTTP请求转换为C#HTTP请求,c#,python,C#,Python,我在python中有以下http post请求: auth_req = requests.post(baseurl, data={'username': 'User', 'password': 'Password', 'output_mode': 'json'}, verify=False) 我正试图将请求“转换”为C#,如下所示: var tHttpReq = (HttpWebRequest)WebRequest.Create(URL);

我在python中有以下http post请求:

auth_req = requests.post(baseurl,
                         data={'username': 'User', 'password': 'Password', 'output_mode': 'json'}, verify=False)
我正试图将请求“转换”为C#,如下所示:

var tHttpReq = (HttpWebRequest)WebRequest.Create(URL);
            // adds basic headers

tHttpReq.ContentType = "application/json";
tHttpReq.Method = "POST";
tHttpReq.Accept = "application/json";
using (StreamWriter sWriter = new StreamWriter(tHttpReq.GetRequestStream()))
{
      // creates request body in the concrete class
      string rBody = "{\"username\": \"User\", \"password\": \"Password\", \"output_mode\": \"json\"}";
       // writes the data to the stream
       sWriter.Write(rBody);
       sWriter.Flush();
}
var webHttpResp = (HttpWebResponse)tHttpReq.GetResponse();
我收到了错误的请求,有什么问题吗


谢谢。

使用HttpClient会更简单。您可以这样做:

    using (var client = new HttpClient())
    {
        client.BaseAddress = new Uri(@"http://yourEndPoint.com");
        HttpContent content = new StringContent("{'json' : 'value'}", Encoding.UTF8, "application/json");
        var result = await client.PostAsync("/endpoint", content);
        var contentAsString = await result.Content.ReadAsStringAsync();
    }

webRequest来自哪里?您应该使用tHttpReq而不是Ireckon@Newboy是的,这是一个打字错误,编辑帖子。