简体   繁体   中英

How to use curl in asp.net/VB.net

i want to use curl in my asp.net project. below is the 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"

any help

Why would you want to use CURL?
You would have to pinvoke it, just to download some data from a website?
Makes little sense.

Why not use 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);
}

To escape the values you pass to rtyu.com, use

System.Uri.EscapeDataString();

In the newer versions of .NET, you can also use System.Net.HttpClient:
https://learn.microsoft.com/en-us/do.net/api/system.net.http.httpclient?view.netcore-3.1

If for some strange reason, you need CURL non-the-less, you could use CurlThin:
https://github.com/stil/CurlThin

If you have trouble with the SSL certificate, you could try ignoring SSL-certificate validation (put this before sending the web-request):

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

If that works, you need to figure out your why your SSL-certificate is invalid. For that, you can use a more sophisticated implementation of ServerCertificateValidationCallback, eg:

/// <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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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