2017-02-23 76 views
5

我在我的asp.net mvc 5應用程序中實現UPS跟蹤API。我在下面創建了方法。獲取「由於遠程方已關閉傳輸流而導致身份驗證失敗。」同時使API調用到UPS跟蹤休息服務

public virtual Core.UPSTrackingResponse.UPSTrackingResponse TrackUPS(string trackingNumber) 
{ 
    Core.UPSTrackingRequest.UPSTrackingRequest requestModal = new Core.UPSTrackingRequest.UPSTrackingRequest(); 
    requestModal.UPSSecurity.ServiceAccessToken.AccessLicenseNumber = UPSAccessKey; 
    requestModal.UPSSecurity.UsernameToken.Username = UPSUserId; 
    requestModal.UPSSecurity.UsernameToken.Password = UPSPassword; 
    requestModal.TrackRequest.InquiryNumber = trackingNumber; 
    requestModal.TrackRequest.Request.RequestOption = "1"; 
    requestModal.TrackRequest.Request.TransactionReference.CustomerContext = trackingNumber + " tracking context"; 

    string Data = new JavaScriptSerializer().Serialize(requestModal); 
    string response = ""; 
    System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls; 
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(UPSTrackingURL); // https://wwwcie.ups.com/rest/Track 
    request.Method = "POST"; 
    request.KeepAlive = false; 
    request.ProtocolVersion = HttpVersion.Version10; 

    request.ContentType = @"application/json"; 
    request.ContentLength = Data.Length; 

    StreamWriter requestWriter = new StreamWriter(request.GetRequestStream(), System.Text.Encoding.ASCII); 
    requestWriter.Write(Data); 
    requestWriter.Close(); 

    try 
    { 
     WebResponse webResponse = request.GetResponse(); 
     Stream webStream = webResponse.GetResponseStream(); 
     StreamReader responseReader = new StreamReader(webStream); 
     response = responseReader.ReadToEnd(); 
     Console.Out.WriteLine(response); 
     responseReader.Close(); 
     Nop.Core.UPSTrackingResponse.UPSTrackingResponse ResponseModal = 
      new JavaScriptSerializer().Deserialize<Nop.Core.UPSTrackingResponse.UPSTrackingResponse>(response); 
     return ResponseModal; 
    } 
    catch (Exception ex) 
    { 
     return null; 
    } 
} 

這是工作正常,當我從我的本地機器調用它。但是當我在AWS服務器上部署我的應用程序時,它會一直給我以下例外。可能是什麼原因,我現在卡住了。

System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a send. ---> System.IO.IOException: Authentication failed because the remote party has closed the transport stream. 
    at System.Net.Security.SslState.StartReadFrame(Byte[] buffer, Int32 readBytes, AsyncProtocolRequest asyncRequest) 
    at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest) 
    at System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest) 
    at System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst, Byte[] buffer, AsyncProtocolRequest asyncRequest) 
    at System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult lazyResult) 
    at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
    at System.Net.TlsStream.ProcessAuthentication(LazyAsyncResult result) 
    at System.Net.TlsStream.Write(Byte[] buffer, Int32 offset, Int32 size) 
    at System.Net.ConnectStream.WriteHeaders(Boolean async) 
    --- End of inner exception stack trace --- 
    at System.Net.HttpWebRequest.GetRequestStream(TransportContext& context) 
    at System.Net.HttpWebRequest.GetRequestStream() 
    at Nop.Services.Shipping.ShippingService.TrackUPS(String trackingNumber) 
    at Nop.Web.Controllers.OrderController.PrepareShipmentDetailsModel(Shipment shipment) 
    at Nop.Web.Controllers.OrderController.ShipmentDetails(Int32 shipmentId) 
    at lambda_method(Closure , ControllerBase , Object[]) 
    at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) 
    at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) 
    at System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState) 
    at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult) 
    at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d() 
    at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() 
    at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() 
    at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() 
    at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() 
    at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() 
    at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() 
    at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() 
    at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() 
    at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass33.<BeginInvokeActionMethodWithFilters>b__32(IAsyncResult asyncResult) 
    at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass21.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() 
    at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) 
    at System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) 
    at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) 
    at System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) 
    at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) 
    at System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) 
    at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) 
    at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() 
    at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) 
+0

安裝什麼的.NET framework版本對AWS服務器和本地機器? – Evk

+0

您可能需要證書認證 – Amorphis

+0

是否有可能您使用的訪問密鑰僅對您的本地開發機器(localhost:***)有效,但將其部署到您的服務器,則域會更改,因此訪問密鑰不可用對該請求客戶有效。 – Gurpreet

回答

3

嘗試在代碼中加入這個地方,使得調用web服務之前:

    System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Ssl3 
             | System.Net.SecurityProtocolType.Tls 
             | System.Net.SecurityProtocolType.Tls11 
             | System.Net.SecurityProtocolType.Tls12; 
+0

這些已經在我的代碼中。 –

+0

這爲我解決了這個問題 – brendonparker

+0

這是在我的本地IIS中工作,當部署在AWS上時,它不起作用, –

相關問題