网络请求错误?

发布于 2024-09-14 21:31:13 字数 734 浏览 7 评论 0原文

编辑:已解决,问题出在服务器端。

我正在使用 C# 和 .NET2,我想知道这是一个 WebRequest bug。我用这个方法做了几个很好的请求,一切都很好,但之后每次我都会收到“操作已超时”。我实在不明白这是为什么。

public string RequestPage(string url) {
        HttpWebRequest req = null;
        string line = "";
        string site = "";

        try {
            req = (HttpWebRequest) WebRequest.Create(url.Trim());
            req.Timeout = 10000;

            StreamReader reader = new StreamReader(req.GetResponse().GetResponseStream());
            while ((line = reader.ReadLine()) != null) {
                site += line;
            }

            return site;
        } catch (Exception ex) {
            MessageBox.Show("ERROR " + ex.Message);
        }

        return null;
    }

EDIT: Solved, the problem was server-side.

I'm using C# and .NET2 and I wonder is that a WebRequest bug.. I do several good requests with this method and all is fine, but after that every time I get "The operation has timed out.". I really don't understand why is that.

public string RequestPage(string url) {
        HttpWebRequest req = null;
        string line = "";
        string site = "";

        try {
            req = (HttpWebRequest) WebRequest.Create(url.Trim());
            req.Timeout = 10000;

            StreamReader reader = new StreamReader(req.GetResponse().GetResponseStream());
            while ((line = reader.ReadLine()) != null) {
                site += line;
            }

            return site;
        } catch (Exception ex) {
            MessageBox.Show("ERROR " + ex.Message);
        }

        return null;
    }

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

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

发布评论

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

评论(2

咽泪装欢 2024-09-21 21:31:13

我不知道这是否解决了您的问题,但是完成后您应该始终处置 HttpWebResponse (以及实现 IDisposable 的其他对象):

public string RequestPage(string url)
{
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
    req.Timeout = 10000;

    using (WebResponse resp = req.GetResponse())
    using (StreamReader reader = new StreamReader(resp.GetResponseStream()))
    {
        return reader.ReadToEnd();
    }
}

如果您实际上不需要 HttpWebRequest 的所有功能,则可以使用 WebClient 改为:

public string RequestPage(string url)
{
    using (WebClient client = new WebClient())
    {
        return client.DownloadString(url);
    }
}

I don't know if this solves your problem, but you should always dispose a HttpWebResponse (and other objects that implement IDisposable) when you're done:

public string RequestPage(string url)
{
    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
    req.Timeout = 10000;

    using (WebResponse resp = req.GetResponse())
    using (StreamReader reader = new StreamReader(resp.GetResponseStream()))
    {
        return reader.ReadToEnd();
    }
}

If you don't actually require all the features of HttpWebRequest, you can use WebClient instead:

public string RequestPage(string url)
{
    using (WebClient client = new WebClient())
    {
        return client.DownloadString(url);
    }
}
如歌彻婉言 2024-09-21 21:31:13

您不会处理响应:

using (WebResponse response = req.GetResponse())
using (StreamReader reader = new StreamReader(response.GetResponseStream())
{
    while ((line = reader.ReadLine()) != null) {
        site += line;
    }
}

基本上,您与之交谈的每个服务器都有池连接。因为您没有关闭响应,所以它们已经用完了。以上应该可以解决。

另外:

  • 这可能是一种非常慢的构建字符串的方法。使用 StringBuilder 在循环中连接文本内容。
  • 您真的要删除所有换行符吗?如果没有,只需使用 reader.ReadToEnd() 代替。

You're not disposing of the response:

using (WebResponse response = req.GetResponse())
using (StreamReader reader = new StreamReader(response.GetResponseStream())
{
    while ((line = reader.ReadLine()) != null) {
        site += line;
    }
}

Basically there are pooled connections per server that you talk to. You're running out of them because you're not closing the response. The above should sort it out.

Additionally:

  • That's a potentially very slow way of building up a string. Use a StringBuilder to concatenate text content in a loop.
  • Do you really want to remove all the line breaks? If not, just use reader.ReadToEnd() instead.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文