G
Guest
I am trying to get tracking information from UPS web site without much luck. I got this working in VB 60 without any problems by using WinInet functions
Here my test program. We need to get Tracking information from www.ups.com/ups.app/xml/track. When I tried to create the WebRequest with above ulr I get server not found. If I try www.ups.com, I do get connected but how I can post my message to ups.app/xml/track
I appricate your help in solving this problem
string url = "http://www.ups.com";
string UserName="testUser"
string UserPassword="testPW ";
string XmlRequest
string XmlResponse;
string hostname="wwwcie.ups.com"
string prefix = "ups.app/xml"
string service ="track"
//URL url = new URL(protocol + "://" + hostname + "/" + prefix + "/" + service)
url = "http://" + hostname + "/" + prefix + "/" + service
XmlRequest =
"<?xml version=1.0?>"
" <TrackRequest xml:lang=en-US>"
"<Request><TransactionReference>"
"<CustomerContext>Example 1</CustomerContext>"
"<XpciVersion>1.0001</XpciVersion>"
"</TransactionReference>"
"<RequestAction>Track</RequestAction>"
"<RequestOption>activity</RequestOption></Request>"
"<TrackingNumber>" + "1Z12345E0291980793" + "</TrackingNumber></TrackRequest>"
//WebProxy proxyObject = new WebProxy("http://wwwcie.ups.com:8080")
// Disable Proxy use when the host is local i.e. without periods
//proxyObject.BypassProxyOnLocal = true;
HttpWebRequest myHttpWebRequest = (HttpWebRequest) WebRequest.Create(url)
myHttpWebRequest.Credentials = new NetworkCredential(UserName,UserPassword )
myHttpWebRequest.Method = "POST"
myHttpWebRequest.KeepAlive = false
//myHttpWebRequest.Connection = "/ups.app/xml/track";
myHttpWebRequest.UserAgent = "Test XML Request";
myHttpWebRequest.ContentType = "application/x-www-form-urlencoded"
// Set the 'ContentLength' property of the WebRequest
myHttpWebRequest.ContentLength = XmlRequest.Length;
Stream SendStream=myHttpWebRequest.GetRequestStream();
ASCIIEncoding encodedData=new ASCIIEncoding()
byte[] byteArray=encodedData.GetBytes(XmlRequest)
SendStream.Write(byteArray,0,byteArray.Length)
HttpWebResponse WebResp = (HttpWebResponse) myHttpWebRequest.GetResponse()
// Now read the data from respons
//Get a readable stream from the server.
Stream RecvStream = WebResp.GetResponseStream()
byte[] readBuff = new byte[256]
int bytesread
XmlResponse = ""
//Read from the stream and write any data to the console
bytesread = RecvStream.Read( readBuff, 0, 256)
while( bytesread > 0 )
{
bytesread = RecvStream.Read( readBuff, 0, 256)
XmlResponse = XmlResponse + readBuff
MessageBox.Show(XmlResponse)
RecvStream.Close()
WebResp.Close();
Here my test program. We need to get Tracking information from www.ups.com/ups.app/xml/track. When I tried to create the WebRequest with above ulr I get server not found. If I try www.ups.com, I do get connected but how I can post my message to ups.app/xml/track
I appricate your help in solving this problem
string url = "http://www.ups.com";
string UserName="testUser"
string UserPassword="testPW ";
string XmlRequest
string XmlResponse;
string hostname="wwwcie.ups.com"
string prefix = "ups.app/xml"
string service ="track"
//URL url = new URL(protocol + "://" + hostname + "/" + prefix + "/" + service)
url = "http://" + hostname + "/" + prefix + "/" + service
XmlRequest =
"<?xml version=1.0?>"
" <TrackRequest xml:lang=en-US>"
"<Request><TransactionReference>"
"<CustomerContext>Example 1</CustomerContext>"
"<XpciVersion>1.0001</XpciVersion>"
"</TransactionReference>"
"<RequestAction>Track</RequestAction>"
"<RequestOption>activity</RequestOption></Request>"
"<TrackingNumber>" + "1Z12345E0291980793" + "</TrackingNumber></TrackRequest>"
//WebProxy proxyObject = new WebProxy("http://wwwcie.ups.com:8080")
// Disable Proxy use when the host is local i.e. without periods
//proxyObject.BypassProxyOnLocal = true;
HttpWebRequest myHttpWebRequest = (HttpWebRequest) WebRequest.Create(url)
myHttpWebRequest.Credentials = new NetworkCredential(UserName,UserPassword )
myHttpWebRequest.Method = "POST"
myHttpWebRequest.KeepAlive = false
//myHttpWebRequest.Connection = "/ups.app/xml/track";
myHttpWebRequest.UserAgent = "Test XML Request";
myHttpWebRequest.ContentType = "application/x-www-form-urlencoded"
// Set the 'ContentLength' property of the WebRequest
myHttpWebRequest.ContentLength = XmlRequest.Length;
Stream SendStream=myHttpWebRequest.GetRequestStream();
ASCIIEncoding encodedData=new ASCIIEncoding()
byte[] byteArray=encodedData.GetBytes(XmlRequest)
SendStream.Write(byteArray,0,byteArray.Length)
HttpWebResponse WebResp = (HttpWebResponse) myHttpWebRequest.GetResponse()
// Now read the data from respons
//Get a readable stream from the server.
Stream RecvStream = WebResp.GetResponseStream()
byte[] readBuff = new byte[256]
int bytesread
XmlResponse = ""
//Read from the stream and write any data to the console
bytesread = RecvStream.Read( readBuff, 0, 256)
while( bytesread > 0 )
{
bytesread = RecvStream.Read( readBuff, 0, 256)
XmlResponse = XmlResponse + readBuff
MessageBox.Show(XmlResponse)
RecvStream.Close()
WebResp.Close();