2017-04-21 61 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

我用'郵差'測試過,一切都很完美。 –

+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我的幫助下,

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

但爲什麼mailgunOk響應的原因仍然神祕。爲了進一步調查,我用Postman張貼到,

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

讓我吃驚,MailgunOk神祕地回答。

相關問題