启用多个网络适配器时 HttpWebRequest 超时

发布于 2024-08-25 19:29:35 字数 678 浏览 5 评论 0原文

在我的 Win7 PC 上,我有几个用于 VMWare 服务器的虚拟网络适配器。 当我启用这些适配器时,我的 HttpWebRequest 超时。我真的应该告诉它要绑定到哪个适配器吗?

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url.AbsoluteUri + "etc.txt");
            request.Timeout = 2000;

            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                {
                    return reader.ReadToEnd();
                }
            }

更新

我猜这是一个常见问题。有人有处理这个问题的标准方法吗?我无法真正提示用户界面,因为他们不是技术人员。 Rohit 的回答是展示如何设置 ServicePoint 的良好开端。

On my Win7 PC I have a couple of virtual network adaptors that are used for VMWare server.
My HttpWebRequest times out when I have these adaptors enabled. Should I really have to tell it which adaptor to bind to?

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url.AbsoluteUri + "etc.txt");
            request.Timeout = 2000;

            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                {
                    return reader.ReadToEnd();
                }
            }

UPDATE

I'm guessing this is a common issue. Does anybody have a standard way to handle this? I cant really prompt the user for the interface as they are non tech. Rohit's answer is good start at showing how to set the ServicePoint.

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

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

发布评论

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

评论(2

美人迟暮 2024-09-01 19:29:35

Tim,如果您看到超时,那是因为您的新适配器具有 URL 的路由,但它们未到达目的地。

public delegate IPEndPoint BindIPEndPoint(ServicePoint servicePoint, IPEndPoint remoteEndPoint, int retryCount);

您可以将其用作

private IPEndPoint BindIPEndPointCallback(ServicePoint servicePoint,IPEndPoint remoteEndPoint, int retryCount)
{
    if(retryCount < 3)
        return new IPEndPoint(IPAddress.Parse("192.168.10.60"), 0); 
    else
        return new IPEndPoint(IPAddress.Any, 0);
}

...

 HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
 req.ServicePoint.BindIPEndPointDelegate =  new BindIPEndPoint(BindIPEndPointCallback);

请参阅 http://www.netbrick.net/blog/PermaLink,guid,b9c255d9-74b4-45ab-8fd0-c9a04784655a.aspx 了解更多详细信息。

Tim, If you are seeing timeout it is because your new adapters have route for the URL and they are not reaching the destination.

public delegate IPEndPoint BindIPEndPoint(ServicePoint servicePoint, IPEndPoint remoteEndPoint, int retryCount);

You can use it as

private IPEndPoint BindIPEndPointCallback(ServicePoint servicePoint,IPEndPoint remoteEndPoint, int retryCount)
{
    if(retryCount < 3)
        return new IPEndPoint(IPAddress.Parse("192.168.10.60"), 0); 
    else
        return new IPEndPoint(IPAddress.Any, 0);
}

and...

 HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
 req.ServicePoint.BindIPEndPointDelegate =  new BindIPEndPoint(BindIPEndPointCallback);

See http://www.netbrick.net/blog/PermaLink,guid,b9c255d9-74b4-45ab-8fd0-c9a04784655a.aspx for more details.

夜巴黎 2024-09-01 19:29:35

以下是罗希茨的回答。这在尝试所有适配器时效果好吗?

    private IPEndPoint BindIPEndPointCallback(ServicePoint servicePoint, IPEndPoint remoteEndPoint, int retryCount)
    {
        List<IPEndPoint> endPoints = new List<IPEndPoint>();

        foreach (NetworkInterface netinface in NetworkInterface.GetAllNetworkInterfaces())
        {
            foreach (IPAddressInformation unicast in netinface.GetIPProperties().UnicastAddresses)
            {
                if(unicast.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                    endPoints.Add(new IPEndPoint(unicast.Address, 80));
            }
        }

        if (retryCount > endPoints.Count - 1)
            return new IPEndPoint(IPAddress.Any, 80);
        else
            return endPoints[retryCount];
    }

Following on from Rohits answer. Would this work well at trying all the adaptors?

    private IPEndPoint BindIPEndPointCallback(ServicePoint servicePoint, IPEndPoint remoteEndPoint, int retryCount)
    {
        List<IPEndPoint> endPoints = new List<IPEndPoint>();

        foreach (NetworkInterface netinface in NetworkInterface.GetAllNetworkInterfaces())
        {
            foreach (IPAddressInformation unicast in netinface.GetIPProperties().UnicastAddresses)
            {
                if(unicast.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
                    endPoints.Add(new IPEndPoint(unicast.Address, 80));
            }
        }

        if (retryCount > endPoints.Count - 1)
            return new IPEndPoint(IPAddress.Any, 80);
        else
            return endPoints[retryCount];
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文