Visual Basic https web request 403错误

时间:2016-02-22 03:42:07

标签: .net vb.net api httpwebrequest

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

我希望有人可以帮助我,这是网站上的内容:

通过连接这四个字符串形成消息:

  • 现时。一个63位正整数。

  • HMAC身份验证密钥。这是密钥/秘密对中的第一个。

  • 相对路径,例如/ api / wallet /

  • 以URL编码格式获取或POST参数。

哈希算法是SHA256。

发送签名

签名通过HTTP标头发送。总共需要三个字段:

  • Apiauth-Key:HMAC身份验证密钥。

  • Apiauth-Nonce:此特定请求中的nonce。

  • Apiauth-Signature:HMAC签名。

这是我的代码,如果有人能帮我打开网站连接,我会非常感激,谢谢

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

1 个答案:

答案 0 :(得分:0)

您是否有理由在网络请求中传递网络凭据?

我建议您在提出网络请求时运行Fiddler(或类似内容),并检查您是否可以查看有关响应的更多详细信息。

相关问题