Visual Basic中HTTPS Web请求403错误 [英] Visual basic https web request 403 error

查看:322
本文介绍了Visual Basic中HTTPS Web请求403错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好我是新来的。我试图连接到一个网站,但它给了我一个403错误。

我希望有人能帮助我,这里的网站上说的:

留言是通过连接这四个串形成的:


  • 随机数。 A 63位正整数。


  • HMAC认证密钥。这是一个关键的/秘密对的第一个。


  • 相对路径,例如/ API /钱包/


  • GET或POST参数的URL连接codeD格式。


散列算法是SHA256。

发送签名

签名是通过HTTP头发送。一共有三个字段的需要:


  • Apiauth键:HMAC认证密钥


  • Apiauth-随机数。在这个特殊的请求现时


  • Apiauth签名:HMAC签名


下面是我的code,我会永远感激,如果有人可以帮助我打开连接到该网站,谢谢

 进口系统
进口System.Web.Services
进口System.Net
进口System.IO
进口System.Security.Cryptography
进口Newtonsoft.Json
进口Newtonsoft.Json.Linq
进口System.Text
进口的System.Web公共类Form1的
    私人小组Form1_Load的(发件人为System.Object的,电子作为System.EventArgs)把手MyBase.Load
        呼叫authtest()
    结束小组    子authtest()
        昏暗webStream作为流
        暗淡WebResponse类=
        昏暗的REQ作为的WebRequest
        昏暗的资源作为HttpWebResponse        昏暗hmacauthkey作为字符串=< myapikey>中
        昏暗hmacsecret作为字符串=< myapisecret>中
        昏暗 - [R作为新的随机
        昏暗nonce1只要= Math.Round(r.NextDouble * Long.MaxValue)
        昏暗relativepath作为字符串=/ API /我/
        昏暗的PARAMS的String =
        昏暗getparamsurlen codeD = WebUtility.HtmlEn code(PARAMS)
        昏暗的信息作为字符串= Convert.ToString(nonce1)及hmacauthkey&安培; relativepath&安培; getparamsurlen codeD
        昏暗的签名作为字符串= HashString(消息)        REQ = CTYPE(WebRequest.Create(https://website.com/api/myself/),HttpWebRequest的)
        req.Credentials =新的NetworkCredential(<用户名>中,<密码和GT;)
        req.Co​​ntentType =应用/的X WWW的形式urlen codeD        req.Headers.Add(Apiauth-键,&所述; myapikey>中)
        req.Headers.Add(Apiauth-随机数,Convert.ToString(nonce1))
        req.Headers.Add(Apiauth签名,签名)
        req.Method =GET
        RES = CTYPE(req.GetResponse(),HttpWebResponse)发送请求
        webStream = res.GetResponseStream()获得响应
        昏暗webStreamReader作为新的StreamReader(webStream)
        虽然webStreamReader.Peek> = 0
            WebResponse类= webStreamReader.ReadToEnd()
        虽然结束
        MSGBOX(WebResponse类)
    结束小组    公共共享功能HashString(BYVAL StringToHash作为字符串)作为字符串
        昏暗的myEn codeR作为新System.Text.UTF8Encoding
        昏暗的密钥()以字节= myEn coder.GetBytes(< mysecretkey>中)
        暗淡文本()作为字节= myEn coder.GetBytes(StringToHash)
        昏暗myHMACSHA256作为新System.Security.Cryptography.HMACSHA256(关键)
        昏暗的哈希code以字节()= myHMACSHA256.ComputeHash(文本)
        返回中StrConv(Convert.ToBase64String(哈希code),vbUpperCase)
    结束功能    功能HMACSHA256_Encrypt(TXT BYVAL作为字符串)作为字符串
        尝试
            昏暗SecretKey的作为字符串=< mysecretkey>中
            昏暗的沙作为新System.Security.Cryptography.HMACSHA256(System.Text.UTF8Encoding.UTF8.GetBytes(SecretKey的))
            昏暗的哈希()作为字节= sha.ComputeHash(System.Text.UTF8Encoding.UTF8.GetBytes(TXT))
            昏暗某人作为新System.Text.StringBuilder(Hash.Length * 2)
            对于每个B为字节哈希
                sb.Append(十六进制(B)的.PadLeft(2,0))
            下一个
            返回sb.ToString.ToLower
        抓住EX为例外
            Debug.Print(Date.Now&安培;SHA256_Encrypt错误&放大器; ex.Message)
            什么都不返回
        结束Try
    结束功能
末级


解决方案

有你传递网络凭据在Web请求的原因?

我建议你运行提琴手(或类似的东西),同时使您的网络请求,并检查看看你是否可以查看周围的反应更多的细节。

Hi everyone i'm new here. I'm trying to connect to a website but it's giving me a 403 error.

I'm hoping somebody could help me, here's what the website says:

Message is formed by concatenating these four strings:

  • Nonce. A 63 bit positive integer.

  • HMAC authentication key. This is the first one of a key/secret pair.

  • Relative path, for example /api/wallet/

  • GET or POST parameters in their URL encoded format.

Hashing algorithm is SHA256.

Sending signature

Signature is sent via HTTP headers. A total of three fields are needed:

  • Apiauth-Key: HMAC authentication key.

  • Apiauth-Nonce: The nonce in this particular request.

  • Apiauth-Signature: HMAC signature.

Here's my code, i'd be ever so grateful if anybody could help me open connection to the site, thanks

Imports System
Imports System.Web.Services
Imports System.Net
Imports System.IO
Imports System.Security.Cryptography
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq
Imports System.Text
Imports System.Web

Public Class Form1
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Call authtest()
    End Sub

    Sub authtest()
        Dim webStream As Stream
        Dim webResponse = ""
        Dim req As WebRequest
        Dim res As HttpWebResponse

        Dim hmacauthkey As String = "<myapikey>"
        Dim hmacsecret As String = "<myapisecret>"
        Dim r As New Random
        Dim nonce1 As Long = Math.Round(r.NextDouble * Long.MaxValue)
        Dim relativepath As String = "/api/myself/"
        Dim params As String = ""
        Dim getparamsurlencoded = WebUtility.HtmlEncode(params)
        Dim message As String = Convert.ToString(nonce1) & hmacauthkey & relativepath & getparamsurlencoded
        Dim signature As String = HashString(message)

        req = CType(WebRequest.Create("https://website.com/api/myself/"), HttpWebRequest)
        req.Credentials = New NetworkCredential("<username>", "<password>")
        req.ContentType = "application/x-www-form-urlencoded"

        req.Headers.Add("Apiauth-Key", "<myapikey>")
        req.Headers.Add("Apiauth-Nonce", Convert.ToString(nonce1))
        req.Headers.Add("Apiauth-Signature", signature)
        req.Method = "GET"
        res = CType(req.GetResponse(), HttpWebResponse) ' Send Request
        webStream = res.GetResponseStream() ' Get Response
        Dim webStreamReader As New StreamReader(webStream)
        While webStreamReader.Peek >= 0
            webResponse = webStreamReader.ReadToEnd()
        End While
        MsgBox(webResponse)
    End Sub

    Public Shared Function HashString(ByVal StringToHash As String) As String
        Dim myEncoder As New System.Text.UTF8Encoding
        Dim Key() As Byte = myEncoder.GetBytes("<mysecretkey>")
        Dim Text() As Byte = myEncoder.GetBytes(StringToHash)
        Dim myHMACSHA256 As New System.Security.Cryptography.HMACSHA256(Key)
        Dim HashCode As Byte() = myHMACSHA256.ComputeHash(Text)
        Return StrConv(Convert.ToBase64String(HashCode), vbUpperCase)
    End Function

    Function HMACSHA256_Encrypt(ByVal Txt As String) As String
        Try
            Dim secretkey As String = "<mysecretkey>"
            Dim sha As New System.Security.Cryptography.HMACSHA256(System.Text.UTF8Encoding.UTF8.GetBytes(secretkey))
            Dim Hash() As Byte = sha.ComputeHash(System.Text.UTF8Encoding.UTF8.GetBytes(Txt))
            Dim sb As New System.Text.StringBuilder(Hash.Length * 2)
            For Each B As Byte In Hash
                sb.Append(Hex(B).PadLeft(2, "0"))
            Next
            Return sb.ToString.ToLower
        Catch ex As Exception
            Debug.Print(Date.Now & " SHA256_Encrypt error " & ex.Message)
            Return Nothing
        End Try
    End Function
End Class

解决方案

Is there a reason you're passing network credentials in your web request?

I'd recommend running Fiddler (or something similar) while making your web request and check to see if you can view more details surrounding the response.

这篇关于Visual Basic中HTTPS Web请求403错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆