
Slängde ihop lite kod som relaterar till ett projekt på jobbet kring att hämta och sprida information på ett "enkelt" sätt. I ovan bild har jag plockat information från en länk till en låt hos Spotify. Med hjälp av regular expressions plockar jag sedan ut titeln, sedan splittar jag titeln för att få ut artisten och låtens namn.
Kodexempel för REST mot Spotify
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;
namespace ConsoleApplication1
{
class Program
{
static void Main(string\[\] args)
{
// Create the web request
HttpWebRequest request = WebRequest.Create("http://open.spotify.com/track/5ErkTzTNppov36yPDng8zW") as HttpWebRequest;
// Get response
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse) {
// Get the response stream
StreamReader reader = new StreamReader(response.GetResponseStream());
// fetch title
string resultString = Regex.Match(reader.ReadToEnd(), "<title>(\[^<\]\*)</title>").Groups\[1\].Value.Replace("Spotify: ", "");
string\[\] title = resultString.Split(´-´);
// Console application output
//Console.WriteLine(reader.ReadToEnd()); //write all output from the request
Console.WriteLine("Artist: " + title\[0\] + "
Song: " + title\[1\]);
Console.ReadLine(); // to stop the console window from closing
}
}
}
}
Här kan du ladda hem projektfiler för Visual Studio 2008 » (webbstrategiforalla.se/upload/Exempelprojekt/091103_REST-mot-Spotify.zip)
Fler kodexempel för REST
- Gör REST-förfrågningar mot Yahoo
- Tutorial och kodexempel för REST (rest.elkstein.org)