c#调用基于REST的API代码

摘自调用Yahoo API的官方代码,可用在Google API,Pingdom API等基于REST的调用上。

原文地址:http://developer.yahoo.com/dotnet/howto-rest_cs.html

C# GET SAMPLE 1

 

  1. using System;
  2. using System.IO;
  3. using System.Net;
  4. using System.Text;
  5. // Create the web request
  6. HttpWebRequest request = WebRequest.Create(“http://developer.yahoo.com/”) as HttpWebRequest;
  7. // Get response
  8. using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
  9. {
  10. // Get the response stream
  11. StreamReader reader = new StreamReader(response.GetResponseStream());
  12. // Console application output
  13. Console.WriteLine(reader.ReadToEnd());
  14. }

 

Simple POST Requests

Some APIs require you to make POST requests. To accomplish this we change the request method and content type and then write the data into a stream that is sent with the request.

C# POST SAMPLE 1

 

  1. // We use the HttpUtility class from the System.Web namespace
  2. using System.Web;
  3. Uri address = new Uri(“http://api.search.yahoo.com/ContentAnalysisService/V1/termExtraction”);
  4. // Create the web request
  5. HttpWebRequest request = WebRequest.Create(address) as HttpWebRequest;
  6. // Set type to POST
  7. request.Method = “POST”;
  8. request.ContentType = “application/x-www-form-urlencoded”;
  9. // Create the data we want to send
  10. string appId = “YahooDemo”;
  11. string context = “Italian sculptors and painters of the renaissance”
  12. + “favored the Virgin Mary for inspiration”;
  13. string query = “madonna”;
  14. StringBuilder data = new StringBuilder();
  15. data.Append(“appid=” + HttpUtility.UrlEncode(appId));
  16. data.Append(“&context=” + HttpUtility.UrlEncode(context));
  17. data.Append(“&query=” + HttpUtility.UrlEncode(query));
  18. // Create a byte array of the data we want to send
  19. byte[] byteData = UTF8Encoding.UTF8.GetBytes(data.ToString());
  20. // Set the content length in the request headers
  21. request.ContentLength = byteData.Length;
  22. // Write data
  23. using (Stream postStream = request.GetRequestStream())
  24. {
  25. postStream.Write(byteData, 0, byteData.Length);
  26. }
  27. // Get response
  28. using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
  29. {
  30. // Get the response stream
  31. StreamReader reader = new StreamReader(response.GetResponseStream());
  32. // Console application output
  33. Console.WriteLine(reader.ReadToEnd());
  34. }

 

HTTP Authenticated requests

The del.icio.us API requires you to make authenticated requests, passing your del.icio.us username and password using HTTP authentication. This is easily accomplished by adding an instance ofNetworkCredentials to the request.

C# HTTP AUTHENTICATION

 

  1. // Create the web request
  2. HttpWebRequest request
  3. = WebRequest.Create(“https://api.del.icio.us/v1/posts/recent”) as HttpWebRequest;
  4. // Add authentication to request
  5. request.Credentials = new NetworkCredential(“username”, “password”);
  6. // Get response
  7. using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
  8. {
  9. // Get the response stream
  10. StreamReader reader = new StreamReader(response.GetResponseStream());
  11. // Console application output
  12. Console.WriteLine(reader.ReadToEnd());
  13. }

 

 



无觅相关文章插件,快速提升流量

标签:, ,

实在不堪垃圾评论的骚扰,还是禁止了吧,有事微博联系吧。