如何在asp.net/VB.net中使用curl

  • 本文关键字:net curl VB asp asp.net curl
  • 更新时间 :
  • 英文 :


我想在我的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"

有什么帮助吗

为什么要使用CURL
你必须点击才能从网站下载一些数据
没什么意义

为什么不使用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://learn.microsoft.com/en-us/dotnet/api/system.net.http.httpclient?view=netcore-3.1

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

如果您在SSL证书方面遇到问题,您可以尝试忽略SSL证书验证(将其放在发送web请求之前(:

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

最新更新