发布

时间:2016-06-17 17:14:29

标签: asp.net-mvc-4 httpclient

我正在尝试做一个httpclient postasync,当我在本地运行它时它会起作用,但在我发布之后却没有。

public async Task<OrderRecord> GetOrderRecord(CheckOutRecord checkOutRecord)
        {          

            OrderRecord orderRecord = new OrderRecord();

            using (var client = new HttpClient())
            {
                try
                {

                    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                    client.Timeout = TimeSpan.FromMilliseconds(100000);

                    HttpResponseMessage response = await client.PostAsJsonAsync("http://somewhere.com/Test/TestCheckout/", checkOutRecord);

                    if (response.IsSuccessStatusCode)
                    {                        
                        string result = await response.Content.ReadAsStringAsync();
                        orderRecord = JsonConvert.DeserializeObject<OrderRecord>(result);
                    }
                    else
                    {
                        logger.Info(response.RequestMessage);
                    }   
                }
                catch(HttpRequestException ex)
                {
                    logger.Error(ex);
                    logger.Error("GetORderRecord failed.  CheckOutRecord: " + checkOutRecord);

                    Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
                }

            }

            return orderRecord;
        }

以下是错误消息: System.Net.Http.HttpRequestException:发送请求时发生错误。 ---&GT; System.Net.WebException:无法连接到远程服务器---&gt; System.Net.Sockets.SocketException:连接尝试失败,因为连接方在一段时间后没有正确响应,或者建立的连接失败,因为连接的主机未能响应“服务器IP地址”    在System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult)    在System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure,Socket s4,Socket s6,Socket&amp; socket,IPAddress&amp; address,ConnectSocketState state,IAsyncResult asyncResult,Exception&amp; exception)    ---内部异常堆栈跟踪结束---    在System.Net.HttpWebRequest.EndGetRequestStream(IAsyncResult asyncResult,TransportContext&amp; context)    在System.Net.Http.HttpClientHandler.GetRequestStreamCallback(IAsyncResult ar)    ---内部异常堆栈跟踪结束---    在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)    在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)    在System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()    在MotorcraftCheckoutService.WebApplication.Controllers.OrderController.d__8.MoveNext()

有什么想法吗?如果我在本地运行它并将其指向远程位置它可以工作。如果我运行它并将其指向我的机器就可以了。发布到网络服务器后不起作用。

0 个答案:

没有答案