【发布时间】:2013-11-07 09:12:29
【问题描述】:
我是 JSON 新手,正在使用 VS 2013/C#。这是请求和响应的代码。很简单,不是吗?
Request request = new Request();
//request.hosts = ListOfURLs();
request.hosts = "www.cnn.com/www.cisco.com/www.microsoft.com/";
request.callback = "process";
request.key = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
string output = JsonConvert.SerializeObject(request);
//string test = "hosts=www.cnn.com/www.cisco.com/www.microsoft.com/&callback=process&key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
try
{
var httpWebRequest = (HttpWebRequest) WebRequest.Create("http://api.mywot.com/0.4/public_link_json2?);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = output;
streamWriter.Write(json);
}
var httpResponse = (HttpWebResponse) httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var responseText = streamReader.ReadToEnd();
}
}
catch (WebException e)
{
MessageBox.Show(e.ToString());
}
//response = true.
//no response = false
return true;
}
当我运行它时,我得到一个 405 错误,表明方法不允许。
在我看来,这里至少存在两个可能的问题:(1) WoT API (www.mywot.com/wiki/API) 需要一个带有正文的 GET 请求,而 httpWebRequest 不允许在 httpWebRequest.Method 中获取;或者 (2) 序列化的字符串没有正确序列化。
注意:在下文中,我不得不删除前导“http://”,因为我没有足够的代表来发布超过 2 个链接。
它应该看起来像: api.mywot.com/0.4/public_link_json2?hosts=www.cnn.com/www.cisco.com/www.microsoft.com/&callback=process&key=xxxxxxxxxxxxxx
但看起来像: api.mywot.com/0.4/public_link_json2?{"hosts":"www.cnn.com/www.cisco.com/www.microsoft.com/","callback":"process","key":"xxxxxxxxxxxxxxxxxxx "}。
如果我浏览到:api.mywot.com/0.4/public_link_json2?hosts=www.cnn.com/www.cisco.com/www.microsoft.com/&callback=process&key=xxxxxxxxxxxxxxx;我得到了预期的响应。
如果我浏览到:api.mywot.com/0.4/public_link_json2?{"hosts":"www.cnn.com/www.cisco.com/www.microsoft.com/","callback":"process ","key":"xxxxxxxxxxxxxxxxxx"};我收到 403 denied 错误。
如果我将请求硬编码并作为 GET 发送,如下所示:
var httpWebRequest = (HttpWebRequest) WebRequest.Create("api.mywot.com/0.4/public_link_json2? + "test"); 它也可以按预期工作。
如果能提供任何帮助,我将不胜感激,希望我已经把问题弄清楚了。谢谢。
【问题讨论】: