Mono: How to access a REST web service?

In this post I will discuss about consuming REST web services in C#…First of all you can make a quick check for the information about REST vs SOAP services in this post: http://www.petefreitag.com/item/431.cfm

As I mentioned before I am newbie developer in Mono! but start to like it more and more after seeing that the applications I created @ C# also worked in Linux! thanks to Mono!

Lets back to our issue again:

In this post I used yahoo news search REST service as in the example below…

/*
 * Created by SharpDevelop.
 * User: cakirh
 * Date: 21.01.2010
 * Time: 11:26
 *
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using Gtk;
using System;
 using System.IO;
 using System.Net;
 using System.Text;
 using System.Xml;

namespace RESTserviceConn
{
class RESTserviceConn : Window
{

 Gtk.Window window = new Gtk.Window("YahooNews!");
 private Entry entYahoo;

 public RESTserviceConn()
 : base("RESTserviceConn")
 {

 window.SetSizeRequest(240, 100);
 window.SetPosition(WindowPosition.Center);

 entYahoo=new Entry();

 Button btnYahoo=new Button();
 btnYahoo.Label="latest news @ yahoo!";

 Fixed fixedDownload=new Fixed();

 fixedDownload.Put(entYahoo,10,10);

 fixedDownload.Put(btnYahoo,10,40);

 btnYahoo.Clicked+= new EventHandler(btnYahoo_Clicked);

 window.Add(fixedDownload);
 window.ShowAll();

 }

 void btnYahoo_Clicked(object sender, EventArgs e)
 {
 //Create the web request
 //HttpWebRequest request = WebRequest.Create("http://www.thomas-bayer.com/sqlrest/") as HttpWebRequest;
 HttpWebRequest request = WebRequest.Create("http://search.yahooapis.com/NewsSearchService/V1/newsSearch?appid=YahooDemo&query="+entYahoo.Text+"&results=1&language=tr") as HttpWebRequest;
 //Get response
 using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
 {
 //Get the response stream
 StreamReader reader = new StreamReader(response.GetResponseStream());

 //Console application output
 Console.BackgroundColor = ConsoleColor.Blue;
 Console.WriteLine("\n\n\n--------------LatestNews---------------");
 Console.ResetColor();
 Console.BackgroundColor = ConsoleColor.White;
 Console.ForegroundColor=ConsoleColor.Black;
 Console.WriteLine(reader.ReadToEnd());
 Console.ResetColor();
 Console.BackgroundColor = ConsoleColor.Blue;
 Console.WriteLine("--------------LatestNews---------------\n\n\n");
 Console.ResetColor();

 }

 }

 public static void Main()
 {
 Application.Init();
 new RESTserviceConn();
 Application.Run();
 }

 }
}
<pre>

Output:

I used language as Turkish but you can change it from the REST url…

References:

[1.] http://docs.google.com/View?docid=dgdcn6h3_38fz2vn5

[2.] http://www.kdedevelopers.org/node/3641

[3.] http://mono-project.com/Consuming_a_WebService

[4.] http://developer.yahoo.com/dotnet/howto-rest_cs.html

[5.] http://www.peej.co.uk/articles/restfully-delicious.html

[6.] http://www.thomas-bayer.com/rest-demo.htm

Thats all folks for this post!
kick it on DotNetKicks.com

About these ads