[英]How to use curl in asp.net/VB.net
我想在我的 asp.net 项目中使用 curl。 下面是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¤cy=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/do.net/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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.