连接到经过身份验证的 REST 服务 C# 以获取 JSON 数据
我试图调用 REST Api 来取回一些 JSON 数据,但无法连接。我认为它需要一个缓存的 cookie 或其他东西才能工作。有使用过 REST Bitbucket.org api 的想法或人吗?这是我正在使用的api地址 https://api.bitbucket.org/1.0/user/repositories /。如果您直接在网络浏览器中使用,它就可以工作。
HttpWebRequest request = WebRequest.Create(
ConfigurationManager.AppSettings["RESTApiPath"]) as HttpWebRequest;
request.Credentials = new NetworkCredential(
ConfigurationManager.AppSettings["user"],
ConfigurationManager.AppSettings["pass"]);
//request.PreAuthenticate = true;
// Get response
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
// Get the response stream
StreamReader reader = new StreamReader(response.GetResponseStream());
// Console application output
string jsonData = reader.ReadToEnd();
if (!string.IsNullOrWhiteSpace(jsonData))
ParseNames(jsonData);
}
I am trying to call a REST Api to get back some JSON data and I can't make it connect. I think it needs a cached cookie or something to work. Any ideas or people that have used the REST Bitbucket.org api? Here is api address I am using https://api.bitbucket.org/1.0/user/repositories/. It works if you used directly in the web browswer.
HttpWebRequest request = WebRequest.Create(
ConfigurationManager.AppSettings["RESTApiPath"]) as HttpWebRequest;
request.Credentials = new NetworkCredential(
ConfigurationManager.AppSettings["user"],
ConfigurationManager.AppSettings["pass"]);
//request.PreAuthenticate = true;
// Get response
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
// Get the response stream
StreamReader reader = new StreamReader(response.GetResponseStream());
// Console application output
string jsonData = reader.ReadToEnd();
if (!string.IsNullOrWhiteSpace(jsonData))
ParseNames(jsonData);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 Fiddler 或 Wireshark 来比较当它工作时(浏览器)和不工作时(您的代码)通过网络传输的内容...一旦您知道差异,您就可以相应地更改您的代码...
Use Fiddler or Wireshark to compare what goes over the wire when it works (browser) and when it doesn't work (your code)... once you know the differences you can change your code accordingly...