I am having problems with posting multiple files to our customer using a ssl URL with a certificate. I seem to be able to send one file to them at a time (with some unknown interval of time between them), but I am unable to send files in succession when sending multiple files. I have spoken with their team, and supposedly we are able to use the same connection for multiple files and/or use parallel sessions. Below is the code that actually does the "sending" of the file. I call the code from another procedure that is looping through a list of files to send. These are XML files that we have generated and the file structure is fine with all of the files we attempt to send. Any help would be much appreciated.
Code:
Public Sub SendData2Customer(ByVal strURL As String, ByVal strFileName As String)
Dim webReq As HttpWebRequest
Dim i As Integer, mySTR As String
Dim mystore As New X509Store(StoreLocation.CurrentUser)
Dim certificate As X509Certificate2
Dim myProxy As New WebProxy(proxyAddress & ":" & proxyPort)
Dim myRdr As StreamReader
Dim myWrtr As StreamWriter
If UserCert = "" Then
MsgBox("You must specify a SSL Certificate to use in the transmission. " & _
"Go to the options page and specify an appropriate certificate.", MsgBoxStyle.Exclamation, _
"Certificate Missing")
returnMsg = "ERROR,NO CERT,NO RESPONSE"
Exit Sub
End If
Try
mystore.Open(OpenFlags.ReadOnly)
i = 0
For Each certificate In mystore.Certificates
If certificate.Subject.ToString = UserCert Then
Exit For
End If
i = i + 1
Next
certificate = mystore.Certificates(i)
i = 0 : mySTR = ""
webReq = HttpWebRequest.Create(strURL)
webReq.KeepAlive = False
webReq.Method = "POST"
webReq.AllowAutoRedirect = True
webReq.ContentType = "text/xml"
webReq.Timeout = 100000
webReq.AllowWriteStreamBuffering = True
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Ssl3
If UseProxy = True Then
myProxy.Credentials = New NetworkCredential(proxyUser, proxyPassword, proxyDomain)
webReq.Proxy = myProxy
End If
webReq.ClientCertificates.Add(certificate)
'Debug.Print(webReq.ClientCertificates.Count)
myRdr = New StreamReader(strFileName)
myWrtr = New StreamWriter(webReq.GetRequestStream)
myWrtr.Write(myRdr.ReadToEnd)
myWrtr.Close()
myRdr.Close()
myRdr = Nothing
myWrtr = Nothing
Dim myRsp As HttpWebResponse = Nothing
myRsp = webReq.GetResponse
If myRsp IsNot Nothing Then
Dim mySR As New StreamReader(myRsp.GetResponseStream)
returnMsg = myRsp.StatusCode & "," & _
myRsp.StatusDescription & "," & mySR.ReadToEnd
Else
returnMsg = "ERROR,NO RESPONSE FROM SERVER,NO RESPONSE"
End If
If myRsp IsNot Nothing Then
myRsp.Close()
myRsp = Nothing
End If
webReq.Abort()
Catch we As WebException
MsgBox("error Occurred: " & we.Message)
returnMsg = "ERROR," & we.Message & "," & we.GetBaseException.ToString
If we.Response IsNot Nothing Then
we.Response.Close()
End If
myRdr = Nothing
myWrtr = Nothing
End Try
End Sub