#include "stdafx.h"
using namespace System::Xml;
using namespace System::Text;
using namespace System;

String^ urlEncode(String^ toEncode)
{
	cli::array&ltwchar_t^>^ toEncodeCharacters = {L'$', L'&', L'+', L',', L'/', L':', L';', L'=', L'?', L'@', L' ', L'\"', L'<', L'>', L'#', L'%'};
    cli::array&ltString^>^ hexCodedChar = { "24", "26", "2B", "2C", "2F", "3A", "3B", "3D", "3F", "40", "20", "22", "3C", "3E", "23", "25"};

	String ^ encoded;

	for (int i = 0; i < toEncode->Length; i++)
	{
		wchar_t^ currentChar = toEncode[i];
		int indice = -1;
		for (int j = 0; j < toEncodeCharacters->Length; j++)
			if (currentChar->Equals(toEncodeCharacters[j])) // Detect the reserved character...
				indice = j;
		if (indice == -1)
			encoded += currentChar;
		else
			encoded += "%" + hexCodedChar[indice]; // ... and replace it by its hexadecimal code
	}

	return encoded;
}

int main(array&ltSystem::String ^> ^args)
{
	String^ xmlCampaign = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><agendize><account><campaign><name>Campagne0</name><color>#EC50FF</color><description>This is a campaign uploaded by c++</description></campaign></account></agendize>";
	
	//authentication
	System::Net::NetworkCredential^ myCred = gcnew System::Net::NetworkCredential("myusername","mypassword");
	System::Net::CredentialCache^ myCache = gcnew System::Net::CredentialCache;
	myCache->Add( gcnew Uri( "http://www.agendize.com/api/1.0/data" ), "Basic", myCred );

	// Create a request for the URL.   
	// Set the 'Method' property of the 'Webrequest' to 'POST'.
	System::Net::HttpWebRequest ^myHttpWebRequest = safe_cast&ltSystem::Net::HttpWebRequest^>(System::Net::HttpWebRequest::Create("http://localhost/selfservice/api/1.0/data"));
	myHttpWebRequest->Credentials = myCache;
	myHttpWebRequest->Method = "POST";
	
	String^ queryString = String::Concat( "key=", "3ac024abf8c72460764b965199f81808f4cb08", "&xml=",urlEncode(xmlCampaign) );
	ASCIIEncoding^ encoding = gcnew ASCIIEncoding;
	array&ltByte>^ queryByte = encoding->GetBytes( queryString );

	// Set the content type of the data being posted.
	myHttpWebRequest->ContentType = "application/x-www-form-urlencoded";

	// Set the content length of the String* being posted.
	myHttpWebRequest->ContentLength = queryByte->Length;

	System::IO::Stream^ newStream = myHttpWebRequest->GetRequestStream();

	newStream->Write( queryByte, 0, queryByte->Length );
	
	System::Net::WebResponse ^_WebResponse = myHttpWebRequest->GetResponse();
	// Open data stream:
	System::IO::Stream ^_WebStream = _WebResponse->GetResponseStream();
	// Read the data stream
	System::IO::StreamReader ^sreader = gcnew System::IO::StreamReader(_WebStream);
	String ^xmlContent = sreader->ReadToEnd();
	
	// Cleanup
	_WebStream->Close();
	_WebResponse->Close();

	// Close the Stream Object*.
	newStream->Close();
    #Analyzes the xml content and return the response code
	XmlDocument ^ xmlDoc = gcnew XmlDocument();
	xmlDoc->LoadXml(xmlContent);
	XmlNodeList ^ listeNoeud = xmlDoc->GetElementsByTagName("result");
    Console::WriteLine(listeNoeud->Item(0)->InnerText);
    return 0;
}