Question

I am (trying) to develop a WPF (C#) app that just gets (or at least is supposed to get) my saved bookmarks at Diigo.com profile. The only helpful page i found is this . It says i have to use HTTP Basic authetication to get my self authenticated and make requests then. But don't understand how C# handles it!. The only solution i came up with below just prints entire HTML source to console window.

string url = "http://www.diigo.com/sign-in";

WebRequest myReq = WebRequest.Create(url);
string usernamePassword = "<username>:<password>";
CedentialCache mycache = new CredentialCache();
mycache.Add(new Uri(url), "Basic", new NetworkCredential("username", "password"));
myReq.Credentials = mycache;
myReq.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(new     ASCIIEncoding().GetBytes(usernamePassword)));
 //Send and receive the response
WebResponse wr = myReq.GetResponse();
Stream receiveStream = wr.GetResponseStream();
StreamReader reader = new StreamReader(receiveStream, Encoding.UTF8);
string content = reader.ReadToEnd();
Console.Write(content);

Here username and password are hardcoded but of course they'll come from some txtUsername.Text thing. And after that how am i going to read the JSON response and parse it? What is that i need to do to get my app or myself HTTP basic authenticated? Any help or suggestion is welcome!

Was it helpful?

Solution 2

Ok i solved the problem after some (not really some) effort. Code below gets the JSON response from server which can then be parsed using any preferred method.

 string key = "diigo api key";
 string username = "username";
 string pass = "password";
 string url = "https://secure.diigo.com/api/v2/";     
 string requestUrl = url + "bookmarks?key=" + key + "&user=" + username + "&count=5";
 HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(requestUrl);
 string usernamePassword = username + ":" + pass;
 myReq.Timeout = 20000;
 myReq.UserAgent = "Sample VS2010";
 //Use the CredentialCache so we can attach the authentication to the request
 CredentialCache mycache = new CredentialCache();
 //this perform Basic auth
 mycache.Add(new Uri(requestUrl), "Basic", new NetworkCredential(username, pass));
 myReq.Credentials = mycache;
 myReq.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(usernamePassword)));
  //Send and receive the response
  WebResponse wr = myReq.GetResponse();
  Stream receiveStream = wr.GetResponseStream();
  StreamReader reader = new StreamReader(receiveStream, Encoding.UTF8);
  string content = reader.ReadToEnd();
  Console.Write(content);

content is the JSON response returned from server Also this link is useful for getting started with api.

OTHER TIPS

If you're trying to talk to a service, you probably want to use the Windows Communication Foundation (WCF). It's designed specifically to solve the problems associated with communicating with services, such as reading/writing XML and JSON, as well as negotiating transports mechanisms like HTTP.

Essentially, WCF will save you doing all of the "plumbing" work of working with HttpRequest objects and manipulating strings. Your problems have already been solved by this framework. Use it if you can.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top