Search

Index

Revindex Storefront

Retrieve product (C#)

Last updated on 2015-12-09 1 mins. to read

The following code shows how to perform a simple POST request using C# to retrieve a Product object:

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

...
...
...

HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://a.com/.../Api/Rest/V1/ServiceHandler.ashx");

req.Timeout = 30000;
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";

// Initialize post string
string postString = @"<?xml version=""1.0"" encoding=""utf-8""?>
<request>
  <version>1.0</version>
  <credential>
    <username>Administrator</username>
    <apiKey>00000000-0000-0000-0000-000000000000</apiKey>
  </credential>
  <service>GetActiveProduct</service>
  <parameters>
    <productID>1</productID>
  </parameters>
</request>";

req.ContentLength = Encoding.UTF8.GetByteCount(postString);
using (Stream sw = req.GetRequestStream())
{
    byte[] bytes = Encoding.UTF8.GetBytes(postString);
    sw.Write(bytes, 0, bytes.Length);
}

HttpWebResponse resp = (HttpWebResponse)req.GetResponse();

// Verify HTTP for 200 OK status code
if (resp.StatusCode == HttpStatusCode.OK)
{
    using (StreamReader sr = new StreamReader(resp.GetResponseStream(), Encoding.UTF8))
    {
        string responseData = sr.ReadToEnd();

        // Parse the XML return data
        XDocument doc = XDocument.Parse(responseData);

        // Verify XML for 2000 Success
        if (((XElement)doc.FirstNode).Element("code").Value == "2000")
        {
            // Read the rest of the XML...
        }
    }
}

 

Comments


Powered by Revindex Wiki