为什么会出现401未经授权的错误VB.NET?

时间:2020-04-15 23:42:26

标签: vb.net authentication http-status-code-401 system.net.webexception getresponse

我一直试图使用vb.net登录到博彩站点-失败。 我可以使用python成功地做到这一点。 我的python代码是...

headers = {"Content-Type": "application/json"} 
data = {"username": "xxx", "password": "xxx"}                                     
response = requests.post("https://api.smarkets.com/v3/sessions/", headers=headers, json=data)

从中我得到一个会话令牌( session_token = response.json()[“ token”]

我从Internet上获取了vb.net的代码,不得不进行了几处更改,但是我总是收到Sytem.Net.WebException:远程服务器返回了错误(401)未经授权。 使用以下代码.....

    Dim user As String
    Dim pass As String
    user = "xxx"
    pass = "xxx"
    Dim request As WebRequest = WebRequest.Create("https://api.smarkets.com/v3/sessions/")
    request.Method = "POST"
    Dim postData As String
    postData = "username=" & user & "&password=" & pass
    Dim quote = Chr(34)
    postData = "{""username"": """ + user + """,""password"": """ + pass + """}"
    Dim byteArray As Byte() = System.Text.Encoding.UTF8.GetBytes(postData)
    request.ContentType = "application/json"
    request.ContentLength = byteArray.Length
    Dim dataStream As System.IO.Stream = request.GetRequestStream()
    dataStream.Write(byteArray, 0, byteArray.Length)
    dataStream.Close()
    Dim response As WebResponse = request.GetResponse()

任何建议/建议将不胜感激。 谢谢

0 个答案:

没有答案