no way to compare when less than two revisions
Differences
This shows you the differences between two versions of the page.
Previous revisionNext revision | |||
— | integration:foxycart:test_xml_post_with_csharp [2012/08/15 08:19] – [Installation] 95.144.180.128 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | ---- dataentry ---- | ||
+ | type : integration # | ||
+ | systems | ||
+ | name : FoxyCart XML Test Generator in C\# # the name of the integration | ||
+ | description | ||
+ | tags_tags | ||
+ | date_dt | ||
+ | version | ||
+ | developer_url : # if you'd like a link back to your site, stick it here | ||
+ | ---- | ||
+ | |||
+ | |||
+ | ====== test xml post with csharp ====== | ||
+ | |||
+ | <WRAP important round> | ||
+ | //**Please note:**// | ||
+ | The code on this page is submitted by members of the FoxyCart community, and may not verified by FoxyCart.com LLC in any way, shape, or form. Please double check the code before installing. If you need help with it please [[http:// | ||
+ | </ | ||
+ | |||
+ | ===== 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 [[: | ||
+ | |||
+ | |||
+ | ===== Installation ===== | ||
+ | Compile the following code. Create or use a sample datafeed document that is saved as " | ||
+ | |||
+ | You can get the sample XML for v0.7.2 from [[v: | ||
+ | ===== Requirements ===== | ||
+ | Requires .Net Framework 2.0 or compatible. | ||
+ | |||
+ | ===== | ||
+ | Code ===== | ||
+ | <code C#> | ||
+ | using System; | ||
+ | using System.IO; | ||
+ | using System.Net; | ||
+ | using System.Text; | ||
+ | using System.Web; | ||
+ | |||
+ | class Program | ||
+ | { | ||
+ | |||
+ | private static string DataFeedKey = "[Your API key here, remove square brackets]"; | ||
+ | |||
+ | static void Main(string[] args) | ||
+ | { | ||
+ | |||
+ | //Load plain XML data (previously parsed) | ||
+ | string transactionData = (new StreamReader(" | ||
+ | |||
+ | //Encode it using the RC4 algorithm (see class implementation belwow). | ||
+ | string encryptedTransactionData = RC4.Encrypt(DataFeedKey, | ||
+ | string encodedTransactionData = HttpUtility.UrlEncode(encryptedTransactionData, | ||
+ | |||
+ | //Create our POST string. | ||
+ | string postData = " | ||
+ | |||
+ | //Create a web request to send the data | ||
+ | HttpWebRequest req = (HttpWebRequest)WebRequest.Create(" | ||
+ | req.Method = " | ||
+ | req.ContentType = " | ||
+ | |||
+ | //Write POST stream | ||
+ | StreamWriter sw = new StreamWriter(req.GetRequestStream(), | ||
+ | sw.Write(postData); | ||
+ | sw.Close(); | ||
+ | |||
+ | //Send it and see if it was okay | ||
+ | HttpWebResponse resp = null; | ||
+ | try | ||
+ | { | ||
+ | resp = (HttpWebResponse)req.GetResponse(); | ||
+ | |||
+ | //This should read " | ||
+ | string r = new StreamReader(resp.GetResponseStream()).ReadToEnd(); | ||
+ | } | ||
+ | catch (WebException ex) | ||
+ | { | ||
+ | string err = new StreamReader(ex.Response.GetResponseStream()).ReadToEnd(); | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | |||
+ | /** | ||
+ | * RC4.NET 1.0 | ||
+ | * | ||
+ | * RC4.NET is a petite library that allows you to use RC4 | ||
+ | * encryption easily in the .NET Platform. It's OO and can | ||
+ | * produce outputs in binary and hex. | ||
+ | * | ||
+ | * (C) Copyright 2006 Mukul Sabharwal [http:// | ||
+ | | ||
+ | * | ||
+ | * @link http:// | ||
+ | * @author Mukul Sabharwal < | ||
+ | * @version $Id: RC4.cs,v 1.0 2006/03/19 15:35:24 mukul Exp $ | ||
+ | * @copyright Copyright © 2006 Mukul Sabharwal | ||
+ | * @license http:// | ||
+ | * @package RC4.NET | ||
+ | */ | ||
+ | public class RC4 | ||
+ | { | ||
+ | /** | ||
+ | * Get ASCII Integer Code | ||
+ | * | ||
+ | * @param char ch Character to get ASCII value for | ||
+ | * @access private | ||
+ | * @return int | ||
+ | */ | ||
+ | private static int ord(char ch) | ||
+ | { | ||
+ | return (int)(Encoding.GetEncoding(1252).GetBytes(ch + "" | ||
+ | } | ||
+ | /** | ||
+ | * Get character representation of ASCII Code | ||
+ | * | ||
+ | * @param int i ASCII code | ||
+ | * @access private | ||
+ | * @return char | ||
+ | */ | ||
+ | private static char chr(int i) | ||
+ | { | ||
+ | byte[] bytes = new byte[1]; | ||
+ | bytes[0] = (byte)i; | ||
+ | return Encoding.GetEncoding(1252).GetString(bytes)[0]; | ||
+ | } | ||
+ | /** | ||
+ | * Convert Hex to Binary (hex2bin) | ||
+ | * | ||
+ | * @param string packtype Rudimentary in this implementation | ||
+ | * @param string datastring Hex to be packed into Binary | ||
+ | * @access private | ||
+ | * @return string | ||
+ | */ | ||
+ | private static string pack(string packtype, string datastring) | ||
+ | { | ||
+ | int i, j, datalength, packsize; | ||
+ | byte[] bytes; | ||
+ | char[] hex; | ||
+ | string tmp; | ||
+ | |||
+ | datalength = datastring.Length; | ||
+ | packsize = (datalength / 2) + (datalength % 2); | ||
+ | bytes = new byte[packsize]; | ||
+ | hex = new char[2]; | ||
+ | |||
+ | for (i = j = 0; i < datalength; i += 2) | ||
+ | { | ||
+ | hex[0] = datastring[i]; | ||
+ | if (datalength - i == 1) | ||
+ | hex[1] = ' | ||
+ | else | ||
+ | hex[1] = datastring[i + 1]; | ||
+ | tmp = new string(hex, 0, 2); | ||
+ | try { bytes[j++] = byte.Parse(tmp, | ||
+ | catch { } /* grin */ | ||
+ | } | ||
+ | return Encoding.GetEncoding(1252).GetString(bytes); | ||
+ | } | ||
+ | /** | ||
+ | * Convert Binary to Hex (bin2hex) | ||
+ | * | ||
+ | * @param string bindata Binary data | ||
+ | * @access public | ||
+ | * @return string | ||
+ | */ | ||
+ | public static string bin2hex(string bindata) | ||
+ | { | ||
+ | int i; | ||
+ | byte[] bytes = Encoding.GetEncoding(1252).GetBytes(bindata); | ||
+ | string hexString = ""; | ||
+ | for (i = 0; i < bytes.Length; | ||
+ | { | ||
+ | hexString += bytes[i].ToString(" | ||
+ | } | ||
+ | return hexString; | ||
+ | } | ||
+ | /** | ||
+ | * The symmetric encryption function | ||
+ | * | ||
+ | * @param string pwd Key to encrypt with (can be binary of hex) | ||
+ | * @param string data Content to be encrypted | ||
+ | * @param bool ispwdHex Key passed is in hexadecimal or not | ||
+ | * @access public | ||
+ | * @return string | ||
+ | */ | ||
+ | public static string Encrypt(string pwd, string data, bool ispwdHex) | ||
+ | { | ||
+ | int a, i, j, k, tmp, pwd_length, data_length; | ||
+ | int[] key, box; | ||
+ | byte[] cipher; | ||
+ | //string cipher; | ||
+ | |||
+ | if (ispwdHex) | ||
+ | pwd = pack(" | ||
+ | pwd_length = pwd.Length; | ||
+ | data_length = data.Length; | ||
+ | key = new int[256]; | ||
+ | box = new int[256]; | ||
+ | cipher = new byte[data.Length]; | ||
+ | //cipher = ""; | ||
+ | |||
+ | for (i = 0; i < 256; i++) | ||
+ | { | ||
+ | key[i] = ord(pwd[i % pwd_length]); | ||
+ | box[i] = i; | ||
+ | } | ||
+ | for (j = i = 0; i < 256; i++) | ||
+ | { | ||
+ | j = (j + box[i] + key[i]) % 256; | ||
+ | tmp = box[i]; | ||
+ | box[i] = box[j]; | ||
+ | box[j] = tmp; | ||
+ | } | ||
+ | for (a = j = i = 0; i < data_length; | ||
+ | { | ||
+ | a = (a + 1) % 256; | ||
+ | j = (j + box[a]) % 256; | ||
+ | tmp = box[a]; | ||
+ | box[a] = box[j]; | ||
+ | box[j] = tmp; | ||
+ | k = box[((box[a] + box[j]) % 256)]; | ||
+ | cipher[i] = (byte)(ord(data[i]) ^ k); | ||
+ | //cipher += chr(ord(data[i]) ^ k); | ||
+ | } | ||
+ | return Encoding.GetEncoding(1252).GetString(cipher); | ||
+ | //return cipher; | ||
+ | } | ||
+ | /** | ||
+ | * Decryption, recall encryption | ||
+ | * | ||
+ | * @param string pwd Key to decrypt with (can be binary of hex) | ||
+ | * @param string data Content to be decrypted | ||
+ | * @param bool ispwdHex Key passed is in hexadecimal or not | ||
+ | * @access public | ||
+ | * @return string | ||
+ | */ | ||
+ | public static string Decrypt(string pwd, string data, bool ispwdHex) | ||
+ | { | ||
+ | return Encrypt(pwd, | ||
+ | } | ||
+ | } | ||
+ | |||
+ | } | ||
+ | |||
+ | </ | ||