我用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;
}
}
}
}
谢谢。这是我犯的一个愚蠢的错误。你能否给我提供一个使用HttpClient的例子?这将是一个很大的帮助。 –
@TanzeelaAli查看修改。 –
再次感谢:) –