uwekeim icon

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

uwekeim | PRO | 12/11/12 03:36:03 PM UTC | 0 ⭐ | 1392 👁️ | Never ⏰ | []
C# |

2.06 KB

|

None

|

0 👍

/

0 👎

namespace ShopwareRestApiTest01
{
    using System;
    using System.Globalization;
    using System.Net;
    using Newtonsoft.Json;
    using RestSharp;
 
    internal class Program
    {
        static void Main()
        {
            const string user = "demo";
            const string pass = "NtMd3OIouT2sr0aJcllBIx1fH3SgxmRr0T6r7D4P";
 
            // http://restsharp.org/
            var client =
                new RestClient(@"http://192.168.147.169/api")
                    {
                        Authenticator = new DigestAuthenticator(user, pass)
                    };
 
            var request = new RestRequest("articles/{id}", Method.GET);
            request.AddUrlSegment("id", 3.ToString(CultureInfo.InvariantCulture)); // replaces matching token in request.Resource
 
            // easily add HTTP Headers
            request.AddHeader("Content-Type", "application/json; charset=utf-8");
 
            // or automatically deserialize result
            // return content type is sniffed but can be explicitly set via RestClient.AddHandler();
            var response = client.Execute(request);
 
            if (response.ErrorException != null)
            {
                Console.WriteLine(@"################ ERROR ################");
                Console.WriteLine(response.ErrorException.Message);
            }
            else
            {
                var content = response.Content; // raw content as string
 
                dynamic json = JsonConvert.DeserializeObject(content);
                Console.WriteLine(json);
            }
        }
    }
 
    public class DigestAuthenticator : 
        IAuthenticator
    {
        private readonly string _user;
        private readonly string _pass;
 
        public DigestAuthenticator(string user, string pass)
        {
            _user = user;
            _pass = pass;
        }
 
        public void Authenticate(IRestClient client, IRestRequest request)
        {
            request.Credentials = new NetworkCredential(_user, _pass);
        }
    }
}

Comments