You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用HttpClient时遇超时及程序冻结问题求助(附VB.NET代码)

问题排查与修复方案

核心问题分析

  • 超时时间设置过短:代码中hwrequest.Timeout = 600仅设置了600毫秒,对于YouTube这类加载资源较多的页面来说完全不足,直接触发超时错误。
  • UserAgent不规范:自定义的http_requester/0.1用户代理容易被网站反爬机制识别拦截,导致请求无法正常响应。
  • UI线程阻塞:在Button点击事件中同步执行网络请求,会直接卡住UI线程,这是程序冻结的直接原因。

修复后的代码

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        ' 用后台线程执行请求避免UI冻结,若坚持同步调用可直接执行WRequest,但冻结问题会保留
        Task.Run(Sub()
                     Dim result = WRequest("https://www.youtube.com/", "GET", "")
                     ' 跨线程更新UI
                     Me.Invoke(Sub() MsgBox(result))
                 End Sub)
        ' WRequest("http://", "POST", "name=jane&stat=active")
    End Sub

    Function WRequest(URL As String, method As String, POSTdata As String) As String
        Dim responseData As String = ""
        Try
            Dim cookieJar As New Net.CookieContainer()
            Dim hwrequest As Net.HttpWebRequest = DirectCast(Net.WebRequest.Create(URL), Net.HttpWebRequest)
            hwrequest.CookieContainer = cookieJar
            ' 设置标准的Accept头
            hwrequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
            hwrequest.AllowAutoRedirect = True
            ' 使用主流浏览器的UserAgent,降低拦截概率
            hwrequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36"
            ' 延长请求超时到10秒
            hwrequest.Timeout = 10000
            ' 设置读取响应的超时时间
            hwrequest.ReadWriteTimeout = 15000
            hwrequest.Method = method

            If hwrequest.Method = "POST" Then
                hwrequest.ContentType = "application/x-www-form-urlencoded"
                ' 改用UTF8编码避免中文乱码
                Dim encoding As New Text.UTF8Encoding()
                Dim postByteArray() As Byte = encoding.GetBytes(POSTdata)
                hwrequest.ContentLength = postByteArray.Length
                ' 使用Using自动释放流资源
                Using postStream As IO.Stream = hwrequest.GetRequestStream()
                    postStream.Write(postByteArray, 0, postByteArray.Length)
                End Using
            End If

            ' 使用Using自动释放响应资源
            Using hwresponse As Net.HttpWebResponse = DirectCast(hwrequest.GetResponse(), Net.HttpWebResponse)
                If hwresponse.StatusCode = Net.HttpStatusCode.OK Then
                    ' 指定UTF8编码读取响应,避免页面乱码
                    Using responseStream As New IO.StreamReader(hwresponse.GetResponseStream(), Text.Encoding.UTF8)
                        responseData = responseStream.ReadToEnd()
                    End Using
                Else
                    responseData = $"请求失败,状态码:{hwresponse.StatusCode}"
                End If
            End Using
        Catch e As Exception
            responseData = $"发生错误:{e.Message}"
        End Try
        Return responseData
    End Function
End Class

关键修改说明

  • 调整超时参数:将请求超时设为10秒,新增读取超时参数,确保复杂页面有足够加载时间。
  • 替换UserAgent:使用Chrome浏览器的标准用户代理,减少被反爬拦截的可能性。
  • 优化资源管理:用Using语句自动释放流和响应对象,避免资源泄漏。
  • 编码优化:POST请求和响应读取均改用UTF8编码,解决潜在的乱码问题。
  • UI线程优化:通过Task.Run在后台线程执行网络请求,彻底解决界面冻结问题。

内容的提问来源于stack exchange,提问作者adev

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 02:19:52