ThyRock,
Sorry to get back to you so late...was at work late and just finished
with the family thing dinner etc.....
I used the HTML developers guide because it was easy to develop....here
is the code along with my USPS code
I am using webservices for all three DHL, UPS, and USPS...damn fedex
can't get there stinkin docs! I changed my USPS code to a stringbuilder
like your example..code looks a little more cluttered using
XmlTextWriter, MemoryStream, and XmlDocument...
I have some merchant components coded, we can exhange some code if you
would like...The ups xml service is pretty straight forward maybe I
will code for the xml service this weekend, if you want to wait till
then..otherwise this one works
Cheers,
Precept
-----UPS
[WebMethod]
public decimal GetPrice(string svcCode, string rateChart,string
shipperZIP,string receiverZIP,
string receiverCountry,string pkgWeight,string
isResidential, string isCOD,
string isSatPickup,string isSatDelivery, string pkgType)
{
decimal shippingRate = 0;
WebRequest myWebReq;
WebResponse myWebResp;
StreamReader myStream;
string URLRequest;
string responseLine;
URLRequest =
BuildRequest(svcCode,rateChart,shipperZIP,receiverZIP,"US",pkgWeight,isResidential,isCOD,isSatPickup,isSatDelivery,pkgType);
myWebReq = WebRequest.Create(URLRequest);
myWebResp = myWebReq.GetResponse();
myStream = new StreamReader(myWebResp.GetResponseStream(),
System.Text.Encoding.ASCII);
try
{
responseLine = myStream.ReadLine();
while(responseLine != null)
{
responseLine = myStream.ReadLine();
if (responseLine.Length > 0)
{
if (responseLine.ToLower().IndexOf("upsonline", 0,
responseLine.Length) > -1)
{
string[] arr;
char[] perct = new char[1];
perct[0] = '%';
arr = responseLine.Split(perct);
if (arr[3].Substring(0,11).ToLower() == "0000success")
shippingRate = Convert.ToDecimal(arr[12]);
responseLine = null;
}
}
}
}
catch(Exception e)
{
throw new AppException("Error occured trying to calculate UPS
shiping", e);
}
return shippingRate;
}
private string BuildRequest(string svcCode, string rateChart, string
shipperZIP, string receiverZIP,
string receiverCountry, string pkgWeight, string isResidential,
string isCOD,
string isSatPickup, string isSatDelivery, string pkgType)
{
System.Text.StringBuilder ups = new System.Text.StringBuilder();
ups.Append("
http://www.ups.com/using/services/rave/qcost_dss.cgi?");
ups.Append("AppVersion=1.2&AcceptUPSLicenseAgreement=YES&");
ups.Append("ResponseType=application/x-ups-rss&ActionCode=3&");
ups.Append("ServiceLevelCode=" + svcCode + "&RateChart=" + rateChart
+ "&");
ups.Append("ShipperPostalCode=" + shipperZIP +
"&ConsigneePostalCode=" + receiverZIP + "&");
ups.Append("ConsigneeCountry=" + receiverCountry +
"&PackageActualWeight=" + pkgWeight + "&");
ups.Append("ResidentialInd=" + isResidential + "&CODInd=" + isCOD +
"&SatDelivInd=" + isSatDelivery);
ups.Append("&SatPickupInd=" + isSatPickup + "&PackagingType=" +
pkgType);
return
ups.ToString();
}
----USPS
[WebMethod]
public decimal GetPrice(string userID, string password, string
zipOrigin, string zipDestination,
string pounds, string ounces, string container, string size, string
service)
{
WebRequest myWebReq;
WebResponse myWebResp;
StreamReader myStream;
string UrlRequest;
decimal shippingRate = 0;
UrlRequest = BuildRequest(userID, password, zipOrigin,
zipDestination, pounds, ounces, container, size, service);
myWebReq = WebRequest.Create(UrlRequest);
try
{
myWebResp = myWebReq.GetResponse();
myStream = new StreamReader(myWebResp.GetResponseStream(),
System.Text.Encoding.ASCII);
XmlTextReader xmlReader = new XmlTextReader(myStream);
System.Text.StringBuilder response = new
System.Text.StringBuilder();
while (xmlReader.Read())
{
if (xmlReader.MoveToContent() == XmlNodeType.Element &&
xmlReader.Name.ToUpper() == "RATE")
{
shippingRate = Convert.ToDecimal(xmlReader.ReadString());
}
}
return shippingRate;
}
catch(Exception e)
{
throw new AppException("Error occured trying to calculate USPS
shiping", e);
}
}
private string BuildRequest(string userID, string password, string
zipOrigin,
string zipDestination, string pounds, string ounces, string
container,
string size, string service)
{
System.Text.StringBuilder usps = new System.Text.StringBuilder();
usps.Append("
http://testing.shippingapis.com/ShippingAPITest.dll");
usps.Append( "?API=RateV2&XML=" );
usps.Append( "<RateV2Request USERID=\"" );
usps.Append( userID + "\" " );
usps.Append( "PASSWORD=\"" );
usps.Append( password + "\">" );
usps.Append( "<Package ID=\"0\">" );
usps.Append( "<Service>" + service + "</Service>" );
usps.Append( "<ZipOrigination>" + zipOrigin + "</ZipOrigination>" );
usps.Append( "<ZipDestination>" + zipDestination +
"</ZipDestination>" );
usps.Append( "<Pounds>" + pounds + "</Pounds>" );
usps.Append( "<Ounces>" + ounces + "</Ounces>" );
usps.Append( "<Container>" + container + "</Container>" );
usps.Append( "<Size>" + size + "</Size>" );
usps.Append( "</Package></RateV2Request>" );
return usps.ToString();
}
Precept,
I tried the URI object and it will return a uri without encoding if you pass
the dontencode parameter set to true.
I tried the StringBuilder approach and it worked so I have move on
to
the
next step.
There are many ways of reaching the same end and I would like very much to
look at
your approach to using the UPS DHL services.
Thanks,
have
and
the
info
);
stringBuilder.Append( "<Size>" + size + "</Size>" );
stringBuilder.Append( "</Package></RateV2Request>" );
WebRequest webRequest = WebRequest.Create( stringBuilder.ToString()
);
try
{
WebResponse webResponse = webRequest.GetResponse();
Stream webResponseStream = webResponse.GetResponseStream();
// txtHTML is a TextArea control to display the raw response for
development
// purposes.
// You would unpack the xml document here and return the needed
values
Byte[] read = new Byte[512];
int bytes = webResponseStream.Read( read, 0, 512 );
txtHTML.InnerHtml = "";
while ( bytes > 0 )
{
// Note:
// The following assumes that the response uses UTF-8 as encoding.
// If the content is sent in a ANSI codepage like 932 use
something like
this:
// Encoding encode =
System.Text.Encoding.GetEncoding("shift-jis");
Encoding encode = System.Text.Encoding.GetEncoding( "utf-8" );
txtHTML.InnerHtml = txtHTML.InnerHtml +
encode.GetString(read,
it?