将Curl脚本转换为c#
本文关键字:转换 脚本 Curl | 更新日期: 2023-09-27 18:12:01
我有一个下载文件的curl脚本。如何将其转换为c# ?
Dim strCurl1
Dim strCurl2
Dim strCurl3
Dim strCurl4
strCurl1 = "c:'temp'Curl'curl -d "
strCurl2 = "username=myUser&password=passwrd&asof=" & fileName & "&format=csv"
strCurl3 = "https://website/PresentValueServlet > " & "c:'temp'Curl'Results_" & fileName & ".csv"
strCurl4 = strCurl1 & Chr(34) & strCurl2 & Chr(34) & " " & strCurl3 & " --insecure --proxy proxy.myserver.com:8080 --proxy-user admin:passAdmin"
Set objShell = CreateObject("WScript.Shell")
objShell.run "cmd /K " & strCurl4
我的c#到目前为止…
string username = "myUser";
string password = "passwrd";
string fileDestination = @"c:'temp'Curl'Results_" + fileName + ".csv";
using (WebClient client = new WebClient())
{
client.Credentials = new NetworkCredential(username, password);
client.DownloadFile(string.Format("https://website/PresentValueServlet"), fileDestination);
}
WebProxy proxy = new WebProxy("myurl:8080", true);
proxy.Credentials = new NetworkCredential("admin", "pswrd");
client.Proxy = proxy;
Error: 远程服务器返回错误:(403)Forbidden.
使用以下选项之一:
HttpWebRequest/HttpWebResponseWebClientHttpClient(从。net 4.5起可用)
我强烈推荐使用HttpClient类,因为它被设计得比前两种要好得多(从可用性的角度来看)。
在你的例子中,你会这样做:
using System.Net.Http;
var client = new HttpClient();
// Create the HttpContent for the form to be posted.
var requestContent = new FormUrlEncodedContent(new [] {
new KeyValuePair<string,>("text", "This is a block of text"),
});
// Get the response.
HttpResponseMessage response = await client.PostAsync(
"http://api.repustate.com/v2/demokey/score.json",
requestContent);
// Get the response content.
HttpContent responseContent = response.Content;
// Get the stream of the content.
using (var reader = new StreamReader(await responseContent.ReadAsStreamAsync()))
{
// Write the output.
Console.WriteLine(await reader.ReadToEndAsync());
}
这个答案来自这个网站。
另一个参考,我在网上找到了这个解决方案。
你也可以试着看看这个答案