Advertisement
uwekeim

Shopware-4-REST-API von .NET 4 aus ansprechen

Dec 11th, 2012
589
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. namespace ShopwareRestApiTest01
  2. {
  3.     using System;
  4.     using System.Globalization;
  5.     using System.Net;
  6.     using Newtonsoft.Json;
  7.     using RestSharp;
  8.  
  9.     internal class Program
  10.     {
  11.         static void Main()
  12.         {
  13.             const string user = "demo";
  14.             const string pass = "NtMd3OIouT2sr0aJcllBIx1fH3SgxmRr0T6r7D4P";
  15.  
  16.             // http://restsharp.org/
  17.             var client =
  18.                 new RestClient(@"http://192.168.147.169/api")
  19.                     {
  20.                         Authenticator = new DigestAuthenticator(user, pass)
  21.                     };
  22.  
  23.             var request = new RestRequest("articles/{id}", Method.GET);
  24.             request.AddUrlSegment("id", 3.ToString(CultureInfo.InvariantCulture)); // replaces matching token in request.Resource
  25.  
  26.             // easily add HTTP Headers
  27.             request.AddHeader("Content-Type", "application/json; charset=utf-8");
  28.  
  29.             // or automatically deserialize result
  30.             // return content type is sniffed but can be explicitly set via RestClient.AddHandler();
  31.             var response = client.Execute(request);
  32.  
  33.             if (response.ErrorException != null)
  34.             {
  35.                 Console.WriteLine(@"################ ERROR ################");
  36.                 Console.WriteLine(response.ErrorException.Message);
  37.             }
  38.             else
  39.             {
  40.                 var content = response.Content; // raw content as string
  41.  
  42.                 dynamic json = JsonConvert.DeserializeObject(content);
  43.                 Console.WriteLine(json);
  44.             }
  45.         }
  46.     }
  47.  
  48.     public class DigestAuthenticator :
  49.         IAuthenticator
  50.     {
  51.         private readonly string _user;
  52.         private readonly string _pass;
  53.  
  54.         public DigestAuthenticator(string user, string pass)
  55.         {
  56.             _user = user;
  57.             _pass = pass;
  58.         }
  59.  
  60.         public void Authenticate(IRestClient client, IRestRequest request)
  61.         {
  62.             request.Credentials = new NetworkCredential(_user, _pass);
  63.         }
  64.     }
  65. }
Advertisement
RAW Paste Data Copied
Advertisement