• <i id='P9bV2'><tr id='P9bV2'><dt id='P9bV2'><q id='P9bV2'><span id='P9bV2'><b id='P9bV2'><form id='P9bV2'><ins id='P9bV2'></ins><ul id='P9bV2'></ul><sub id='P9bV2'></sub></form><legend id='P9bV2'></legend><bdo id='P9bV2'><pre id='P9bV2'><center id='P9bV2'></center></pre></bdo></b><th id='P9bV2'></th></span></q></dt></tr></i><div id='P9bV2'><tfoot id='P9bV2'></tfoot><dl id='P9bV2'><fieldset id='P9bV2'></fieldset></dl></div>

        <legend id='P9bV2'><style id='P9bV2'><dir id='P9bV2'><q id='P9bV2'></q></dir></style></legend>

        <small id='P9bV2'></small><noframes id='P9bV2'>

      1. <tfoot id='P9bV2'></tfoot>

        • <bdo id='P9bV2'></bdo><ul id='P9bV2'></ul>
      2. 在 C# 中通过 POST 发送 JSON 并接收返回的 JSON?

        时间:2023-08-24
        <legend id='z1aD8'><style id='z1aD8'><dir id='z1aD8'><q id='z1aD8'></q></dir></style></legend>

            <i id='z1aD8'><tr id='z1aD8'><dt id='z1aD8'><q id='z1aD8'><span id='z1aD8'><b id='z1aD8'><form id='z1aD8'><ins id='z1aD8'></ins><ul id='z1aD8'></ul><sub id='z1aD8'></sub></form><legend id='z1aD8'></legend><bdo id='z1aD8'><pre id='z1aD8'><center id='z1aD8'></center></pre></bdo></b><th id='z1aD8'></th></span></q></dt></tr></i><div id='z1aD8'><tfoot id='z1aD8'></tfoot><dl id='z1aD8'><fieldset id='z1aD8'></fieldset></dl></div>
              1. <tfoot id='z1aD8'></tfoot>
                • <bdo id='z1aD8'></bdo><ul id='z1aD8'></ul>
                    <tbody id='z1aD8'></tbody>

                  <small id='z1aD8'></small><noframes id='z1aD8'>

                • 本文介绍了在 C# 中通过 POST 发送 JSON 并接收返回的 JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  这是我第一次在我的任何应用程序中使用 JSON 以及 System.NetWebRequest.我的应用程序应该向身份验证服务器发送一个类似于下面的 JSON 有效负载:

                  This is my first time ever using JSON as well as System.Net and the WebRequest in any of my applications. My application is supposed to send a JSON payload, similar to the one below to an authentication server:

                  {
                    "agent": {                             
                      "name": "Agent Name",                
                      "version": 1                                                          
                    },
                    "username": "Username",                                   
                    "password": "User Password",
                    "token": "xxxxxx"
                  }
                  

                  为了创建这个负载,我使用了 JSON.NET 库.我如何将此数据发送到身份验证服务器并接收其 JSON 响应?这是我在一些示例中看到的,但没有 JSON 内容:

                  To create this payload, I used the JSON.NET library. How would I send this data to the authentication server and receive its JSON response back? Here is what I have seen in some examples, but no JSON content:

                  var http = (HttpWebRequest)WebRequest.Create(new Uri(baseUrl));
                  http.Accept = "application/json";
                  http.ContentType = "application/json";
                  http.Method = "POST";
                  
                  string parsedContent = "Parsed JSON Content needs to go here";
                  ASCIIEncoding encoding = new ASCIIEncoding();
                  Byte[] bytes = encoding.GetBytes(parsedContent);
                  
                  Stream newStream = http.GetRequestStream();
                  newStream.Write(bytes, 0, bytes.Length);
                  newStream.Close();
                  
                  var response = http.GetResponse();
                  
                  var stream = response.GetResponseStream();
                  var sr = new StreamReader(stream);
                  var content = sr.ReadToEnd();
                  

                  但是,与我过去使用过的其他语言相比,这似乎是很多代码.我这样做正确吗?以及如何获取 JSON 响应以便我可以解析它?

                  However, this seems to be a lot of code compaired to using other languages I have used in the past. Am I doing this correctly? And how would I get the JSON response back so I can parse it?

                  谢谢,精英.

                  更新代码

                  // Send the POST Request to the Authentication Server
                  // Error Here
                  string json = await Task.Run(() => JsonConvert.SerializeObject(createLoginPayload(usernameTextBox.Text, password)));
                  var httpContent = new StringContent(json, Encoding.UTF8, "application/json");
                  using (var httpClient = new HttpClient())
                  {
                      // Error here
                      var httpResponse = await httpClient.PostAsync("URL HERE", httpContent);
                      if (httpResponse.Content != null)
                      {
                          // Error Here
                          var responseContent = await httpResponse.Content.ReadAsStringAsync();
                      }
                  }
                  

                  推荐答案

                  我发现自己在使用 HttpClient 库来查询 RESTful API,因为代码非常简单且完全异步.要发送此 JSON 有效负载:

                  I found myself using the HttpClient library to query RESTful APIs as the code is very straightforward and fully async'ed. To send this JSON payload:

                  {
                    "agent": {                             
                      "name": "Agent Name",                
                      "version": 1                                                          
                    },
                    "username": "Username",                                   
                    "password": "User Password",
                    "token": "xxxxxx"
                  }
                  

                  有两个类代表您发布的 JSON 结构,可能如下所示:

                  With two classes representing the JSON structure you posted that may look like this:

                  public class Credentials
                  {
                      public Agent Agent { get; set; }
                      
                      public string Username { get; set; }
                      
                      public string Password { get; set; }
                      
                      public string Token { get; set; }
                  }
                  
                  public class Agent
                  {
                      public string Name { get; set; }
                      
                      public int Version { get; set; }
                  }
                  

                  您可以使用这样的方法来执行您的 POST 请求:

                  You could have a method like this, which would do your POST request:

                  var payload = new Credentials { 
                      Agent = new Agent { 
                          Name = "Agent Name",
                          Version = 1 
                      },
                      Username = "Username",
                      Password = "User Password",
                      Token = "xxxxx"
                  };
                  
                  // Serialize our concrete class into a JSON String
                  var stringPayload = JsonConvert.SerializeObject(payload);
                  
                  // Wrap our JSON inside a StringContent which then can be used by the HttpClient class
                  var httpContent = new StringContent(stringPayload, Encoding.UTF8, "application/json");
                  
                  var httpClient = new HttpClient()
                      
                  // Do the actual request and await the response
                  var httpResponse = await httpClient.PostAsync("http://localhost/api/path", httpContent);
                  
                  // If the response contains content we want to read it!
                  if (httpResponse.Content != null) {
                      var responseContent = await httpResponse.Content.ReadAsStringAsync();
                      
                      // From here on you could deserialize the ResponseContent back again to a concrete C# type using Json.Net
                  }
                  

                  这篇关于在 C# 中通过 POST 发送 JSON 并接收返回的 JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何使用 EF Core 将 JSON 存储在实体字段中? 下一篇:有没有办法在不使用 JsonIgnore 属性的情况下忽略

                  相关文章

                  最新文章

                  • <bdo id='ZRv0a'></bdo><ul id='ZRv0a'></ul>

                  <small id='ZRv0a'></small><noframes id='ZRv0a'>

                • <i id='ZRv0a'><tr id='ZRv0a'><dt id='ZRv0a'><q id='ZRv0a'><span id='ZRv0a'><b id='ZRv0a'><form id='ZRv0a'><ins id='ZRv0a'></ins><ul id='ZRv0a'></ul><sub id='ZRv0a'></sub></form><legend id='ZRv0a'></legend><bdo id='ZRv0a'><pre id='ZRv0a'><center id='ZRv0a'></center></pre></bdo></b><th id='ZRv0a'></th></span></q></dt></tr></i><div id='ZRv0a'><tfoot id='ZRv0a'></tfoot><dl id='ZRv0a'><fieldset id='ZRv0a'></fieldset></dl></div>
                      <tfoot id='ZRv0a'></tfoot>

                    1. <legend id='ZRv0a'><style id='ZRv0a'><dir id='ZRv0a'><q id='ZRv0a'></q></dir></style></legend>