测试 URL 时 Java 和 C# 代码的性能差异

发布于 2024-07-13 00:10:43 字数 1757 浏览 6 评论 0原文

运行以下 Java 代码时,我在确定我正在测试的网页是否已启动时获得了非常准确且一致的结果。

protected synchronized boolean checkUrl(HttpURLConnection connection){
    boolean error = false;
    //HttpURLConnection connection = null;
    GregorianCalendar calendar = new GregorianCalendar();

    try{
        if(connection != null){
            connection.connect();

            //200 is the expected HTTP_OK response
            error = processResponseCode(connection.getResponseCode());

            connection.disconnect();
        } else{
            error = false;
        }

    }catch(java.net.UnknownHostException uhe){
        ...     } 
    catch(Exception e){
        ...     }

    return error;
}

C# 中与 Java 模式最接近的匹配具有更高的误报结果(主要是由于超时 - 默认周期为 100000 毫秒)。

protected bool connectedToUrl = false;
        response = null;

        HttpWebRequest webreq = (HttpWebRequest)WebRequest.Create(this.getUri());
        webreq.Credentials = CredentialCache.DefaultCredentials;
        WebResponse res = null;// webreq.GetResponse();

        try
        {
            WebRequest request = WebRequest.Create(this.getUri()) as WebRequest;
            request.Credentials = CredentialCache.DefaultCredentials;

            if (request != null)
            {
                // Get response 
                res = webreq.GetResponse();

                connectedToUrl = processResponseCode(res);
            }
            else
            {
                logger.Fatal(getFatalMessage());

                string error = string.Empty;
            }
        }
        catch (Exception e)
        {
            throw e;
        }

        return connectedToUrl;
    }

我尝试了 C# 中的各种模式来匹配引用的 Java 代码的有效性,但没有成功。

有任何想法吗?

When running the following Java code, I get very accurate and consistent results in determining if the web page I'm testing is up.

protected synchronized boolean checkUrl(HttpURLConnection connection){
    boolean error = false;
    //HttpURLConnection connection = null;
    GregorianCalendar calendar = new GregorianCalendar();

    try{
        if(connection != null){
            connection.connect();

            //200 is the expected HTTP_OK response
            error = processResponseCode(connection.getResponseCode());

            connection.disconnect();
        } else{
            error = false;
        }

    }catch(java.net.UnknownHostException uhe){
        ...     } 
    catch(Exception e){
        ...     }

    return error;
}

The closest match to the Java pattern in c# has much higher results of false positives (mostly due to timeouts - which has a default period of 100000ms).

protected bool connectedToUrl = false;
        response = null;

        HttpWebRequest webreq = (HttpWebRequest)WebRequest.Create(this.getUri());
        webreq.Credentials = CredentialCache.DefaultCredentials;
        WebResponse res = null;// webreq.GetResponse();

        try
        {
            WebRequest request = WebRequest.Create(this.getUri()) as WebRequest;
            request.Credentials = CredentialCache.DefaultCredentials;

            if (request != null)
            {
                // Get response 
                res = webreq.GetResponse();

                connectedToUrl = processResponseCode(res);
            }
            else
            {
                logger.Fatal(getFatalMessage());

                string error = string.Empty;
            }
        }
        catch (Exception e)
        {
            throw e;
        }

        return connectedToUrl;
    }

I have tried various patterns in c# to match the effectiveness of the quoted Java code, to no avail.

Any ideas?

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

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

发布评论

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

评论(4

半边脸i 2024-07-20 00:10:43

我相信这是因为您没有关闭任何请求对象。

I believe this is because you're not closing any of the request objects.

白龙吟 2024-07-20 00:10:43

只需将其更改

res = webreq.GetResponse();
connectedToUrl = processResponseCode(res);

using (WebResponse res = webreq.GetResponse()) 
{
    connectedToUrl = processResponseCode(res);
}

:(删除之前的声明。)

在您尚未关闭/处置响应(或已完成)之前,它会保留连接。 您一次只能与任何一台主机建立一定数量的连接(我认为默认为 2 个),因此会出现超时。 当您处理响应时,它允许另一个请求使用同一连接。

Simply change this:

res = webreq.GetResponse();
connectedToUrl = processResponseCode(res);

to

using (WebResponse res = webreq.GetResponse()) 
{
    connectedToUrl = processResponseCode(res);
}

(Remove the declaration from earlier.)

Until you haven't closed/disposed the response (or it's been finalized), it's holding onto the connection. You can only have a certain number (2 by default, I believe) of connections to any one host at a time, hence the timeouts. When you dispose the response, it allows another request to use the same connection.

橘亓 2024-07-20 00:10:43

另外:

   catch (Exception e)
   {
      throw e;
   }

除了销毁向上冒泡的异常的堆栈跟踪之外,什么也不做。 如果代码中的其他地方有错误处理,我建议删除 try catch 块。 否则,您应该记录异常并继续。 不要只是抓住它然后扔掉它。

Also this:

   catch (Exception e)
   {
      throw e;
   }

Does nothing but destroy the stack trace on an exception that's been bubbled upwards. If you have error handling elsewhere in your code I suggest removing the try catch block. Otherwise you should log the exception and move on. Don't just catch it to throw it.

最美的太阳 2024-07-20 00:10:43

我认为您缺少 C# 版本中的 GregorianCalendar :-)

为什么 C# 版本中有两个请求对象?

I think you're missing the GregorianCalendar in the C# version :-)

Why do you have two Request Objects in the C# version?

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