xml rpc tricks

Sunday, May 16, 2010

Post SAGE XMLDataStream Using ASP.NET

If you already have the XML DataStream service activated on your SAGE account, you can use your
SAGE ID and password in order to test your implementation. If you do not yet have the service activated
on your account, you can use our test account in order to test your implementation. Please note that the
test account will not return all of the real product and supplier information – since it is purely for testing.
However, it will return something for all of the fields for each query so that you can fully test your
implementation before subscribing to the service.
The test account Acct ID is 9000, Login ID is TestXMLDSUser, and the password is abc123.

public string SendCategoryXmlRequest()
{
string xmlString = GetCategoryXML();
string requestUrl = ConfigurationSettings.AppSettings["SageUrl"].ToString().Trim();
WebClient wc = new WebClient();
string response = wc.UploadString(requestUrl, "POST", xmlString);
return response;
}



//get the xml from the file and put into a string
private string GetCategoryXML()
{
StringBuilder sbXML = new StringBuilder();
sbXML.Append("");
sbXML.Append("3.1");
sbXML.Append("");
sbXML.Append("" + ConfigurationSettings.AppSettings["AcctID"].ToString().Trim() + "");
sbXML.Append("" + ConfigurationSettings.AppSettings["LoginID"].ToString().Trim() + "");
sbXML.Append("" + ConfigurationSettings.AppSettings["Password"].ToString().Trim() + "");
sbXML.Append("
");
sbXML.Append("");
sbXML.Append("1");
sbXML.Append("NAME");
sbXML.Append("
");
sbXML.Append("
");
return sbXML.ToString().Trim();
}

Friday, December 11, 2009

xml rpc tricks

While Xmlrpc in theory is a really nice feature, in practice it's really hard to work with. Never considered myself as a XML type of guy and now we're adding a whole new level of rpc urls, authentication etc.

I bet there might be some nice library out there, but when I need to get somethiung done I always take the easy way out - so here I poresent my quick and dirty code for posting a blog entry to your livejournal blog from C#:

I started out with how I wanted the C# code to look:



protected void LiveJournalPost(string sHeader, string sTxt, string Username, string Password, string RpcUrl)
{
}




And here's that code:



...
string sTxtPostData = @"

LJ.XMLRPC.postevent




username
" + Username + @"

password

" + Password + @"

event
" + sTxt + @"



subject
" + sHeader + @"

lineendings

pc

year
" + DateTime.Now.Year.ToString() + @"


mon
" + DateTime.Now.Month.ToString() + @"

day

" + DateTime.Now.Day.ToString() + @"

hour
" + DateTime.Now.Hour.ToString() + @"


min
" + DateTime.Now.Minute.ToString() + @"






";

WebHelper.PostXml(RpcUrl, sTxtPostData);


...






Building up the XML the RPC endpoint at Livejournal wants to see by just creating a long string.

And here's the WebHelper.PostXml code:




public static string PostXml(string url, string sPostData)
{
string sRet = "ERROR";
System.Net.WebClient oCli = null;
try
{
oCli = new WebClient();
oCli.Headers.Add("Content-Type", "text/xml");
byte[] byteArray = Encoding.ASCII.GetBytes(sPostData);
byte[] responseArray = oCli.UploadData(url, "POST", byteArray);
sRet = Encoding.ASCII.GetString(responseArray);

}
catch (WebException ex)
{
//Console.Write(ex.Message);
}
if (oCli != null)
{
oCli.Dispose();
oCli = null;
}
return sRet;

}


So, calling it is like:

LiveJournalPost("The post header", "And some post content bla bla", "ljusername", "ljpassword", "http://www.livejournal.com/interface/xmlrpc");

Followers