我有一个项目需要通过网络请求发送数据文件。我们需要设置双向身份验证,也称为相互身份验证。我们不确定是否需要特殊证书,但我们知道它需要达到 3 级。
我无法找到此案例的示例代码。我不知道在哪里添加我们的证书信息。使用此代码,Underlying connection is closed
当我们尝试读取响应流并且ServicePointManager.ServerCertificateValidationCallback
永远不会被调用时,会引发错误。这是我所拥有的:
ServicePointManager.ServerCertificateValidationCallback = New Security.RemoteCertificateValidationCallback(AddressOf MyCertValidationCb)
httpReq = CType(System.Net.HttpWebRequest.Create(url), HttpWebRequest)
For Each cert As String In certs
X509cert = X509Certificate2.CreateFromCertFile(cert)
X509cert2 = New X509Certificate2(X509cert)
httpReq.ClientCertificates.Add(X509cert2)
Next
httpReq.Method = "POST" ' Post method
httpReq.ContentType = "text/xml" ' content type
' Wrap the request stream with a text-based writer
writer = New StreamWriter(httpReq.GetRequestStream())
' Write the XML text into the stream
reader = New StreamReader(filename.Name)
ret = reader.ReadToEnd()
reader.Close()
' Send the data to the webserver
writer.WriteLine(ret)
writer.Close()
' Wait for response
Dim httpRsp As System.Net.HttpWebResponse = CType(httpReq.GetResponse(), HttpWebResponse)
sr = New StreamReader(httpRsp.GetResponseStream)
responseText = sr.ReadToEnd
If httpReq IsNot Nothing Then
httpReq.GetRequestStream().Close()
End If
If httpRsp IsNot Nothing Then
httpRsp.GetResponseStream().Close()
End If
任何带有示例代码的博客提示或链接都会很棒。谢谢。