将Curl的--key和--cert转换为VB.NET

时间:2019-01-08 22:04:35

标签: vb.net curl webservice-client

我需要通过VB.NET发布两个文件,一个密钥文件和一个pem文件,这可以通过Curl使用以下命令来完成:

curl https://someWebSite.com/test --key ./private_key.key --cert ./certificate.pem -d grant_type=password -d username="" -d password=""

到目前为止,我已经在VB.NET中完成了此操作,但是它只是弹出一个错误,提示“ Additional information: The remote server returned an error: (401) Unauthorized.

我举了一个例子,他们使用一个* .p12文件,我试图在VB.NET中重新创建它,但仍然说同样的错误。这是我的代码:

        Dim token As String = ""
    Dim uriVar As New Uri("https://someWebSite.com/test")
    Dim user As String = ""
    Dim password As String = ""



    Dim request As HttpWebRequest = CType(WebRequest.Create(uriVar), HttpWebRequest)
    request.KeepAlive = False
    request.ProtocolVersion = HttpVersion.Version11
    request.Method = "POST"

    Dim credential As New NetworkCredential(user, password)
    request.Credentials = credential
    Dim keystoreFileName As String = "keystore.p12"
    Dim pathCert As String = Path.Combine(Environment.CurrentDirectory, "cert\", keystoreFileName)
    Dim cert As New X509Certificate(pathCert, "allpassword")
    request.ClientCertificates.Add(cert)


    request.ContentType = "application/x-www-form-urlencoded"
    request.ContentLength = 39
    request.Accept = "*/*"

    Dim requestStream As Stream = request.GetRequestStream()
    Dim post_data As String = "grant_type=password&username=&password="
    Dim postBytes As Byte() = Encoding.ASCII.GetBytes(post_data)
    requestStream.Write(postBytes, 0, postBytes.Length)
    requestStream.Close()

    Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse) 'Here pops the error...
    Dim responseJson = New StreamReader(response.GetResponseStream()).ReadToEnd()

    Return responseJson

0 个答案:

没有答案