type:
integration
system:
C#
name:
FoxyCart XML Test Generator in C#
description:
Code in C# to generate a FoxyCart XML datafeed on demand.
tags:
xml,
datafeed,
testing,
C#,
.net,
dotnet
date:
2009-07-30

test xml post with csharp

Description

The purpose of this file is to help you set up and debug your FoxyCart XML DataFeed scripts. It's designed to mimic FoxyCart.com and send encrypted and encoded XML to a URL of your choice. It will print out the response that your script gives back, which should be “foxy” if successful.

Installation

Compile the following code. Create or use a sample datafeed document that is saved as “SampleData.xml” in the output executable's directory.

Requirements

Requires .Net Framework 2.0 or compatible.

Code

using System;
using System.IO;
using System.Net;
using System.Text;
using System.Web;
 
class Program
{
 
    private static string DataFeedKey = "[your FoxyCart datafeed key goes here]";
 
    static void Main(string[] args)
    {
        //Load data
        string cartData = (new StreamReader("SampleData.xml")).ReadToEnd();
 
        byte[] byteData = new UTF8Encoding().GetBytes(cartData);
        byte[] keyData = new ASCIIEncoding().GetBytes(DataFeedKey);
 
        //Encrypt, then URL encode
        RC4(ref byteData, keyData);
        string postData = "FoxyData=" + HttpUtility.UrlEncode(byteData);
 
        //Create a web request to send the data
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://[your datafeed processing page here]");
        req.Method = "POST";
        req.ContentType = "application/x-www-form-urlencoded";
 
        //Write POST stream
        StreamWriter sw = new StreamWriter(req.GetRequestStream(), Encoding.ASCII);
        sw.Write(new ASCIIEncoding().GetBytes(postData));
        sw.Flush();
        sw.Close();
 
        //Send it and see if it was okay
        HttpWebResponse resp = null;
        try
        {
            resp = (HttpWebResponse)req.GetResponse();
        }
        catch (WebException ex)
        {
            string err = new StreamReader(ex.Response.GetResponseStream()).ReadToEnd();
        }
    }
 
 
    /// <summary>
    /// Symetric RC4 decrypter/encrypter.  Cut-and-paste code from
    /// http://dotnet-snippets.com/dns/rc4-encryption-SID577.aspx.
    /// Note that if you call this method twice on the same data with the same
    /// key, it will be in its original form.
    /// </summary>
    /// <param name="bytes">The data to transform.</param>
    /// <param name="key">They key used to encrypt/decrypt the data</param>
    private static void RC4(ref Byte[] bytes, Byte[] key)
    {
        Byte[] s = new Byte[256];
        Byte[] k = new Byte[256];
        Byte temp;
        int i, j;
 
        for (i = 0; i < 256; i++)
        {
            s[i] = (Byte)i;
            k[i] = key[i % key.GetLength(0)];
        }
 
        j = 0;
        for (i = 0; i < 256; i++)
        {
            j = (j + s[i] + k[i]) % 256;
            temp = s[i];
            s[i] = s[j];
            s[j] = temp;
        }
 
        i = j = 0;
 
        Byte[] ret = new Byte[bytes.GetLength(0)];
        for (int x = 0; x < bytes.GetLength(0); x++)
        {
            i = (i + 1) % 256;
            j = (j + s[i]) % 256;
            temp = s[i];
            s[i] = s[j];
            s[j] = temp;
            int t = (s[i] + s[j]) % 256;
            bytes[x] ^= s[t];
        }
    }
}
 
integration/foxycart/test_xml_post_with_csharp.txt · Last modified: 2009/07/31 01:34 by foxybrett
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki