【问题标题】:maingun reply with OK but nothing happensmaingun 回复 OK 但没有任何反应
【发布时间】:2017-09-18 11:49:20
【问题描述】:

使用x-www-from-urlencoded 和基本身份验证在Postman 上一切正常。现在想弄脏我的手,我只得到状态代码 200,mailgun 上什么都没有,没有记录日志。

using (var client = new HttpClient())
{
  client.BaseAddress = new Uri("https://api.mailgun.net/v3");

  client.DefaultRequestHeaders.Authorization = 
     new AuthenticationHeaderValue("api", "key-withheld-till-a-verdict-has-passed");

  var msg = new List<KeyValuePair<string, string>>
  {
    new KeyValuePair<string, string>("from",
        $"Excited User <mailgun@sandboxSOMEGUIDHERE.mailgun.org>"),
    new KeyValuePair<string, string>("to","approved-to-receive@mailgun"),
    new KeyValuePair<string, string>("subject", "Test Please Do Not Reply"),
    new KeyValuePair<string, string>("text","Thanks for borrowing me your inbox")
  };

  var request = new HttpRequestMessage(HttpMethod.Post, 
     "sandboxSOMEGUIDHERE.mailgun.org/messages");

  request.Content = new FormUrlEncodedContent(msg);

  var response = await client.SendAsync(request);
  // I get 200 status code

  var result = await response.Content.ReadAsStringAsync(); 
  //I get result = "Mailgun Magnificent API"         
}

【问题讨论】:

  • 这似乎不是正确的反应。您确定您使用的是正确的 URL/API 端点吗?
  • 我用Postman 测试过,一切都很完美。
  • 查看here 并记录您的请求。查找 postman 请求和 c# 请求之间的差异。

标签: c# dotnet-httpclient mailgun


【解决方案1】:

首先,我得到了BaseAddress not right。我必须在BaseAddress 的末尾添加一个斜线。

 client.BaseAddress = new Uri("https://api.mailgun.net/v3/");

没有斜线,我发帖到(注意 v3 不见了),

https://api.mailgun.net/sandboxSOMEGUIDHERE.mailgun.org/messages

整理之后,又出现了另一个问题401 - ANAUTHORIZED。在这个SO answer 的帮助下,我做到了,

var byteArray = new UTF8Encoding()
    .GetBytes("api:key-withheld-till-a-verdict-has-passed");
client.DefaultRequestHeaders.Authorization =
     new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));

mailgun 回复Ok 的原因仍然是个谜。为了进一步调查,我使用Postman 发帖到,

https://api.mailgun.net/sandboxSOMEGUIDHERE.mailgun.org/messages

令我惊讶的是,Mailgun 神秘地回复了 Ok

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-09
    • 2012-12-20
    • 2016-09-27
    • 2021-07-27
    相关资源
    最近更新 更多