2013-08-06 150 views
2

餘米嘗試發送以下請求:發送POST的HTTPS內容類型application JSON請求對C#

POST /messaging/registrations/(REGISTRATION_ID_FOR_DESTINATION_APP_INSTANCE)/messages HTTP/1.1 
Host: api.amazon.com 
Authorization: Bearer (MY_ACCESS_TOKEN) 
Content-Type: application/json 
X-Amzn-Type-Version: [email protected] 
Accept: application/json 
X-Amzn-Accept-Type: [email protected] 

{ 
"data":{"message":"value1","title":"value2"}, 
"consolidationKey":"Some Key", 
"expiresAfter":86400 
} 

爲了得到迴應的格式是這樣的:

HTTP/1.1 200 
X-Amzn-Data-md5: t5psxALRTM7WN30Q8f20tw== 
X-Amzn-RequestId: e8bef3ce-242e-11e2-8484-47f4656fc00d 
Content-Type: application/json 
X-Amzn-Type-Version: [email protected] 
Content-Length: 308 

{"registrationID":(REGISTRATION_ID_FOR_DESTINATION_APP_INSTANCE)} 

做我試着用這段代碼:

private void sendNotification(String registrationID,String message, 
              String title,String accessToken) 
{  
//registrationID content (REGISTRATION_ID_FOR_DESTINATION_APP_INSTANCE) that can vary 
string url = "https://api.amazon.com/messaging/registrations/"+ registrationID +"/messages"; 

var client = new HttpClient(); 

//set Request headers 
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(
                 "Bearer", accessToken); 
client.DefaultRequestHeaders.Accept.Add(
      new MediaTypeWithQualityHeaderValue("application/json")); 
client.DefaultRequestHeaders.Add("X-Amzn-Type-Version", 
           "[email protected]"); 
client.DefaultRequestHeaders.Add("X-Amzn-Accept-Type", 
          "[email protected]"); 

//the content of the message body 

var content = new Dictionary<string, Object>(); 
content.Add("consolidationKey", "SyncNow"); 
content.Add("expiresAfter", 86400); 
var data = new Dictionary<string, string>(); 
data.Add("message", message); 
data.Add("title", title); 
content.Add("data", data); 


var result = client.PostAsJsonAsync<Dictionary<string, Object>>(url, content).Result; 
} 

作爲響應我得到StatusCode:400,ReasonPhrase:'Bad Request',我不知道爲什麼?

有人可以幫助我,請!

有關結果至極細節我:

result {StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content: System.Net.Http.StreamContent, Headers: 
{ 
    x-amzn-RequestId: 1b943a1c-fe94-11e2-b963-71a537223b43 
    Vary: Accept-Encoding 
    Vary: User-Agent 
    Cneonction: close 
    Date: Tue, 06 Aug 2013 12:31:24 GMT 
    Content-Length: 34 
    Content-Type: application/json 
}} System.Net.Http.HttpResponseMessage 

result.RequestMessage {Method: POST, 
RequestUri: 'https://api.amazon.com/messaging/registrations/(REGISTRATION_ID_FOR_DESTINATION_APP_INSTANCE)/messages', 
Version: 1.1, 
Content: System.Net.Http.ObjectContent`1[[System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089], 
[System.Object, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], 
Headers: 
{ 
    Authorization: Bearer (MY_ACCESS_TOKEN) 
    Accept: application/json 
    X-Amzn-Type-Version: [email protected] 
    X-Amzn-Accept-Type: [email protected] 
    Content-Type: application/json; charset=utf-8 
    Content-Length: 98 
}}  System.Net.Http.HttpRequestMessage 

回答

7

我發現了另一個完美工作的解決方案。

private void sendNotification(String registrationID,String message,String title,String accessToken) 
    { 
     HttpWebRequest httpWReq = 
      (HttpWebRequest)WebRequest.Create("https://api.amazon.com/messaging/registrations/" + registrationID + "/messages"); 

     Encoding encoding = new UTF8Encoding(); 
     string postData = "{\"data\":{\"message\":\""+message+"\",\"title\":\""+title+"\"},\"consolidationKey\":\"Some Key\",\"expiresAfter\":86400}"; 
     byte[] data = encoding.GetBytes(postData); 

     httpWReq.ProtocolVersion = HttpVersion.Version11; 
     httpWReq.Method = "POST"; 
     httpWReq.ContentType = "application/json";//charset=UTF-8"; 
     httpWReq.Headers.Add("X-Amzn-Type-Version", 
              "[email protected]"); 
     httpWReq.Headers.Add("X-Amzn-Accept-Type", 
             "[email protected]"); 
     httpWReq.Headers.Add(HttpRequestHeader.Authorization, 
      "Bearer " + accessToken); 
     httpWReq.ContentLength = data.Length; 


     Stream stream = httpWReq.GetRequestStream(); 
     stream.Write(data, 0, data.Length); 
     stream.Close(); 

     HttpWebResponse response = (HttpWebResponse)httpWReq.GetResponse(); 
     string s=response.ToString(); 
     StreamReader reader = new StreamReader(response.GetResponseStream()); 
     String jsonresponse = ""; 
     String temp = null; 
     while ((temp = reader.ReadLine()) != null) 
     { 
      jsonresponse += temp; 
     } 

    } 
0

只是看着這個很快,好像這行代碼是錯誤的。

var result = client.PostAsJsonAsync<Dictionary<string, Object>>(url, data).Result; 

基於事實,你說亞馬遜希望後json看起來像這樣。

{"data":{"message":"value1","title":"value2"},"consolidationKey":"Some Key","expiresAfter":86400} 

看起來你應該將var內容傳遞給帖子而不是var數據。即

var result = client.PostAsJsonAsync<Dictionary<string, Object>>(url, content).Result; 
+0

是的,它已經完成,我糾正它。但我有相同的錯誤響應。 – mouhcine

+0

我的代碼中沒有看到任何明顯的錯誤。您可能需要下載並運行像fiddler這樣的代理服務器,並將其設置爲與https協同工作,並查看通過網絡發送的實際內容。也許標題有些奇怪的事情發生,但是如果不能真正查看發送和返回的內容,這很難說。通常不好的請求是因爲json文章有問題。 – cgotberg

+0

好的!爲所有人認爲@cgotberg。我找到了其他解決方案。 – mouhcine

相關問題