HttpWebResponse在线程中导致内部服务器错误

本文关键字:内部 服务器 错误 线程 HttpWebResponse | 更新日期: 2023-09-27 18:18:06

我构建了一个与远程服务器通信的程序。当我处理所有消息(在JSON对象)顺序它的行为正确。然而,当我在线程中构建它时,它会返回远程服务器返回一个错误:(500)内部服务器错误。

远程服务器的配置会有问题吗?从日志中我可以看到请求将在0.5s内到达远程服务器。

下面是我与远程服务器通信的代码。

void Execute(){
    var httpWebRequest = (HttpWebRequest)WebRequest.Create(REMOTE_URL);
    httpWebRequest.ContentType = "application/json";
    httpWebRequest.Method = "POST";
    using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
    {                             
        streamWriter.Write(json);
        streamWriter.Close();
    }                        
    HttpWebResponse httpResponse = null;
    try
    {
        httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();       
    }
    catch (Exception exc)
    {
        // Internal Server Error exception caught here.     
    }
    if (httpResponse != null)
    {
        using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
        {
            string result = streamReader.ReadToEnd();
            // deserialize the result                                      
            streamReader.Close();
        }                
    }
    httpResponse.Close();
}

我是这样开始这个线程的

ObjectManager o = new ObjectManager()
Thread thread = new Thread(() => o.Execute(parameter));
thread.Start();

我认为这是一个问题在线程或服务器配置。任何想法吗?

HttpWebResponse在线程中导致内部服务器错误

在我看来,线程并没有什么问题。

Error 500,表示服务器有问题。可能有很多可能性,其中之一是服务器(应用程序)无法处理并发请求。同样,您需要查看服务器上的错误,以便更好地了解在这一端发生了什么。