【问题标题】:HttpWebRequest received error 406 and Fiddler seems to have confused thingsHttpWebRequest 收到错误 406 并且 Fiddler 似乎有些困惑
【发布时间】:2023-03-30 21:34:01
【问题描述】:

我正在使用以下代码....

Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create(PostingUrl), HttpWebRequest)
myHttpWebRequest.Method = "POST"
myHttpWebRequest.Headers.Add("Authorization", "Bearer " & RSettings.access_token)
myHttpWebRequest.Headers.Add("Accept-Version", "2")
myHttpWebRequest.ContentType = "application/json; charset=UTF-8"
myHttpWebRequest.Accept = "application/json"
' myHttpWebRequest.Proxy = Nothing ' ** SEE NOTES ON THIS LINE **

Dim Byt As Byte() = Encoding.UTF8.GetBytes(DataString)
Using stream = myHttpWebRequest.GetRequestStream()
    stream.Write(Byt, 0, Byt.Length)
End Using

Using myHttpWebResponse As HttpWebResponse = CType(myHttpWebRequest.GetResponse(), HttpWebResponse)
    Using srRead As New StreamReader(myHttpWebResponse.GetResponseStream())
        ListingResponse = srRead.ReadToEnd()
    End Using
End Using

地点:

  • PostingUrl 是“https://reverb.com/api/listings
  • RSettings.access_token(显然)是我的这个 API 的访问令牌
  • DataString 是将数据发布到 Reverb API 的 JSON 字符串

如果我从 Visual Studio (localhost) 运行我的代码,它会返回

远程服务器返回错误:(406) Not Acceptable。

试图找出原因,我打开 Fiddler 希望我可以检查内容类型并找出问题,但响应错误变为:

基础连接已关闭:无法为 SSL/TLS 安全通道建立信任关系。 ---> System.Security.Authentication.AuthenticationException: 根据验证程序,远程证书无效。

(我从这个错误中猜测我的 Auth 密钥也有问题,但我认为这是一个单独的主题。)

经过一番谷歌搜索和 S/O 页面后,我发现了两个建议:

  1. 工具 > Fiddler 选项 > HTTPS 和取消选中“捕获 HTTPS 连接”
  2. 添加注释掉的行:myHttpWebRequest.Proxy = Nothing

如果我进行任何这些更改,我会返回我在没有运行 Fiddler 的情况下得到的 错误 406

但是,如果我添加了 myHttpWebRequest.Proxy = Nothing 行,我将无法在 Fiddler 中看到 Tunnel to http://reverb.com:443 日志条目,没有对 Reverb.com 的请求记录,因此我无法检查任何内容。

我现在对自己在做什么感到非常困惑,而且我想实际上根本没有任何进展!

  • 我仍然遇到错误 406,并且似乎无法像我希望的那样使用 Fiddler 检查标题/内容类型问题(如果我错了,请解释一下!)
  • 即使我确实纠正了 406,我认为我有一个身份验证错误。

顺便说一句...所有这些都是在此处的 Rever 文档页面上重新创建 cURL 示例的尝试:

https://dev.reverb.com/docs/create-a-listing

我也在这里讨论过这个问题:

https://dev.reverb.com/v1.0/discuss/57bb2ca0aa8f760e004588cf

【问题讨论】:

    标签: asp.net curl httpwebrequest fiddler


    【解决方案1】:

    啊......好吧,我对 Fiddler 的困惑仍然存在,但 406 错误是由以下原因引起的:

    myHttpWebRequest.Headers.Add("Accept-Version", "2")
    

    应该是

    myHttpWebRequest.Headers.Add("Accept-Version", "2.0")
    

    多么简单!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-03
      • 1970-01-01
      相关资源
      最近更新 更多