【发布时间】:2011-08-27 19:12:57
【问题描述】:
我正在尝试使用以下 url 访问 REST Disqus API:
http://disqus.com/api/3.0/threads/listPosts.json
?api_key=myKey
&forum=myForum
&thread:ident=myIdent
当我在 Chrome 中访问 url 时,它工作正常。当我尝试在 WebClient 中下载它时,我遇到了困难:
WebClient data = new WebClient();
Uri queryUri = new Uri(DisqusQuery + ident, UriKind.Absolute);
data.DownloadStringCompleted += new DownloadStringCompletedEventHandler(onDownloadCompleted);
data.DownloadStringAsync(queryUri);
DownloadStringCompletedEventArgs 包含以下错误:
{"The remote server returned an error: NotFound."}
at System.Net.Browser.ClientHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)
at System.Net.Browser.ClientHttpWebRequest.<>c__DisplayClass2.<EndGetResponse>b__1(Object sendState)
at System.Net.Browser.AsyncHelper.<>c__DisplayClass4.<BeginOnUI>b__1(Object sendState)
at System.Reflection.RuntimeMethodInfo.InternalInvoke(RuntimeMethoThe thread '<No Name>' (0xfc10086) has exited with code 0 (0x0).
我做错了什么?
更新:查看 Fiddler 显示响应是这样的:
HTTP/1.1 400 BAD REQUEST
Date: Sun, 28 Aug 2011 14:51:39 GMT
Server: Apache/2.2.14 (Ubuntu)
Vary: Cookie,Accept-Encoding
p3p: CP="DSP IDC CUR ADM DELi STP NAV COM UNI INT PHY DEM"
Content-Length: 68
Connection: close
Content-Type: application/json
X-Pad: avoid browser bug
{"code": 11, "response": "Your API key is not valid on this domain"}
这是来自 Chrome Incognito(未登录到 disqus)的请求时的响应:
HTTP/1.1 200 OK
Date: Mon, 29 Aug 2011 17:00:29 GMT
Server: Apache/2.2.14 (Ubuntu)
X-Ratelimit-Remaining: 1000
Content-Encoding: gzip
Vary: Cookie,Accept-Encoding
X-Ratelimit-Limit: 1000
p3p: CP="DSP IDC CUR ADM DELi STP NAV COM UNI INT PHY DEM"
X-Ratelimit-Reset: 1314640800
Content-Length: 3120
Connection: close
Content-Type: application/json
/* expected JSON response */
更新 2:上述错误是使用我的公钥。使用密钥会导致:
HTTP/1.1 403 FORBIDDEN
Date: Sun, 28 Aug 2011 20:40:32 GMT
Server: Apache/2.2.14 (Ubuntu)
Vary: Cookie,Accept-Encoding
p3p: CP="DSP IDC CUR ADM DELi STP NAV COM UNI INT PHY DEM"
Connection: close
Transfer-Encoding: chunked
Content-Type: application/json
2a
{"code": 5, "response": "Invalid API key"}
0
【问题讨论】:
-
在浏览器中放同一个url可以吗?
-
您是否检查过变量
queryUri包含您期望的内容?当您在 Internet Explorer 中托管时会发生什么?你用 Fiddler 详细检查过 http 会话吗? -
@alfonso 是的,它在浏览器中运行良好
-
NotFound 不会是实际错误。由于某种原因,WP7 http api 不会轻易报告真正的错误。尝试使用 fiddler 来观察流量,或者使用 Hammock 库来提出您的请求并取回真正的错误信息。
-
确实添加到 Chris 的评论 NotFound 不是实际错误。我把你的代码放在一个空的项目里通过Fiddler验证结果,发现实际返回的结果是“{”code”: 5, “response”: “Invalid API key”}”(不填任何key时的预期)。手机仍然显示相同的 NotFound 异常。请尝试this 获取更多信息,我们或许可以为您提供帮助
标签: silverlight windows-phone-7 rest webclient disqus