运行线程应用程序后无法在所有浏览器中浏览所有站点

发布于 2024-10-30 15:55:22 字数 1512 浏览 1 评论 0原文

我有一个奇怪的问题,我不确定它来自哪里。 昨晚我开始申请时就开始了。

我有以下代码:

DateTime startTime = DateTime.Now;
WebRequest req = (WebRequest)HttpWebRequest.Create("http://" + server + url + action);
WebResponse res = req.GetResponse();
try
{
    using (StreamReader reader = new StreamReader(res.GetResponseStream()))
    {
        string response = reader.ReadToEnd();
        TestInfo.CheckMsg retMsg;

        // individual Test
        retMsg = indFunc(req.RequestUri.ToString(), response);
        printMessage(retMsg, req, res, startTime);

        if (retMsg.SeverityLevel > TestInfo.CheckMsg.Severity.Warning)
            return;

        // group Test
        retMsg = groupFunc(relatedGroup, req.RequestUri.ToString(), response);
        printMessage(retMsg, req, res, startTime);

        if (retMsg.SeverityLevel > TestInfo.CheckMsg.Severity.Warning)
            return;

        // common Test
        retMsg = commonFunc(req.RequestUri.ToString(), response);
        printMessage(retMsg, req, res, startTime);

        if (retMsg.SeverityLevel > TestInfo.CheckMsg.Severity.Warning)
            return;

        reader.Close();
    };
}
catch (Exception e)
{
    Console.WriteLine("EXCEPTION WAS THROWN!!!!! " + e.Message);
}
finally
{
    res.Close();
}

该函数在多个线程上运行。 经过多次应用程序调试后,此代码无法连接,并声称底层连接已关闭。 当尝试在我的计算机(WIN XP)中的所有浏览器中浏览所有站点时,我要么得到空白页面,要么在 Chrome 中得到服务器的响应为空。

只有 Http 被搞砸了,而其他 TCP 连接却可以工作!

所以我猜问题可能出在我的代码中(也许有些东西没有发布???)

让我知道你对代码的看法。

谢谢

I have a weird problem and I am not sure where is it coming from.
Started last night when I started my application.

I have the following code:

DateTime startTime = DateTime.Now;
WebRequest req = (WebRequest)HttpWebRequest.Create("http://" + server + url + action);
WebResponse res = req.GetResponse();
try
{
    using (StreamReader reader = new StreamReader(res.GetResponseStream()))
    {
        string response = reader.ReadToEnd();
        TestInfo.CheckMsg retMsg;

        // individual Test
        retMsg = indFunc(req.RequestUri.ToString(), response);
        printMessage(retMsg, req, res, startTime);

        if (retMsg.SeverityLevel > TestInfo.CheckMsg.Severity.Warning)
            return;

        // group Test
        retMsg = groupFunc(relatedGroup, req.RequestUri.ToString(), response);
        printMessage(retMsg, req, res, startTime);

        if (retMsg.SeverityLevel > TestInfo.CheckMsg.Severity.Warning)
            return;

        // common Test
        retMsg = commonFunc(req.RequestUri.ToString(), response);
        printMessage(retMsg, req, res, startTime);

        if (retMsg.SeverityLevel > TestInfo.CheckMsg.Severity.Warning)
            return;

        reader.Close();
    };
}
catch (Exception e)
{
    Console.WriteLine("EXCEPTION WAS THROWN!!!!! " + e.Message);
}
finally
{
    res.Close();
}

This function runs on several threads.
After several application debugging this code fails to connect claiming UNDERLYING CONNECTION WAS CLOSED.
when trying to browse in my computer (WIN XP) in all browsers to all sites I get either blank page or in chrome I get that the response from the server was empty.

Http only is screwed while other TCP connections work!

So I guess the problem might be in my code (maybe something is not released???)

Let me know what do you think of the code.

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱你不解释 2024-11-06 15:55:22

我确实认为错误的一件事是您需要移入

 WebResponse res = req.GetResponse();

try 块,因为它可能会引发异常(MSDN)。我还会添加一个空检查:

 if(res != null){
   res.Close()
 }

One thing I do see wrong is that you need to move

 WebResponse res = req.GetResponse();

Into your try block bc it can throw an exception(MSDN). I would also add a null check to:

 if(res != null){
   res.Close()
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文