服务器返回请求错误,但我的简单 post 看起来正确

The server is returning that the request is bad, but my simple post looks correct

我有一个相当简单直接的 post 我正在尝试发送到控制打印机的服务器。最终它是针对 asp.net 网络应用程序的,但我想我应该先把它简化一下。

使用 Postman 应用程序,我可以通过以下方式将作业提交给打印机:

Post: 127.0.0.1:8083/rxlabelprint

Body:
{   
     "name": "Natalie D Sanderson",
    "dob": "08/05/2009",                            
    "directions": "Take twice daily",     
    "med-details": "Amox 500mg",              
    "dry-run": "True"                               
}

效果很好!

using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

class Program
{
    static void Main(string[] args)
    {
        using (var client = new HttpClient())
        {
            string content = "{'name': 'Natalie D Sanderson', 'dob': '08/05/2009', 'directions': 'Take twice daily', 'med-details': 'Amox 500mg','dry-run': 'True'}";
            var result = client.PostAsync(
                "http://localhost:8083/rxlabelprint",
         new StringContent(content, Encoding.UTF8, "application/json"));
            string resultContent = result.Result.ToString();  // Content.ReadAsStringAsync();
            Console.WriteLine(resultContent);
            Console.ReadLine();
        }
    }

错误信息:

StatusCode:400,ReasonPhrase:'BAD REQUEST',版本:1.1,内容:System.Net.Http.StreamContent,Headers: { 连接:keep-alive 日期:2019 年 7 月 31 日,星期三 21:31:17 GMT 服务器:nginx/1.15.8 Content-Length: 92 Content-Type: application/json }

通过使用 FormUrlEncodedContent 格式化您的参数来发送可靠的请求,您可以随时使用

        var targeturi = "http://localhost:8083/rxlabelprint"
        var client = new System.Net.Http.HttpClient();

        HttpContent content = new FormUrlEncodedContent(new[]
        {
            new KeyValuePair<string, string>("name", "Natalie D Sanderson"),
            new KeyValuePair<string, string>("dob", "08/05/2009"),
            new KeyValuePair<string, string>("directions", "Take twice daily"),
            new KeyValuePair<string, string>("med-details", "Amox 500mg"),
            new KeyValuePair<string, string>("dry-run", "True")
        });

        content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");  
        var result = await client.GetAsync(targeturi, content);
        string resultContent = await result.Content.ReadAsStringAsync();
        //log response
        Console.WriteLine(resultContent);
        Console.ReadLine();

我需要 Post,除此之外评论很棒!这是我的最终答案:

    var targeturi = "http://localhost:8083/rxlabelprint";
    var client = new System.Net.Http.HttpClient();

    HttpContent content = new FormUrlEncodedContent(new[]
    {
        new KeyValuePair<string, string>("name", "Natalie Sanderson"),
        new KeyValuePair<string, string>("dob", "08/05/2005"),
        new KeyValuePair<string, string>("directions", "Take twice daily"),
        new KeyValuePair<string, string>("med-details", "Amox 500mg"),
        new KeyValuePair<string, string>("dry-run", "False")
    });

    content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");  
    var result = client.PostAsync(targeturi, content).Result;
    string resultContent = await result.Content.ReadAsStringAsync();
    //log response
    Console.WriteLine(resultContent);
    Console.ReadLine();