当 url 正确时,VB.NET webrequest 返回 404

Posted

技术标签:

【中文标题】当 url 正确时,VB.NET webrequest 返回 404【英文标题】:VB.NET webrequest returning 404 when the url is correct 【发布时间】:2021-03-29 18:29:55 【问题描述】:

我已经在这几个小时尝试了一切。我正在将此代码从 Access 2010 项目移植到 vb.net 我不知道这里可能出了什么问题。

Visual Studio 2019,框架 4.7.2

    Dim data As Byte() = Encoding.UTF8.GetBytes(strBody)
    url = "https://rest.avatax.com/api/v2/companies/" & OE2019HomeForm.strAvalaraCompany & "/certificates/"

    Dim uri As Uri = New Uri(url)

    Dim request As System.Net.WebRequest = System.Net.WebRequest.Create(uri)
    request.Method = "Post"
    request.Headers.Add("40", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)")
    request.Headers.Add("Authorization", "Basic " & OE2019HomeForm.strAuth)
    request.Headers.Add("Cache-Control", "no-cache")
    request.Headers.Add("20", "application/json") 'this is the accept header
    request.Headers.Add("12", "text/plain") ' content type
    request.ContentLength = data.Length
    



    Dim stream As Stream = request.GetRequestStream()
    stream.Write(data, 0, data.Length)
    stream.Close()

    Using response As WebResponse = request.GetResponse()
        Using responsestream As Stream = response.GetResponseStream()
            Using reader As StreamReader = New StreamReader(responsestream)
                Dim webresponse As String
                webresponse = reader.ReadToEnd()
                MsgBox(webresponse)


            End Using
        End Using
    End Using

' 如果我在使请求变暗之前在下面添加行,我会得到“'底层连接已关闭:接收时发生意外错误。'内部异常:Win32Exception:客户端和服务器无法通信,因为它们没有共同的算法”

ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3

任何帮助都会很棒。

【问题讨论】:

request.ContentType = "application/json" request.UserAgent = "[A User Agent]" request.Accept = "application/json,*;q=0.8" request.Headers.Add(HttpRequestHeader.Authorization, "Basic " & [Your Token] request.Headers.Add(HttpRequestHeader.CacheControl, "no-cache") 等。不要尝试将字符串作为 Header 枚举传递,它不是字符串。设置Option Strict On -- 没有人使用 SSL3,它已被弃用。 -- 你在什么系统中测试过这段代码?如果是 Windows 7 / Windows Server 2008 R2,您可能需要先指定ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 无论如何,404 是 not found 错误,因此您可能需要检查 API 是否同时更改了某些内容。在代码中设置好之后,您应该发布完整的url 字符串(并在调试时验证其内容)。 【参考方案1】:

这对你有用吗? 把它放在顶部附近。

    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls Or SecurityProtocolType.Tls11 Or
        SecurityProtocolType.Tls12 Or SecurityProtocolType.Ssl3 Or SecurityProtocolType.Ssl3



    ServicePointManager.ServerCertificateValidationCallback = Function(obj As [Object],
                                                       certificate As X509Certificate,
                                                       chain As X509Chain,
                                                       errors As SslPolicyErrors) (True)

【讨论】:

以上是关于当 url 正确时,VB.NET webrequest 返回 404的主要内容,如果未能解决你的问题,请参考以下文章

使用 vb.net 获取和忘记 URL

VB.NET 中的线程安全变量

如何在.NET 中进行基本身份验证后重定向到 Pentaho 的主页?

如何在 VB.Net 中正确地从内存中删除控件? [复制]

C# Dll 注入器、VB.Net Dll 注入器

C#/VB.NET 给PDF文档添加文本/图像水印