2017-04-21 16 views
0

Postmanx-www-from-urlencodedと基本認証ですべて正常に動作します。今私の手を汚してみると、私はちょうど郵便銃に何もない状態コード200を得て、ログは記録されません。maingunはOKで返信しますが、何も起こりません。

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 <[email protected]>"), 
    new KeyValuePair<string, string>("to","[email protected]"), 
    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"   
} 
+0

これは正しい応答のようには思われません。正しいURL/APIエンドポイントを使用していますか? – FrankerZ

+0

私は 'Postman'でテストし、すべてが完璧に機能しました。 –

+0

[こちら](http://stackoverflow.com/questions/18924996/logging-request-response-messages-when-using-httpclient)をご覧になり、リクエストを記録してください。郵便配達要求とc#要求の違いを探します。 – FrankerZ

答えて

0

まず、それは私がBaseAddressnot rightを得ていたが判明しました。私はBaseAddressの最後にスラッシュを入れなければならなかった。

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

スラッシュなしで、私はそれを整理した後、(ノートv3が欠落している)に

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

を掲示して、別の問題が401 - ANAUTHORIZEDを浮上しました。そして、私はこのSO answerの助けを借りて、mailgunOkで応答された理由を

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

しかし、その理由はまだ謎のまま。さらに調査するために、私は、に投稿Postmanを使用

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

と私の驚きに、Mailgunは不思議なOkと答えました。

関連する問題