I am creating a web application using VB.NET which tracks UPS orders using their XML web service.
I have followed their documentation to the best of my skills and looked all over the internet to see how I can do this.
What I currently have so far is below. However the XML returned and displayed in the UPSResponse is wrong, the returned XML says "The XML document is not well formed" for what I sent. But I have come to a loss on how to make this work.
Hope somebody can help,
Public Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim strPayload As String
strPayload = "<?xml version='1.0'?>"
strPayload += "<AccessRequest xml:lang='en-US'>"
strPayload += " <AccessLicenseNumber>xxxxxx</AccessLicenseNumber>"
strPayload += " <UserId>xxxxxx</UserId>"
strPayload += " <Password>xxxxxx</Password>"
strPayload += "</AccessRequest>"
strPayload += "<?xml version=""1.0""?>"
strPayload += "<TrackRequest xml:lang='en-US'>"
strPayload += " <Request>"
strPayload += " <TransactionReference>"
strPayload += " <CustomerContext>"
strPayLoad += " <InternalKey>guidlikesubstance</InternalKey>"
strPayLoad += " </CustomerContext>"
strPayload += " <XpciVersion>1.0</XpciVersion>"
strPayload += " </TransactionReference>"
strPayload += " <RequestAction>Track</RequestAction>"
strPayload += " </Request>"
strPayLoad += " <TrackingNumber>1Z12345E0291980793</TrackingNumber>"
strPayLoad += "</TrackRequest>"
PostPage("https://wwwcie.ups.com/ups.app/xml/Track", strPayload)
End Sub
Public Sub PostPage(ByVal url As String, ByVal payload As String)
Dim result As WebResponse
Try
Dim req As HttpWebRequest
Dim RequestStream As Stream
Dim ReceiveStream As Stream
Dim encode As Encoding
Dim sr As StreamReader
req = HttpWebRequest.Create(url)
'Create the proxy class instance
Dim prxy as New WebProxy("xxxxxx:8080", true)
'Set proxy credentials for access through moog proxy server
Dim myCred As New NetworkCredential("xxxxxx","xxxxxx","xx" )
prxy.Credentials = myCred
'Specify that the HttpWebRequest should use the proxy server
req.Proxy = prxy
req.Method = "POST"
req.ContentType = "application/x-www-form-urlencoded"
Dim SomeBytes() As Byte
Dim UrlEncoded As New StringBuilder()
Dim reserved() As Char = {ChrW(63), ChrW(61), ChrW(38)}
If payload <> Nothing Then
Dim i As Integer = 0
Dim j As Integer
While i < payload.Length
j = payload.IndexOfAny(reserved, i)
If j = -1 Then
UrlEncoded.Append(HttpUtility.UrlEncode(payload.Substring(i, payload.Length - i)))
Exit While
End If
UrlEncoded.Append(HttpUtility.UrlEncode(payload.Substring(i, j - i)))
UrlEncoded.Append(payload.Substring(j, 1))
i = j + 1
End While
SomeBytes = System.Text.Encoding.UTF8.GetBytes(UrlEncoded.ToString())
req.ContentLength = SomeBytes.Length
RequestStream = req.GetRequestStream()
RequestStream.Write(SomeBytes, 0, SomeBytes.Length)
RequestStream.Close()
Else
req.ContentLength = 0
End If
result = req.GetResponse()
ReceiveStream = result.GetResponseStream()
encode = System.Text.Encoding.GetEncoding("utf-8")
sr = New StreamReader(ReceiveStream, encode)
Dim read(65536) As Char
Dim count As Integer = sr.Read(read, 0, 65536)
Do While count > 0
Dim str As String = New String(read, 0, count)
Dim sw As StreamWriter = New StreamWriter(Server.MapPath("UPSResponse.xml"))
' Add some text to the file.
sw.Write(str)
sw.Close()
count = sr.Read(read, 0, 65536)
Loop
Catch Exc As Exception
Dim respErr As String = Exc.Message
Response.Write("The request URI could not be found or was malformed<br>" & respErr)
Finally
If Not result Is Nothing Then
result.Close()
End If
End Try
Response.redirect("UPSResponse.xml")
End Sub
You are essentially passing 2 xml strings via your strPayload. Now I'm not sure how UPS's receiving object parses it, but my guess is that its trying to parse the string as a single XML document which if that is the case it will return the error its not
well formed.
You are only allowed 1 root in a single XML document. While in yours you have 2 (<AccessRequest> and <TrackRequest>)
You are also ony allowed one PI (Processing Instruction) that defines what type of document it is (<?xml version='1.0'?>)
The error message I recieve back from UPS is as follows
<TrackResponse>
−
<Response>
<TransactionReference/>
<ResponseStatusCode>0</ResponseStatusCode>
<ResponseStatusDescription>Failure</ResponseStatusDescription>
−
<Error>
<ErrorSeverity>Hard</ErrorSeverity>
<ErrorCode>10001</ErrorCode>
<ErrorDescription>The XML document is not well formed</ErrorDescription>
</Error>
</Response>
</TrackResponse>
The only problem is UPS say to send both bits of XML data together, the first is to autheticate myself as a user allowed to connect to their server, the second is the request the tracking information, so I blieve it is ok for me to send both sets of XML
data togeher.
I have tried just sending each xml sets on their own and still recieve the same mesage.
I was wondering if I am passing the XML data to them in the correct format in the outputStream?
before you send it to them, write out your outputstream so you can see how its formated. and if you can post your XML here so we can see exactly how its being sent to them.
After this I tried sending this variable but this time got this error message back:
<TrackResponse>
−
<Response>
−
<TransactionReference>
<XpciVersion>1.0</XpciVersion>
</TransactionReference>
<ResponseStatusCode>0</ResponseStatusCode>
<ResponseStatusDescription>Failure</ResponseStatusDescription>
−
<Error>
<ErrorSeverity>Hard</ErrorSeverity>
<ErrorCode>250000</ErrorCode>
<ErrorDescription>Missing XML declaration in the XML document</ErrorDescription>
</Error>
</Response>
</TrackResponse>
I was like, yes, I am on to something, so I added the extra data in (the tracking info) but got the XML is not well formed correctly again.
In response to the Documentation I have checked this and it seems fine. The is a paragraph that displays an example tracker request message, this is copied to below:
listen, what are you trying to do. You just simply need to use a xmlreader to read the document, ask ups to send you a a copy of the xml model to see what elements are being used this should tell you how to use xpath with your xml reader. I think the problem
is that you are getting way cought up in a work load that simply broken down to a simple xmlreader process. Read up on using the xmlreader information on msdn library and how to parse data from an xml document through webservices. All the code you wrote above
shows no relation to any xml. Non-what-so-ever. First of All you arent even using any xpaths
look up xpaths from www.altova.com find out how to use xpaths and what they are this will show how to tree through elements with in an xml file.
His issue has nothing to do with XMLReader. He's able to read the returning XML from UPS just fine (as he posted the returning XML above.) The issue has to do with the XML that is being sent to UPS. The response he's getting back from UPS says that the
XML is not valid.
So either there is indeed an issue with how the XML is being encoded >> sent to UPS >> and then decoded on thier end, or a problem with the XML itself (which has been validated as seperate documents) or there's a step that is being missed somewhere. Thus
the reason for the request for UPS's documentation.
PU43X
Member
31 Points
51 Posts
UPS Tracking - WebRequest Using XML
Aug 15, 2007 01:14 PM|LINK
Hi There,
I am creating a web application using VB.NET which tracks UPS orders using their XML web service.
I have followed their documentation to the best of my skills and looked all over the internet to see how I can do this.
What I currently have so far is below. However the XML returned and displayed in the UPSResponse is wrong, the returned XML says "The XML document is not well formed" for what I sent. But I have come to a loss on how to make this work.
Hope somebody can help,
Public Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim strPayload As String strPayload = "<?xml version='1.0'?>" strPayload += "<AccessRequest xml:lang='en-US'>" strPayload += " <AccessLicenseNumber>xxxxxx</AccessLicenseNumber>" strPayload += " <UserId>xxxxxx</UserId>" strPayload += " <Password>xxxxxx</Password>" strPayload += "</AccessRequest>" strPayload += "<?xml version=""1.0""?>" strPayload += "<TrackRequest xml:lang='en-US'>" strPayload += " <Request>" strPayload += " <TransactionReference>" strPayload += " <CustomerContext>" strPayLoad += " <InternalKey>guidlikesubstance</InternalKey>" strPayLoad += " </CustomerContext>" strPayload += " <XpciVersion>1.0</XpciVersion>" strPayload += " </TransactionReference>" strPayload += " <RequestAction>Track</RequestAction>" strPayload += " </Request>" strPayLoad += " <TrackingNumber>1Z12345E0291980793</TrackingNumber>" strPayLoad += "</TrackRequest>" PostPage("https://wwwcie.ups.com/ups.app/xml/Track", strPayload) End Sub Public Sub PostPage(ByVal url As String, ByVal payload As String) Dim result As WebResponse Try Dim req As HttpWebRequest Dim RequestStream As Stream Dim ReceiveStream As Stream Dim encode As Encoding Dim sr As StreamReader req = HttpWebRequest.Create(url) 'Create the proxy class instance Dim prxy as New WebProxy("xxxxxx:8080", true) 'Set proxy credentials for access through moog proxy server Dim myCred As New NetworkCredential("xxxxxx","xxxxxx","xx" ) prxy.Credentials = myCred 'Specify that the HttpWebRequest should use the proxy server req.Proxy = prxy req.Method = "POST" req.ContentType = "application/x-www-form-urlencoded" Dim SomeBytes() As Byte Dim UrlEncoded As New StringBuilder() Dim reserved() As Char = {ChrW(63), ChrW(61), ChrW(38)} If payload <> Nothing Then Dim i As Integer = 0 Dim j As Integer While i < payload.Length j = payload.IndexOfAny(reserved, i) If j = -1 Then UrlEncoded.Append(HttpUtility.UrlEncode(payload.Substring(i, payload.Length - i))) Exit While End If UrlEncoded.Append(HttpUtility.UrlEncode(payload.Substring(i, j - i))) UrlEncoded.Append(payload.Substring(j, 1)) i = j + 1 End While SomeBytes = System.Text.Encoding.UTF8.GetBytes(UrlEncoded.ToString()) req.ContentLength = SomeBytes.Length RequestStream = req.GetRequestStream() RequestStream.Write(SomeBytes, 0, SomeBytes.Length) RequestStream.Close() Else req.ContentLength = 0 End If result = req.GetResponse() ReceiveStream = result.GetResponseStream() encode = System.Text.Encoding.GetEncoding("utf-8") sr = New StreamReader(ReceiveStream, encode) Dim read(65536) As Char Dim count As Integer = sr.Read(read, 0, 65536) Do While count > 0 Dim str As String = New String(read, 0, count) Dim sw As StreamWriter = New StreamWriter(Server.MapPath("UPSResponse.xml")) ' Add some text to the file. sw.Write(str) sw.Close() count = sr.Read(read, 0, 65536) Loop Catch Exc As Exception Dim respErr As String = Exc.Message Response.Write("The request URI could not be found or was malformed<br>" & respErr) Finally If Not result Is Nothing Then result.Close() End If End Try Response.redirect("UPSResponse.xml") End SubDiamsorn
Contributor
2119 Points
384 Posts
Re: UPS Tracking - WebRequest Using XML
Aug 15, 2007 02:54 PM|LINK
There is an issue in how your forming your XML.
You are essentially passing 2 xml strings via your strPayload. Now I'm not sure how UPS's receiving object parses it, but my guess is that its trying to parse the string as a single XML document which if that is the case it will return the error its not well formed.
You are only allowed 1 root in a single XML document. While in yours you have 2 (<AccessRequest> and <TrackRequest>)
You are also ony allowed one PI (Processing Instruction) that defines what type of document it is (<?xml version='1.0'?>)
Your 2 documents seperate are well formed.
My Blog
PU43X
Member
31 Points
51 Posts
Re: UPS Tracking - WebRequest Using XML
Aug 16, 2007 07:58 AM|LINK
Hi There, thanks for the reply to my problem.
The error message I recieve back from UPS is as follows
The only problem is UPS say to send both bits of XML data together, the first is to autheticate myself as a user allowed to connect to their server, the second is the request the tracking information, so I blieve it is ok for me to send both sets of XML data togeher.
I have tried just sending each xml sets on their own and still recieve the same mesage.
I was wondering if I am passing the XML data to them in the correct format in the outputStream?
Thanks for all the help.
Diamsorn
Contributor
2119 Points
384 Posts
Re: UPS Tracking - WebRequest Using XML
Aug 16, 2007 01:46 PM|LINK
before you send it to them, write out your outputstream so you can see how its formated. and if you can post your XML here so we can see exactly how its being sent to them.
My Blog
kalvagadda
Participant
1513 Points
281 Posts
Re: UPS Tracking - WebRequest Using XML
Aug 16, 2007 10:08 PM|LINK
Hi,
can u post link where we can find ups web service documentation that tells us what xml we should post.
I am guessing the way u r sending two xml should be different as it would be difficult for them to seperate both xml.
i just want to check their documentation.
Thanks,
Kiran
PU43X
Member
31 Points
51 Posts
Re: UPS Tracking - WebRequest Using XML
Aug 17, 2007 08:46 AM|LINK
Hi There, thanks for all the help you are giving me, I printed out the UrlEncoded variable that I believe is being sent to UPS and got this Response
"%3c?xml+version='1.0'?%3e%3cAccessRequest+xml%3alang='en-US'%3e++%3cAccessLicenseNumber%xxxxxx%3c%2fAccessLicenseNumber%3e++%3cUserId%3exxxxxx%3c%2fUserId%3e++%3cPassword%3exxxxxx%3c%2fPassword%3e%3c%2fAccessRequest%3e%3c?xml+version='1.0'?%3e%3cAccessRequest+xml%3alang='en-US'%3e++%3cAccessLicenseNumber%3e5C10B70DECE1A510%3c%2fAccessLicenseNumber%3e++%3cUserId%3egdavies83%3c%2fUserId%3e++%3cPassword%3eluxem*&86%3c%2fPassword%3e%3c%2fAccessRequest%3e"
I was like, yes, I am on to something, so I added the extra data in (the tracking info) but got the XML is not well formed correctly again.
In response to the Documentation I have checked this and it seems fine. The is a paragraph that displays an example tracker request message, this is copied to below:
If you would like a copy of the documentation, please let me know and Ill send it over.
Many Thanks for your continued help!
kalvagadda
Participant
1513 Points
281 Posts
Re: UPS Tracking - WebRequest Using XML
Aug 17, 2007 02:25 PM|LINK
Hi,
can u send link whre i can find documentation.
Thanks,
Kiran
Srheal
Member
159 Points
38 Posts
Re: UPS Tracking - WebRequest Using XML
Aug 17, 2007 05:32 PM|LINK
listen, what are you trying to do. You just simply need to use a xmlreader to read the document, ask ups to send you a a copy of the xml model to see what elements are being used this should tell you how to use xpath with your xml reader. I think the problem is that you are getting way cought up in a work load that simply broken down to a simple xmlreader process. Read up on using the xmlreader information on msdn library and how to parse data from an xml document through webservices. All the code you wrote above shows no relation to any xml. Non-what-so-ever. First of All you arent even using any xpaths
look up xpaths from www.altova.com find out how to use xpaths and what they are this will show how to tree through elements with in an xml file.
Diamsorn
Contributor
2119 Points
384 Posts
Re: UPS Tracking - WebRequest Using XML
Aug 17, 2007 05:39 PM|LINK
His issue has nothing to do with XMLReader. He's able to read the returning XML from UPS just fine (as he posted the returning XML above.) The issue has to do with the XML that is being sent to UPS. The response he's getting back from UPS says that the XML is not valid.
So either there is indeed an issue with how the XML is being encoded >> sent to UPS >> and then decoded on thier end, or a problem with the XML itself (which has been validated as seperate documents) or there's a step that is being missed somewhere. Thus the reason for the request for UPS's documentation.
My Blog
PU43X
Member
31 Points
51 Posts
Re: UPS Tracking - WebRequest Using XML
Aug 21, 2007 07:49 AM|LINK
Hi There, following documentation is available from the below link.
You cannot get it from UPS unless you have an account with them.
Hope this helps, and thanks for the help you are giving me with this problem.
www.pu43x.co.uk/ups/docs/order_tracking.rar