curl Http请求从远程服务器下载文件

vd2z7a6w  于 2023-03-03  发布在  其他
关注(0)|答案(1)|浏览(199)

我正在尝试转换以下curl命令

curl --location --request GET "https://xxx.xxxx.xxx/artifacts?session=016e1f70-d9da-41bf-b93d-80e281236c46&path=/home/gauntlet_gameye/LinuxServer/Game/Saved/Logs/Game.log" -H "Authorization:Bearer xxxx" -H "Accept:application/json" --output C:\Temp\Game.log

转换成C#代码,我有以下代码

string SessionId = "016e1f70-d9da-41bf-b93d-80e281236c46";
string Token = "xxxx"; 
string FilePath = "/home/gauntlet_gameye/LinuxServer/Game/Saved/Logs/Game.log";  

string Endpoint = string.Format("https://xxx.xxxx.xxx/artifacts?session={0}&path={1}", SessionId, FilePath);            
HttpRequestMessage HttpRequestMsg = new HttpRequestMessage();
HttpRequestMsg.RequestUri = new Uri(Endpoint);
HttpRequestMsg.Method = HttpMethod.Get;
HttpRequestMsg.Headers.Add("Accept", "application/json");
HttpRequestMsg.Headers.Add("Authorization", string.Format("Bearer {0}", Token));

HttpRequestMsg.Content = new StringContent(string.Format("--output {0}", OutFilePath), Encoding.UTF8, "application/json");

using (HttpClient Client = new HttpClient())
{
    var HttpResponseTask = Client.SendAsync(HttpRequestMsg);
}

但它提供了以下异常信息

Cannot send a content-body with this verb-type.
nzkunb0c

nzkunb0c1#

  • -location和--output不是C#支持的选项
HttpClient client = new HttpClient();
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "https://xxx.xxxx.xxx/artifacts?session=016e1f70-d9da-41bf-b93d-80e281236c46&path=/home/gauntlet_gameye/LinuxServer/Game/Saved/Logs/Game.log");

request.Headers.Add("Authorization", "Bearer xxxx");
request.Headers.Add("Accept", "application/json");

HttpResponseMessage response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();
string responseBody = await response.Content.ReadAsStringAsync();

来源:Convert curl commands to C#

相关问题