开发者

Connect to authenticated REST service c# to get back JSON data

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.Ge开发者_如何学JAVAtResponseStream());
            // Console application output
            string jsonData = reader.ReadToEnd();

            if (!string.IsNullOrWhiteSpace(jsonData))
                ParseNames(jsonData);
        }


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...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜