2014-02-09 114 views
0

我用Pushbots将通知发送给我的Android应用程序,并计划写在C#中的方法调用pushbots REST API,以撒施消息到所有连接的手机。如提及Here。但是当我提出请求时,我得到了400个响应。错误的请求[400]返回时,REST API调用在C#

这里是我的C#类:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Net; 
using System.IO; 

namespace RestAPICallTest 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 

      HttpWebRequest httpWReq =(HttpWebRequest)WebRequest.Create("https://api.pushbots.com/push/all"); 

      Encoding encoding = new UTF8Encoding(); 
      string postData = "{\"platform\":\"[1]\", \"msg\":\"Hi from Tali\" ,\"badge\":\"10\" ,\"sound\":\"default\""; 
      byte[] data = encoding.GetBytes(postData); 

      httpWReq.ProtocolVersion = HttpVersion.Version11; 
      httpWReq.Method = "POST"; 
      httpWReq.ContentType = "application/json";//charset=UTF-8"; 
      httpWReq.Headers.Add("X-PUSHBOTS-APPID", 
               "52ee4bd11d0ab1282a8b458e"); 
      httpWReq.Headers.Add("X-PUSHBOTS-SECRET", 
              "b28825277373379b8c62126b16359d46"); 

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

回答

1

你缺少你的身体JSON右大括号。 “\”\“\”msg \“:\”Hi,from Tali \“,\”badge \“:\”10 \“,\” “sound \”:\“default \”}“;

顺便说一下,从C#发出HTTP请求的方式是HttpClient类。你的代码会更短,更干净。

编辑:

好吧,这里是你的原代码HttpClient的风格。它具有异步API,所以你应该学习TPL。这是另一个故事。

using System; 
using System.Text; 
using System.Net.Http; 
using System.Threading.Tasks; 

namespace RestAPICallTest 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      Run().Wait(); 
     } 

     static async Task Run() 
     { 
      var httpClient = new HttpClient(); 
      httpClient.DefaultRequestHeaders.Add("X-PUSHBOTS-APPID", "52ee4bd11d0ab1282a8b458e"); 
      httpClient.DefaultRequestHeaders.Add("X-PUSHBOTS-SECRET", "b28825277373379b8c62126b16359d46"); 

      var postData = "{\"platform\":\"[1]\", \"msg\":\"Hi from Tali\" ,\"badge\":\"10\" ,\"sound\":\"default\"}"; 
      var content = new StringContent(postData, Encoding.UTF8, "application/json"); 

      var response = await httpClient.PostAsync("https://api.pushbots.com/push/all", content); 
      response.EnsureSuccessStatusCode(); 

      var responseJson = await response.Content.ReadAsStringAsync(); 
      Console.WriteLine(responseJson); 
     } 
    } 
} 
+0

谢谢。这是我犯的一个愚蠢的错误。你能否给我提供一个使用HttpClient的例子?这将是一个很大的帮助。 –

+0

@TanzeelaAli查看修改。 –

+0

再次感谢:) –

1

v.shashenko's answer是正确的,但它会导致另一个错误“409(冲突)”,因为各地的平台数组值的报价。

//Incorrect 
var postData = "{\"platform\":\"[1]\", \"msg\":\"Hi from Tali\" ,\"badge\":\"10\" ,\"sound\":\"default\"}"; 

//Correct 
var postData = "{\"platform\":[1], \"msg\":\"Hi from Tali\" ,\"badge\":\"10\" ,\"sound\":\"default\"}"; 
+0

这是一个合法的答案。我已经编辑了你的帖子,以更可靠地指向你提到的其他答案。 – ryanyuyu

相关问题