https的httpwebrequet因&#34而失败;系统无法找到指定的异常文件"

时间:2016-12-10 04:18:58

标签: c# https httpwebrequest x509certificate

我正在创建一个" httpWebRequest"以及以下代码

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函数

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

当我打电话

WebResponse response = request.GetResponse();

我得到"系统找不到指定的异常文件"。 跟踪我得到了它

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)

使用Command " openssl s_client -connect 127.0.0.1:portno"

能够连接并返回服务器证书。

在浏览器中,我可以访问相同的网址。

请有人帮助我。

感谢。

1 个答案:

答案 0 :(得分:1)

在我看来,它无法找到文件" client.cer"。确保该文件位于应用程序的当前工作目录中。将此代码添加到您发布的内容之上:

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

您需要将文件放在应用程序的工作目录中,或提供路径以便系统可以找到它。

您应该在try ... catch的呼叫周围放置一个request.GetResponse()并检查异常。我假设你得到了FileNotFoundException。根据{{​​3}},FileName属性将告诉您它正在尝试读取哪个文件。所以:

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

在调试器中,在两个catch子句中放置断点并检查异常。这应该告诉你它试图找到什么文件。