httpwebrequet for https 失败,"The system cannot find the file specified exception"



我正在创建一个" 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);

CustomServiceCertificateDatificateValidation

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)

使用命令" openssl s_client -connect 127.0.0.1:portno"

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

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

请一些人对此有帮助。

谢谢。

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

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

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

您应该在呼叫request.GetResponse()周围放置一个try ... catch并检查例外。我认为您正在获得FileNotFoundException。根据文档,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子句中并检查例外。那应该告诉你要找到什么文件。

相关内容

最新更新