lin_sgr
lin_sgr

Reputation: 25

httpwebrequet for https fails with "The system cannot find the file specified exception"

I am creating an "httpWebRequest"with following code

System.Net.WebRequest.DefaultWebProxy = null;
HttpWebRequest request =  (HttpWebRequest)WebRequest.Create(uri);
request.Method = "GET";
ServicePointManager.ServerCertificateValidationCallback += CustomServiceCertificateValidation;
X509Certificate2 clientCertificate = new X509Certificate2("Client.cer");
request.ClientCertificates.Add(clientCertificate);

CustomServiceCertificateValidation function

private static bool CustomServiceCertificateValidation(
                object sender, X509Certificate cert, X509Chain chain,
                SslPolicyErrors error)
{
     return true;
}

when I call

WebResponse response = request.GetResponse();

I get "The system cannot find the file specified exception". Following trace I got it

at System.Security.Cryptography.CryptographicException.ThrowCryptographicException(Int32 hr)
   at System.Security.Cryptography.X509Certificates.X509Utils._QueryCertFileType(String fileName)
   at System.Security.Cryptography.X509Certificates.X509Certificate.LoadCertificateFromFile(String fileName, Object password, X509KeyStorageFlags keyStorageFlags)
   at System.Security.Cryptography.X509Certificates.X509Certificate2..ctor(String fileName)
   at Transport.SendOut.CustomServiceCertificateValidation(Object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors error)
   at System.Net.Security.RemoteCertificateValidationCallback.Invoke(Object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
   at System.Net.ServerCertValidationCallback.Callback(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Net.ServerCertValidationCallback.Invoke(Object request, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
   at System.Net.Security.SecureChannel.VerifyRemoteCertificate(RemoteCertValidationCallback remoteCertValidationCallback)
   at System.Net.Security.SslState.CompleteHandshake()
   at System.Net.Security.SslState.CheckCompletionBeforeNextReceive(ProtocolToken message, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.ProcessReceivedBlob(Byte[] buffer, Int32 count, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.StartReceiveBlob(Byte[] buffer, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.StartSendBlob(Byte[] incoming, Int32 count, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.ForceAuthentication(Boolean receiveFirst, Byte[] buffer, AsyncProtocolRequest asyncRequest)
   at System.Net.Security.SslState.ProcessAuthentication(LazyAsyncResult lazyResult)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Net.TlsStream.ProcessAuthentication(LazyAsyncResult result)
   at System.Net.TlsStream.Write(Byte[] buffer, Int32 offset, Int32 size)
   at System.Net.PooledStream.Write(Byte[] buffer, Int32 offset, Int32 size)
   at System.Net.ConnectStream.WriteHeaders(Boolean async)

using Command "openssl s_client -connect 127.0.0.1:portno"

able to connect and return the server certificate.

In browser, I am able to access the same URL.

Please some one help me on this.

Thanks.

Upvotes: 0

Views: 620

Answers (1)

Jim Mischel
Jim Mischel

Reputation: 134125

It looks to me like it can't find the file "client.cer". Make sure that the file is in the application's current working directory. Add this code above what you've posted:

if (!System.IO.File.Exists("client.cer"))
{
    throw new FileNotFoundException();
}

You either need to put the file in the application's working directory, or supply a path so that the system can find it.

You should put a try ... catch around your call to request.GetResponse() and examine the exception. I assume you're getting FileNotFoundException. According to the documentation, the FileName property will tell you which file it was trying to read. So:

try
{
    WebResponse response = request.GetResponse();
}
catch (FileNotFoundException fex)
{
    Console.WriteLine("Unable to find file " + fex.FileName);
}
catch (Exception ex)
{
    Console.WriteLine("Some other exception.");
}

In the debugger, put breakpoints inside both of the catch clauses and examine the exception. That should tell you what file it's trying to find.

Upvotes: 1

Related Questions