This seems to be a very old issue however, all the said solutions are not working for me. I am making a request to a client and receive a HTTPS reponse that I used fiddler to capture early on, however now my application doesn't work without Fiddler certificates. When I select the descrypt HTTPS traffic in fiddler then the app works or else it doesn't even if I remove the HTTPS capture option from Fiddler and remove the fiddler certificate.
I have also tried to remove fiddler altogether but that din't solve the issue as well. I am out of options as of now. This is my code
Dim cert As X509Certificate = X509Certificate.CreateFromCertFile("Cert Location")
ServicePointManager.ServerCertificateValidationCallback = New RemoteCertificateValidationCallback(AddressOf OnCertificateValidation)
ServicePointManager.Expect100Continue = False
Dim request As HttpWebRequest = CType(HttpWebRequest.Create(WebURL), HttpWebRequest)
request.Method = "POST"
Dim requestDataByte() As Byte = Encoding.UTF8.GetBytes(CreateRequestXML)
request.ContentLength = requestDataByte.Length
Dim requestdata As Stream = request.GetRequestStream()
requestdata.Write(requestDataByte, 0, requestDataByte.Length)
requestdata.Close()
request.GetRequestStream.Close()
request.ClientCertificates.Add(cert)
request.KeepAlive = False
Dim response As HttpWebResponse = CType(request.GetResponse, HttpWebResponse)
If response.StatusCode > 400 Then
MsgBox("Error:" + response.StatusDescription + vbCrLf + response.StatusCode)
End If
Dim response_stream As Stream = response.GetResponseStream
response_stream.Close()
response.Close()
Dim reader As StreamReader = New StreamReader(response_stream)
_response = reader.ReadToEnd
reader.Close()
I keep receiving the 500 internal server error. Edit: I was not closing the response connection properly before however I modified that part of the code long back. Shouldn't that modification have solved my problem?
I have modified the code as per the posts I could find online. But still unable to resolve the issue.
The solution to the above problem is to have the ClientCertificates.add() method just after you define the request object. And to close your response streams properly by calling the appropriate .Close() methods.