将JSON发布到Web服务(400)错误请求

时间:2015-09-20 14:53:15

标签: c# json web-services http-headers

我正在尝试将一些JSON发布到Web服务,如下所示但我一直收到(400)错误请求响应,我似乎无法弄清楚原因。

文档

  

发送短信

     

POST / customers / {customerId} / sms

     

要发送新短信,只需将新短信的表示发布到   列表资源。如果成功,则表示新的   创建的smsmessage将在响应正文中返回。

POST https://pbx.sipcentric.com/api/v1/customers/25/sms

{
  "type": "smsmessage",
  "to": "07902000000",
  "from": "01212854400",
  "body": "Hey, this API is awesome!"
}

我的代码

  var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://pbx.sipcentric.com/api/v1/customers/3682/sms");
    String username = "username";
    String password = "password";
    //Encode Password & user
    String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(username + ":" + password));
    //attach authentication details to header
    httpWebRequest.Headers.Add("Authorization", "Basic " + encoded);
    httpWebRequest.ContentType = "application/json";
    httpWebRequest.Method = "POST";
    httpWebRequest.Accept = "application/json";
    using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
    {

        streamWriter.Write(smsJson);
        streamWriter.Flush();
        streamWriter.Close();
    }

    var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
    using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
    {
        var result = streamReader.ReadToEnd();
    }

和我的JSON

{"smsmessage":{"type":"smsmessage","to":"07984389886","from":"07984389886","body":"THIS IS A TEXT MESSAGE"}}

0 个答案:

没有答案