我正试着用C#做a json call个.我试着拨打电话,但没有成功:

public bool SendAnSMSMessage(string message)
{
    HttpWebRequest request = (HttpWebRequest)
                             WebRequest.Create("http://api.pennysms.com/jsonrpc");
    request.Method = "POST";
    request.ContentType = "application/json";

    string json = "{ \"method\": \"send\", "+
                  "  \"params\": [ "+
                  "             \"IPutAGuidHere\", "+
                  "             \"msg@MyCompany.com\", "+
                  "             \"MyTenDigitNumberWasHere\", "+
                  "             \""+message+"\" " +
                  "             ] "+
                  "}";

    StreamWriter writer = new StreamWriter(request.GetRequestStream());
    writer.Write(json);
    writer.Close();

    return true;
}

任何关于如何使这项工作的建议都将不胜感激.

推荐答案

In your code you don't get the HttpResponse, so you won't see what the server side sends you back.

你需要得到类似于你得到请求的方式的回复.所以

public static bool SendAnSMSMessage(string message)
{
  var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://api.pennysms.com/jsonrpc");
  httpWebRequest.ContentType = "text/json";
  httpWebRequest.Method = "POST";

  using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
  {
    string json = "{ \"method\": \"send\", " +
                      "  \"params\": [ " +
                      "             \"IPutAGuidHere\", " +
                      "             \"msg@MyCompany.com\", " +
                      "             \"MyTenDigitNumberWasHere\", " +
                      "             \"" + message + "\" " +
                      "             ] " +
                      "}";

    streamWriter.Write(json);
  }
  var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
  using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
  {
    var responseText = streamReader.ReadToEnd();
    //Now you have your response.
    //or false depending on information in the response
    return true;        
  }
}

I also notice in the pennysms documentation that they expect a content type of "text/json" and not "application/json". That may not make a difference, but it's worth trying in case it doesn't work.

Json相关问答推荐

2020-12年草案中的Json列举模式

如何获取brew list作为JSON输出

从JSON格式提取数据时分隔通用名称

织女星-没有循环的动画条形图第二部分(实际上是织女星)

nlohmann json:为什么我会得到一个解析错误?

Jolt-Json转换:通过引用标识符(而不是索引)设置值

Jolt:数组中两个字符串的连接

GitHub Pages无法正确显示我的项目

Groovy JsonBuilder 在for循环中添加数组元素

具有 (RegEx) 模式的 json-schema 中的枚举

jq json - 按键名 Select

提交后使用 Rails 7 结合 JSON 标签进行标记

一起使用 Argparse 和 Json

哪个更好:Json 或 XML (PHP)

JSON 语法错误:'unexpected number' 或 'JSON.parse: expected ',' or '}' after property value in object'

以 unicode 将 pandas DataFrame 写入 JSON

Rails 中奇怪的 JSON Javascript 问题

ASP.NET MVC 读取原始 JSON 发布数据

从 JSON 创建 Hashtable

在 Java 中比较两个 JSON 文件的最佳方法