【问题标题】:Post Http call working on postman but code not working in C#Post Http 调用在邮递员上工作,但代码在 C# 中不起作用
【发布时间】:2021-05-27 14:38:18
【问题描述】:

我对邮递员和 C# 代码有疑问。我有两个对 API 的 post 调用,最终必须对另一个 API(webhook)进行回调。

我尝试通过 Postman 发起这两个调用,并且确实获得了正确的回调响应。我的问题是,当我使用此代码时,我没有任何回调响应,但我从调用的服务器获得了 200 条消息。我发送的所有 http post 调用的实现都有同样的问题,并且遇到了同样的问题。

    public static void Main(string[] args)
    {
        // first call 
        var client = new RestClient("http://xxxxxxx/emails/attachments/");
        client.Timeout = -1;
        var request = new RestRequest(Method.POST);
        request.AddHeader("Authorization",
            "Bearer theToken");
        request.AddFile("attachment",
            "C:/Users/..../pdf.pdf");
        IRestResponse response = client.Execute(request);
        Console.WriteLine(response.Content);
        var attachmentId = response.Content;

        // second call
        client = new RestClient("http://xxxxxxx/emails/");
        client.Timeout = -1;
        request = new RestRequest(Method.POST);
        request.AddHeader("Accept", "application/json");
        request.AddHeader("Content-Type", "application/json");
        request.AddHeader("Authorization",
            "Bearer theToken");
        request.AddParameter("application/json",
            "{\r\n  \"subject\": \"email with attachment\",\r\n  \"body\": \"Hi !!!l\\r\\n\",\r\n  \"received_on\": \"2021-05-24T14:07:01.5874416+02:00\",\r\n  \"author\": {\r\n    \"name\": \"dvera@mymail.fr\",\r\n    \"smtp_address\": \"\"\r\n  },\r\n  \"sender\": {\r\n    \"name\": \"dvera@mymail.fr\",\r\n    \"smtp_address\": \"\"\r\n  },\r\n  \"to\": [\r\n    {\r\n      \"name\": \"dvera@mymail.fr\",\r\n      \"smtp_address\": \"\"\r\n    }\r\n  ],\r\n  \"cc\": [\r\n    {\r\n      \"name\": \"\",\r\n      \"smtp_address\": \"\"\r\n    }\r\n  ],\r\n  \"attachments\": [\r\n       " +
            attachmentId + "\r\n  ]\r\n}\r\n", ParameterType.RequestBody);
        response = client.Execute(request);
        Console.WriteLine(response.Content);

    }
}

知道出了什么问题吗?奇怪的是,我每次拨打电话都会收到 200 条回复。

【问题讨论】:

  • 嗯,我觉得还是打开Fiddler,catch requests,比较一下比较好。另外,尽量不要为请求设置超时 - 这将是默认值并检查请求
  • request.AddHeader("Authorization","Bearer theToken); 好像你的引号有问题,虽然这可能不是问题。
  • 另外,您是否从第一个电话中得到响应,或者两次都没有得到响应?
  • 附件以包含两个破折号的新行开头。请参阅:docs.microsoft.com/en-us/previous-versions/office/developer/…
  • 我对两个调用都有响应,状态码为 200 ......这很奇怪。如果我有 200 响应,我应该会收到一个 webhook url 的回调......但我不明白

标签: c# postman restsharp


【解决方案1】:

我用这段代码解决了我的问题:

using (var client = new HttpClient())
{
    using (var form = new MultipartFormDataContent())
    {
        client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);
        
       var file = File.OpenRead(attachmentPath);
       byte[] fileBytes = new byte[file.Length];
       form.Add(new ByteArrayContent(fileBytes, 0, fileBytes.Length), "attachment", Path.GetFileName(attachmentPath));
       HttpResponseMessage response = await client.PostAsync(url, form);
       response.EnsureSuccessStatusCode();
                             
       var output = await response.Content.ReadAsStringAsync();
       PostEmailAttachmentResponse returnValue = new PostEmailAttachmentResponse();
       returnValue.Id = Int32.Parse(output);
       return returnValue;
  }
}

当我发送附件时,我之前的代码没有返回错误消息。服务器端出现问题,没有返回错误。

【讨论】:

  • 不要将 HttpClient 放在 using 块中。尽管它是一次性的,但它实际上是可重入的,并且一个静态实例可以而且应该(重新)用于您的服务。 IE。不要丢弃它
  • 是的,不知道你原来的问题。您甚至说这是服务器端的问题。这类问题实际上不可能完全回答,因为它们是特定于应用程序的并且需要在您的环境中进行调试。 (TBH 这就是为什么 Stack Overflow 实际上没有真正的价值:问题不回答对其他人真正有用)
猜你喜欢
  • 2017-03-10
  • 2019-09-11
  • 2017-06-26
  • 2019-05-27
  • 2019-08-04
  • 2021-09-14
  • 2019-09-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多