Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Post Http调用在postman上工作,但代码在C中不工作#_C#_Postman_Restsharp - Fatal编程技术网

C# Post Http调用在postman上工作,但代码在C中不工作#

C# Post Http调用在postman上工作,但代码在C中不工作#,c#,postman,restsharp,C#,Postman,Restsharp,我对邮递员和密码有意见。我有两个对一个API的post调用,最后必须回调另一个API(webhook) 我试图通过邮递员启动这两个呼叫,但我确实正确地获得了回调响应。我的问题是,当我使用这段代码时,我没有任何回调响应,但我从我调用的服务器获得了200条消息。我发送的http post调用的所有实现都有相同的问题,并且得到相同的问题 public static void Main(string[] args) { // first call var

我对邮递员和密码有意见。我有两个对一个API的post调用,最后必须回调另一个API(webhook)

我试图通过邮递员启动这两个呼叫,但我确实正确地获得了回调响应。我的问题是,当我使用这段代码时,我没有任何回调响应,但我从我调用的服务器获得了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个响应。

我用以下代码解决了我的问题:

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;
  }
}

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

我认为最好打开Fiddler,捕获请求并进行比较。另外,尽量不要为请求设置超时-它将是默认值,并检查request
request.AddHeader(“Authorization”,“Bearer theToken”);
这里的引号似乎有问题,尽管这可能不是问题所在。此外,您是否从第一次呼叫中得到响应,或者两次都没有收到响应?附件以包含两个破折号的新行开头。请参见:我对两次呼叫都有200状态代码的响应……这很奇怪。如果我有一个200 r作为响应,我应该收到一个对webhook url的回调…但我不明白。不要将HttpClient放在using块中。虽然它是一次性的,但实际上是可重入的,一个静态实例可以而且应该是(re)用于您的服务。例如,不要处理。是的,不知道您的原始问题。您甚至在服务器端声明这是一个问题。这些问题实际上不可能完全回答,因为它们是特定于应用程序的,需要在您的环境中调试。(TBH这就是为什么堆栈溢出实际上没有真正的价值:问题没有答案,对其他人真的有用)