gpt4 book ai didi

c# - 将 Curl 脚本转换为 C#

转载 作者:行者123 更新时间:2023-11-30 17:41:45 25 4
gpt4 key购买 nike

我有一个下载文件的 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);
}

使用代理更新了 C# 代码...

WebProxy proxy = new WebProxy("myurl:8080", true);
proxy.Credentials = new NetworkCredential("admin", "pswrd");
client.Proxy = proxy;

错误:远程服务器返回错误:(403) 禁止。

最佳答案

使用以下选项之一:

HttpWebRequest/HttpWebResponseWeb客户端HttpClient(从 .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());
}

这个答案来自this site .

我发现的另一个引用 this网络解决方案。

您也可以尝试查看this回答。

关于c# - 将 Curl 脚本转换为 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32501249/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com