首页 > 解决方案 > 线程化的 HttpWebResponse 并不总是触发

问题描述

此代码正在 Web 服务中运行。有时它执行得很好,有时似乎线程没有调用 url。我也从来没有看到写在 HttpWebResponse using 语句中的日志事件。我不确定发生了什么,请帮助?

HttpWebRequest urlRequest = (HttpWebRequest)WebRequest.Create(url);
urlRequest.Method = "POST";
byte[] data = Encoding.ASCII.GetBytes(JSON);
urlRequest.ContentLength = data.Length;
urlRequest.ContentType = "application/json";

ThreadPool.QueueUserWorkItem(o =>
                    {
                        try
                        {
                            new Logger().Info("data length = " + data.Length.ToString());
                            new Logger().Info(JSON);
                            new Logger().Info("Before posting to ERPFullAppointmentCheckout controller");
                            using (Stream stream = urlRequest.GetRequestStream())
                            {
                                stream.Write(data, 0, data.Length);

                                new Logger().Info("Before using HttpWebResponse");
                                using (HttpWebResponse response = (HttpWebResponse)urlRequest.GetResponse())
                                {
                                    new Logger().Info("Inside using HttpWebResponse");
                                    string receivedresponse = new StreamReader(response.GetResponseStream()).ReadToEnd();
                                    string statuscode = response.StatusCode.ToString();
                                    new Logger().Info("Response from POST: " + (String.IsNullOrEmpty(receivedresponse) ? "" : receivedresponse));
                                    new Logger().Info("Status Code: " + statuscode);
                                }
                            }


                            new Logger().Info("After posting to ERPFullAppointmentCheckout controller");
                        }
                        catch (Exception ex)
                        {
                            new Logger().Error("Error posting to ERPFullAppointmentCheckout controller", ex);
                        }
                    });

标签: c#multithreadingweb-services

解决方案



推荐阅读