1

I am trying to validate the zipCode with City using UPS API "https://www.ups.com/ups.app/xml/AV".

Below is my code

string xmlRequest = "<?xml version=\"1.0\"?>" +
                "<AccessRequest>" +
                "<AccessLicenseNumber>xxxx</AccessLicenseNumber>" +
                "<UserId>xxx</UserId>" +
                "<Password>xxx</Password>" +
                "</AccessRequest>" +
                "<?xml version=\"1.0\"?>" +
                "<AddressValidationRequest xml:lang=\"en-US\">" +
                "<Request>" +
                "<TransactionReference>" +
                "<CustomerContext>Customer Data</CustomerContext>" +
                "<XpciVersion>1.0001</XpciVersion>" +
                "</TransactionReference>" +
                "<RequestAction>AV</RequestAction>" +
                "</Request>" +
                "<Address>" +
                "<PostalCode>" + "38305" + "</PostalCode>" +
                "<City>" + "TENNESSEE" + "</City>" +
                "</Address>" +
                "</AddressValidationRequest>";
                string url = "https://www.ups.com/ups.app/xml/AV";

                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
                //ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
                //ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11;


                HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                byte[] requestBytes = System.Text.Encoding.ASCII.GetBytes(xmlRequest);
                req.Method = "POST";
                req.ContentType = "application/x-www-form-urlencode";
                req.ContentLength = requestBytes.Length;
//                req.KeepAlive = false;

                //req.UseDefaultCredentials = true;
  //              req.UserAgent = "Patoooey";
                Stream requestStream = req.GetRequestStream();                
                requestStream.Write(requestBytes, 0, requestBytes.Length);
                requestStream.Close();
                HttpWebResponse res = (HttpWebResponse)req.GetResponse();

                StreamReader sr = new StreamReader(res.GetResponseStream(), System.Text.Encoding.Default);
                string backstr = sr.ReadToEnd();

It works fine on my local machine. but when i deploy it on live server. it gives error The underlying connection was closed: An unexpected error occurred on a receive._____ at System.Net.HttpWebRequest.GetRequestStream(TransportContext& context) at System.Net.HttpWebRequest.GetRequestStream()

I have tried setting ServicePointManager.SecurityProtocol to TLS, TLS11 or TLS12 but nothing is working on live server. Also tried request.KeepAlive = false, request.UseDefaultCredentials = true and request.UserAgent to "any word greater than 5 char".

Server is running Windows Server Data Center SP2 Your help will be appreciated. Thanks

mck
  • 978
  • 3
  • 14
  • 38
  • You're sending an XML body with a content type of `application/x-www-form-urlencode`: maybe the server is rejecting your invalid request? – Richard Jan 30 '18 at 08:41
  • Just tried the "application/xml" content type. but still the same issue. it is working fine on my local machine. only giving error on server. – mck Jan 30 '18 at 08:49
  • did u try this `ServicePointManager.ServerCertificateValidationCallback = (a,b,c,d) => true;` – levent Jan 30 '18 at 08:53
  • Tried but nothing changed. if i use the line ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; then i get the error underlying connection close and if i comment the ServicePointManager.SecurityProtocol then i get the following error The remote server returned an error: (403) Forbidden – mck Jan 30 '18 at 08:58
  • 1
    Did you ever get this figured out? – Rico Feb 20 '19 at 03:36

1 Answers1

0
  • Even i was facing same issue it was resolved by setting below code ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; .net picks the highest security protocol by default present in your server
  • Later for detailed understanding got below reference please refer in case of explanation Default SecurityProtocol in .NET 4.5
tejashiwini
  • 81
  • 1
  • 10