简体   繁体   English

如何用C ++ / CLI编写此行?

[英]How to write this line in C++/CLI?

I'm using Visual C++ to call a web service. 我正在使用Visual C ++调用Web服务。 I need to bypass the invalid SSL certificates since I'm getting this error - 由于出现此错误,我需要绕过无效的SSL证书-

The request failed. 请求失败。 The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel. 基础连接已关闭:无法为SSL / TLS安全通道建立信任关系。

While writing the same thing in C#, I used this line - 在C#中编写相同的内容时,我使用了这一行-

System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };

and it worked for me. 它为我工作。

How do I achieve the same thing in Visual C++? 如何在Visual C ++中实现同一目标? How do I write the same line in Visual C++? 如何在Visual C ++中编写同一行?

You need to create callback object explicitly and assign it to corresponding property. 您需要显式创建回调对象,并将其分配给相应的属性。

using namespace System;
using namespace System::Net;
using namespace System::Net::Security;
using namespace System::Security::Cryptography::X509Certificates;


static bool ValidateServerCertificate(
        Object^ sender,
        X509Certificate^ certificate,
        X509Chain^ chain,
        SslPolicyErrors sslPolicyErrors)
    {
        return true;
    }

int main(array<System::String ^> ^args)
{
    // create callback object, pointing to your function
    System::Net::Security::RemoteCertificateValidationCallback^ clb = gcnew RemoteCertificateValidationCallback(ValidateServerCertificate);
    // assign it to the property
    System::Net::ServicePointManager::ServerCertificateValidationCallback = clb;

    return 0;
}

C++/CLI in VS 2010 doesn't support lambdas, so you'll have to write your delegate as a normal function: VS 2010中的C ++ / CLI不支持lambda,因此您必须将委托编写为普通函数:

using namespace System::Net;
using namespace System::Net::Security;
using namespace System::Security::Cryptography::X509Certificates;

bool returnTrueCallback(
    Object^ sender, X509Certificate^ certificate, X509Chain^ chain,
    SslPolicyErrors sslPolicyErrors)
{
    return true;
}

...

ServicePointManager::ServerCertificateValidationCallback =
    gcnew RemoteCertificateValidationCallback(returnTrueCallback);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM