public async Task <VoidShipmentResponse> Cancel(string shipmentIdentificationNumber, UpsCredentials credentials) { if (shipmentIdentificationNumber == null) { throw new ArgumentNullException(nameof(shipmentIdentificationNumber)); } if (credentials == null) { throw new ArgumentNullException(nameof(credentials)); } var client = _httpClientFactory.GetHttpClient(); var security = new AccessRequestType { AccessLicenseNumber = credentials.LicenseNumber, Password = credentials.Password, UserId = credentials.UserName }; var request = new VoidShipmentRequest { Request = new RequestType { RequestAction = "1", TransactionReferenceType = new TransactionReference { CustomerContext = "Your Test Case Summary Description", XpciVersion = "1.0014" } }, ShipmentIdentificationNumber = shipmentIdentificationNumber }; var serializerSecurity = new XmlSerializer(typeof(AccessRequestType)); var serializerBody = new XmlSerializer(typeof(VoidShipmentRequest)); var xmlSecurity = ""; var xmlBody = ""; using (var sww = new StringWriter()) { using (var writer = XmlWriter.Create(sww)) { serializerSecurity.Serialize(writer, security); xmlSecurity = sww.ToString(); } } using (var sww = new StringWriter()) { using (var writer = XmlWriter.Create(sww)) { serializerBody.Serialize(writer, request); xmlBody = sww.ToString(); } } var xml = xmlSecurity + "" + xmlBody; var body = new StringContent(xml); var req = new HttpRequestMessage { Method = HttpMethod.Post, Content = body, RequestUri = new Uri(_voidUrl) }; var serializedContent = await client.SendAsync(req).ConfigureAwait(false); var res = await serializedContent.Content.ReadAsStringAsync(); var deserializer = new XmlSerializer(typeof(LocatorResponse)); using (TextReader reader = new StringReader(res)) { return((VoidShipmentResponse)deserializer.Deserialize(reader)); } }
static void Main() { try { VoidService voidService = new VoidService(); VoidShipmentRequest voidRequest = new VoidShipmentRequest(); RequestType request = new RequestType(); String[] requestOption = { "1" }; request.RequestOption = requestOption; voidRequest.Request = request; VoidShipmentRequestVoidShipment voidShipment = new VoidShipmentRequestVoidShipment(); voidShipment.ShipmentIdentificationNumber = "1ZISDE016691676846"; voidRequest.VoidShipment = voidShipment; UPSSecurity upss = new UPSSecurity(); UPSSecurityServiceAccessToken upssSvcAccessToken = new UPSSecurityServiceAccessToken(); upssSvcAccessToken.AccessLicenseNumber = "4CF6E9703C30E4B6"; upss.ServiceAccessToken = upssSvcAccessToken; UPSSecurityUsernameToken upssUsrNameToken = new UPSSecurityUsernameToken(); upssUsrNameToken.Username = "******"; upssUsrNameToken.Password = "******"; upss.UsernameToken = upssUsrNameToken; voidService.UPSSecurityValue = upss; System.Net.ServicePointManager.CertificatePolicy = new TrustAllCertificatePolicy(); Console.WriteLine(voidRequest); VoidShipmentResponse voidResponse = voidService.ProcessVoid(voidRequest); Console.WriteLine("The transaction was a " + voidResponse.Response.ResponseStatus.Description); Console.WriteLine("The shipment has been : " + voidResponse.SummaryResult.Status.Description); Console.ReadKey(); } catch (System.Web.Services.Protocols.SoapException ex) { Console.WriteLine(""); Console.WriteLine("---------Void Web Service returns error----------------"); Console.WriteLine("---------\"Hard\" is user error \"Transient\" is system error----------------"); Console.WriteLine("SoapException Message= " + ex.Message); Console.WriteLine(""); Console.WriteLine("SoapException Category:Code:Message= " + ex.Detail.LastChild.InnerText); Console.WriteLine(""); Console.WriteLine("SoapException XML String for all= " + ex.Detail.LastChild.OuterXml); Console.WriteLine(""); Console.WriteLine("SoapException StackTrace= " + ex.StackTrace); Console.WriteLine("-------------------------"); Console.WriteLine(""); } catch (System.ServiceModel.CommunicationException ex) { Console.WriteLine(""); Console.WriteLine("--------------------"); Console.WriteLine("CommunicationException= " + ex.Message); Console.WriteLine("CommunicationException-StackTrace= " + ex.StackTrace); Console.WriteLine("-------------------------"); Console.WriteLine(""); } catch (Exception ex) { Console.WriteLine(""); Console.WriteLine("-------------------------"); Console.WriteLine(" Generaal Exception= " + ex.Message); Console.WriteLine(" Generaal Exception-StackTrace= " + ex.StackTrace); Console.WriteLine("-------------------------"); } finally { Console.ReadKey(); } }