vb.net 在 WebClient 上更改超时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23414876/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 17:29:48  来源:igfitidea点击:

Change timeout on WebClient

vb.nettimeoutwebclient

提问by MattB

I need to change the timeout for a WebClient object. I have a query I'm passing to retrieve a CSV that works through a browser URL but does not work in my .Net project. It appears the client times out. Here is the line...

我需要更改 WebClient 对象的超时时间。我有一个查询要通过浏览器 URL 检索 CSV,但在我的 .Net 项目中不起作用。看起来客户端超时了。这是线路...

ResponseText = Client.DownloadString("http://someBIwebquery.com")

The line executes, but ResponseText is nothing after. I've passed other queries to the same server using the same method in the past, and again this does work when passed as a browser URL, so the issue is simply WebClient timing out.

该行执行,但 ResponseText 之后什么都没有。我过去使用相同的方法将其他查询传递到同一台服务器,并且在作为浏览器 URL 传递时同样有效,因此问题只是 WebClient 超时。

Further complicating things, this object is already modified to accept cookies, so I already have an altered WebClient class I'm using:

更复杂的是,这个对象已经被修改为接受 cookie,所以我已经有一个改变的 WebClient 类我正在使用:

Imports System.Net

Public Class CookieAwareWebClient
    Inherits WebClient

    Private cc As New CookieContainer()
    Private lastPage As String

    Protected Overrides Function GetWebRequest(ByVal address As System.Uri) As System.Net.WebRequest
        Dim R = MyBase.GetWebRequest(address)
        If TypeOf R Is HttpWebRequest Then
            With DirectCast(R, HttpWebRequest)
                .CookieContainer = cc
                If Not lastPage Is Nothing Then
                    .Referer = lastPage
                End If
            End With
        End If
        lastPage = address.ToString()
        Return R
    End Function

    Protected Overrides WebRequest GetWebRequest(Uri address)

End Class

And I need to figure out a way to override the timeout request. I've already found this C# code, but I'm having trouble translating it. I'm not completely clueless when it comes to C#, but I'm missing something:

我需要想办法覆盖超时请求。我已经找到了这个 C# 代码,但是我在翻译它时遇到了麻烦。当谈到 C# 时,我并不是完全无能为力,但我遗漏了一些东西:

private class MyWebClient : WebClient
    {
        protected override WebRequest GetWebRequest(Uri uri)
        {
            WebRequest w = base.GetWebRequest(uri);
            w.Timeout = 20 * 60 * 1000;
            return w;
        }
    }

That's what I've got for now, if anyone can give me a clue, I'd appreciate it!

这就是我现在所拥有的,如果有人能给我一个线索,我将不胜感激!

回答by MattB

Man I feel dumb....

老子觉得自己很蠢....

Anyway, after looking at this more closely, I figured out what I needed to do. I'd normally delete a question like this, but since I just figured out what I was missing and it was hard to find, I'm going to go ahead and post the answer.

无论如何,在更仔细地查看之后,我想出了我需要做的事情。我通常会删除这样的问题,但由于我刚刚弄清楚我遗漏了什么并且很难找到,我将继续发布答案。

I just needed to add these lines to my class:

我只需要将这些行添加到我的班级中:

Property Timeout As Integer  '(I actually did this the "proper" way, but this would be sufficient.)

and in the Override for GetWebRequest:

并在 GetWebRequest 的覆盖中:

R.Timeout = Me.Timeout

That's pretty much it. Here's the class in its entierty:

差不多就是这样。这是它的实体类:

Imports System.Net

Public Class CookieAwareWebClient
    Inherits WebClient

    Private cc As New CookieContainer()
    Private lastPage As String

    Private _Timeout As Integer
    Public Property Timeout() As Integer
        Get
            Return _Timeout
        End Get
        Set(ByVal value As Integer)
            _Timeout = value
        End Set
    End Property

    Protected Overrides Function GetWebRequest(ByVal address As System.Uri) As System.Net.WebRequest
        Dim R = MyBase.GetWebRequest(address)
        R.Timeout = Me.Timeout
        If TypeOf R Is HttpWebRequest Then
            With DirectCast(R, HttpWebRequest)
                .CookieContainer = cc
                If Not lastPage Is Nothing Then
                    .Referer = lastPage
                End If
            End With
        End If
        lastPage = address.ToString()
        Return R
    End Function

End Class

回答by QuickDanger

For other users stumbling upon this answer, who may not need to add custom cookie handling, there is an easier way to change the timeout.

对于偶然发现这个答案的其他用户,他们可能不需要添加自定义 cookie 处理,有一种更简单的方法来更改超时。

In my projects, I've used this snippet, which is much simpler:

在我的项目中,我使用了这个片段,它更简单:

Dim request As HttpWebRequest = CType(WebRequest.Create(url), HttpWebRequest)
request.Timeout = 10 * 60 * 1000 ' 10 minutes

No custom classes needed (they already exist).

不需要自定义类(它们已经存在)。