【问题标题】:Trying to send CURL Command using VB.Net and HttpWebRequest - Error 403 Forbidden尝试使用 VB.Net 和 HttpWebRequest 发送 CURL 命令 - 错误 403 Forbidden
【发布时间】:2017-01-23 21:32:49
【问题描述】:

我一直在尝试在下面发送此 CURL 命令,并尝试了一些示例,但它们都不起作用,因为我需要发送 username:password 并且不确定这是否是 HEADER 以及如何发送GET 请求的正文。只是在寻找一个发送 user:password 的示例,因为在任何地方都找不到?

curl --data {"version":"1.1","method":"room_verify","params":{"account":{"type":"room","value":"0"} }} --user test:test http://ressrv.worldweb.com:8001/json_pos_generic/22

这是我的代码:

        Dim url As String = "http://ressrv.worldweb.com:8001/json_pos_generic/22"
    Dim wrq = CType(Net.WebRequest.Create(url), HttpWebRequest)
        Dim postString As String = "{""version"":""1.1"",""method"":""room_verify"",""params"":{""account"":{""type"":""room"",""value"":""0""}}}"

        wrq.Method = "POST"
        wrq.ContentType = "application/json"
        wrq.ContentLength = postString.Length
        wrq.Credentials = New NetworkCredential("test", "test")


    Dim wrqWriter As New StreamWriter(wrq.GetRequestStream())
    wrqWriter.Write(postString)
    wrqWriter.Close()

    Dim responseReader As New StreamReader(wrq.GetResponse().GetResponseStream())
        Dim responseData As String = responseReader.ReadToEnd()

任何帮助或指导将不胜感激......

【问题讨论】:

  • 对不起,我的意思是在上面说 POST 请求

标签: json vb.net curl httpwebrequest


【解决方案1】:

好的,经过 2 天的尝试进行排序后,我找到了答案,虽然我使用了稍微不同的代码,但我认为这完全是因为没有将我的凭据转换为我现在要研究的 Base64了解为什么需要这样做。我从这里的另一篇文章中找到了这个答案Curl request equivalent in VB.NET

            Dim myReq As HttpWebRequest
        Dim myResp As HttpWebResponse

        myReq = HttpWebRequest.Create("http://ressrv.worldweb.com:8001/json_pos_generic/22")

        myReq.Method = "POST"
        myReq.ContentType = "application/json"
        myReq.Headers.Add("Authorization", "Basic " & Convert.ToBase64String(Encoding.UTF8.GetBytes("test:test")))
        Dim myData As String = "{""version"":""1.1"",""method"":""room_verify"",""params"":{""account"":{""type"":""room"",""value"":""0""}}}"
        myReq.GetRequestStream.Write(System.Text.Encoding.UTF8.GetBytes(myData), 0, System.Text.Encoding.UTF8.GetBytes(myData).Count)
        myResp = myReq.GetResponse
        Dim myreader As New System.IO.StreamReader(myResp.GetResponseStream)
        Dim myText As String
        myText = myreader.ReadToEnd

        MsgBox(myText)

还是谢谢!!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-15
    • 1970-01-01
    • 2019-11-18
    • 2022-11-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-25
    相关资源
    最近更新 更多