.net中的curl命令

时间:2019-03-22 02:05:24

标签: json vb.net curl

我有一个curl命令,试图在.net中进行转换和使用。我尝试了许多不同的方案,但这是我认为最可行的方案。有人知道我在做什么错吗?响应返回si表示无效的API密钥

Curl COMMAND = https://api.com/api/v1/scores --data "token=YOUR_API_TOKEN"

    Dim request As HttpWebRequest = HttpWebRequest.Create("https://api.com/api/v1/groups")
    request.Proxy = Nothing
    request.Method = "GET"
    request.ContentType = "application/json"
    request.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(New ASCIIEncoding().GetBytes(String.Format("{0}={1}", "Token", personalaccesstoken))))

    'create the response and reader
    Dim Response As HttpWebResponse = request.GetResponse
    Dim ResponseStream As System.IO.Stream = Response.GetResponseStream

    'Create Stream reader
    Dim StreamReader As New System.IO.StreamReader(ResponseStream)
    Dim data As String = StreamReader.ReadToEnd
    StreamReader.Close()

    'get the data
    jsonString = data.ToString.Replace("'", "''")

1 个答案:

答案 0 :(得分:0)

我最终使用以下代码解决了这个问题。

    Dim request As HttpWebRequest = HttpWebRequest.Create(apiURI)
    request.Proxy = Nothing
    request.Method = "POST"
    request.ContentType = "application/x-www-form-urlencoded"

    Dim postData As String = "token=22222222222222222"
    Dim encoding As ASCIIEncoding = New ASCIIEncoding()
    Dim byte1 As Byte() = encoding.GetBytes(postData)
    request.ContentLength = byte1.Length
    Dim newStream As Stream = request.GetRequestStream()
    newStream.Write(byte1, 0, byte1.Length)
    Dim Response As HttpWebResponse = request.GetResponse
    Dim ResponseStream As System.IO.Stream = Response.GetResponseStream

    'Create Stream reader
    Dim StreamReader As New System.IO.StreamReader(ResponseStream)
    _jsonString = StreamReader.ReadToEnd
    StreamReader.Close()