I know many people are looking for help developing their own XML code - like myself, I have tried asking and seen many recommendations to buy a control - but we learn from typing - not just plugging in a fix...
So - I finally figured this out myself with some trial and error - and I had promised to share when I had figured it out...
Disclaimer: I am fairly new to C# so it may not be in perfect format. No warranties or guarantees are provided. Code is provided as-is - use at your own risk... :)
I am unemployed - so if anyone is feeling grateful (or hiring) - feel free to email and thank me (I can accept donations too...). I should note - I did ask many, one person sent me his VB code - far from complete, I didn't use or even look at it since it was not for rate quotes and VB uses totally different calls vs C# which I am working on now.
This should be enough to get your started, I left in parts (commented out) from the sample that caused the sample to fail with UPS error 250005 as I have seen others with this error - might be the same failing code. (Error handling is done in the parser code).
If anyone needs help with the parser code - post here and I'll help - but that should be easy as I did it from the MS examples and the UPS .pdf file.
Sample code:
//------------------------------------------------------------
// UPS XML Rate Query Example in C#
// by Todd Nelson
//
// Method: string sendUPSXMLquery(string url,string reqXml)
// This method should be added to a Component class file - not in codebehind
//
// Returns:
// Success: XML string ready to be parsed
// Failure: error code in 'resultStr' XML string from UPS -or-
// Exception code from catch block
//
// Input:
// url - url i.e. "https://wwwcie.ups.com/ups.app/xml/Rate"
// reqXml - entire XML string like UPS sample
//
// sample reqXml string =
// "<?xml version=\"1.0\"?>"+
// "<AccessRequest xml:lang=\"en-US\">"+
// "<AccessLicenseNumber>YOURACCESSLICENSE</AccessLicenseNumber>" +
// "<UserId>YOURUPSUSERID</UserId>" +
// "<Password>YOURUPSPW</Password>" +
// "</AccessRequest>" +
// "<?xml version=\"1.0\"?>"+
// "<RatingServiceSelectionRequest xml:lang=\"en-US\">"+
// "<Request>"+
// "<TransactionReference>"+
// ... rest same as sample UPS string from .pdf file
//------------------------------------------------------------
internal static string sendUPSXMLquery(string url,string reqXml)
{
WebResponse result = null;
string resultStr="";
try
{
// This paragraph from MS Help file...
// The WebRequest and WebResponse classes use Secure Sockets Layer (SSL)
// to communicate with hosts that support SSL automatically.
// The decision to use SSL is made by the WebRequest class,
// based on the URI it is given.
// If the URI begins with "https:" then SSL is used;
// if the URI begins with "http:" SSL is not used.
// ...end MS Help file comment
WebRequest req = WebRequest.Create(url);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
byte[] SomeBytes = null;
// This sample taken from MS Example file - but:
// Rest of all commented area here prevented UPS code from executing
// apparently changed XML to a non-compatible format
// guess - UPS cannot handle reserved char replacement routine
// left it here commented so readers will know - if they
// had tried this MS example code - Now they know why it did not work
//
// StringBuilder UrlEncoded = new StringBuilder();
// Char[] reserved = {'?', '=', '&'};
if (reqXml != null)
{
//
// int i=0, j;
// while(i<reqXml.Length)
// {
// j=reqXml.IndexOfAny(reserved, i);
// if (j==-1)
// {
// UrlEncoded.Append(HttpUtility.UrlEncode(reqXml.Substring(i, reqXml.Length-i)));
// break;
// }
// UrlEncoded.Append(HttpUtility.UrlEncode(reqXml.Substring(i, j-i)));
// UrlEncoded.Append(reqXml.Substring(j,1));
// i = j+1;
// }
SomeBytes = Encoding.UTF8.GetBytes(reqXml.ToString());
req.ContentLength = SomeBytes.Length;
Stream newStream = req.GetRequestStream();
newStream.Write(SomeBytes, 0, SomeBytes.Length);
newStream.Close();
}
else
{
req.ContentLength = 0;
}
result = req.GetResponse();
Stream ReceiveStream = result.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
StreamReader sr = new StreamReader( ReceiveStream, encode );
Char[] read = new Char[256];
int count = sr.Read( read, 0, 256 );
while (count > 0)
{
String str = new String(read, 0, count);
resultStr += str;
count = sr.Read(read, 0, 256);
}
}
catch(Exception e)
{
resultStr = "Error: " + e.ToString();
}
finally
{
if ( result != null )
{
result.Close();
}
}
return resultStr;
}
Have Fun!
Todd Nelson
[email protected]