【问题标题】:How to use curl in asp.net/VB.net如何在 asp.net/VB.net 中使用 curl
【发布时间】:2021-01-07 06:10:37
【问题描述】:

我想在我的 asp.net 项目中使用 curl。 下面是卷曲。

curl https:www.rtyu.com \
-d "Operation=CREATE_CHECKOUT_SESSION" \
-d "authentication=xxxxxxxxxx" \
-d "name=merchant" \
-d "merchant=xxxxxx" \
-d "operation=PURCHASE" \
-d "id=012245841" \
-d "amount=100.00" \
-d "currency=USD"

任何帮助

【问题讨论】:

    标签: asp.net curl


    【解决方案1】:

    为什么要使用 CURL ?
    您将不得不 pinvoke 它,只是为了从网站下载一些数据?
    没有什么意义。

    为什么不使用 System.Net.WebClient ?

    string url = "https://www.rtyu.com?Operation=CREATE_CHECKOUT_SESSION&authentication=xxxxxxxxxx&name=merchant&merchant=xxxxxx&operation=PURCHASE&id=012245841&amount=100.00&currency=USD";
    
    using (System.Net.WebClient wc = new System.Net.WebClient())
    {
        // wc.Headers.Add("Cookie", "CookieValue");
        wc.Encoding = System.Text.Encoding.UTF8;
    
        string response = wc.DownloadString(url);
    }
    

    要转义传递给 rtyu.com 的值,请使用

    System.Uri.EscapeDataString();
    

    在较新版本的 .NET 中,还可以使用 System.Net.HttpClient:
    https://docs.microsoft.com/en-us/dotnet/api/system.net.http.httpclient?view=netcore-3.1

    如果出于某种奇怪的原因,您需要 CURL,您可以使用 CurlThin:
    https://github.com/stil/CurlThin

    如果您在使用 SSL 证书时遇到问题,可以尝试忽略 SSL 证书验证(将其放在发送网络请求之前):

    System.Net.ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true; 
    

    如果可行,您需要弄清楚您的 SSL 证书为何无效。 为此,您可以使用更复杂的 ServerCertificateValidationCallback 实现,例如:

    /// <summary>
    ///     This is to take care of SSL certification validation which are not issued by Trusted Root CA.
    /// </summary>
    /// <param name="sender">The sender.</param>
    /// <param name="certificate">The certificate.</param>
    /// <param name="chain">The chain.</param>
    /// <param name="sslPolicyErrors">The errors.</param>
    /// <returns></returns>
    /// <code></code>
    public static bool RemoteCertValidate(object sender
        , System.Security.Cryptography.X509Certificates.X509Certificate certificate
        , System.Security.Cryptography.X509Certificates.X509Chain chain
        , System.Net.Security.SslPolicyErrors sslPolicyErrors)
    {
        // If the certificate is a valid, signed certificate, return true.
        if (sslPolicyErrors == System.Net.Security.SslPolicyErrors.None)
        {
            return true;
        }
    
        // Logger.Current.Error("X509Certificate [{0}] Policy Error: '{1}'", certificate.Subject, sslPolicyErrors);
    
    
        // If there are errors in the certificate chain, look at each error to determine the cause.
        if ((sslPolicyErrors & System.Net.Security.SslPolicyErrors.RemoteCertificateChainErrors) != 0)
        {
            if (chain != null && chain.ChainStatus != null)
            {
                foreach (System.Security.Cryptography.X509Certificates.X509ChainStatus status in chain.ChainStatus)
                {
                    if ((certificate.Subject == certificate.Issuer) &&
                       (status.Status == System.Security.Cryptography.X509Certificates.X509ChainStatusFlags.UntrustedRoot))
                    {
                        // Self-signed certificates with an untrusted root are valid. 
                        continue;
                    }
                    else if (status.Status == System.Security.Cryptography.X509Certificates.X509ChainStatusFlags.NotTimeValid)
                    {
                        // Ignore Expired certificates
                        continue;
                    }
                    else
                    {
                        if (status.Status != System.Security.Cryptography.X509Certificates.X509ChainStatusFlags.NoError)
                        {
                            // If there are any other errors in the certificate chain, the certificate is invalid,
                            // so the method returns false.
                            return false;
                        }
                    }
                } // Next status 
    
            } // End if (chain != null && chain.ChainStatus != null) 
    
            // When processing reaches this line, the only errors in the certificate chain are 
            // untrusted root errors for self-signed certificates (, or expired certificates). 
            // These certificates are valid for default Exchange server installations, so return true.
            return true;
        } // End if ((sslPolicyErrors & System.Net.Security.SslPolicyErrors.RemoteCertificateChainErrors) != 0) 
    
        return false;
    }
    

    【讨论】:

    • 不工作。错误:无法创建 SSL/TLS 安全通道。
    • @Jas:这可能意味着 SSL 证书无效。或者根证书不受 .NET 信任。对于测试,您可以尝试(在调用 System.Net.WebClient 之前): System.Net.ServicePointManager.ServerCertificateValidationCallback += (sender, certificate, chain, sslPolicyErrors) => true;另见stackoverflow.com/questions/63626244/…
    猜你喜欢
    • 2020-11-07
    • 1970-01-01
    • 1970-01-01
    • 2016-07-06
    • 1970-01-01
    • 2013-04-13
    • 1970-01-01
    • 2015-11-13
    • 2014-09-26
    相关资源
    最近更新 更多