C#将文件上载到转换服务,然后将生成的文件下载到特定文件夹

C#将文件上载到转换服务,然后将生成的文件下载到特定文件夹,c#,httpclient,dotnet-httpclient,C#,Httpclient,Dotnet Httpclient,我继承了一个C#脚本,该脚本应该将一个pdf文件上传到a,然后下载生成的转换文件。但是这个脚本似乎缺少下载部分 代码如下: using System; using System.IO; using System.Net; using System.Net.Http; using System.Net.Http.Headers; using CustomScript.Api; public class Script { public static CustomScriptReturn C

我继承了一个C#脚本,该脚本应该将一个pdf文件上传到a,然后下载生成的转换文件。但是这个脚本似乎缺少下载部分

代码如下:

using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using CustomScript.Api;

public class Script
{
    public static CustomScriptReturn CustomScript(CustomScriptArguments args)
    {
        using (HttpClient httpClient = new HttpClient())
            {
              httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "token");
              MultipartFormDataContent form = new MultipartFormDataContent();  
              byte[] fileBytes = File.ReadAllBytes(@"C:\Users\user1\Documents\Test\Files\test1.pdf");
              form.Add(new ByteArrayContent(fileBytes, 0, fileBytes.Length), "test1", "test1.pdf"); 
              HttpResponseMessage response = httpClient.PostAsync("https://pdftables.com/api?key=1234567&format=html", form).Result; 
            }
        return CustomScriptReturn.Empty();
    }
}
尽管converter服务的支持人员指出这是正确的,但该脚本在我们使用的程序中运行时没有错误,但实际上没有下载任何内容。也许问题出在
return CustomScriptReturn.Empty
行上,但我不确定,因为我对
C.
比较陌生


要将转换后的文件下载到输入文件所在的同一文件路径,需要向其中添加哪些代码?

当您向web服务发送帖子时,在这种情况下,将pdf转换为所选格式,转换将发生,并将转换后的数据吐回给您。这是返回数据内容的一部分。当前代码设置为“始终返回”

return CustomScriptReturn.Empty();
这也是你永远看不到你所寻找的任何数据的部分原因。更好的方法是获得完整的响应

HttpResponseMessage response = httpClient.PostAsync("https://pdftables.com/api?key=1234567&format=html", form)
然后,您可以在发送回CustomScriptReturn.Empty()之前进行检查以确保一切正常,例如确保HTTP请求状态为OK发送转换后的内容流,或者如果不是send Empty

HttpResponseMessage response = httpClient.PostAsync("https://pdftables.com/api?key=1234567&format=html", form)
if ((int)response.StatusCode == 200)
{
    //obviously you will need to handle converting the return data to your custom type
    return (CustomScriptReturn)response.Content.ReadAsStringAsync();
}
else
{
    return CustomScriptReturn.Empty();
}

它应该是响应变量的一部分谢谢,你能详细说明一下吗?更改HttpResponseMessage response=httpClient.PostAsync(“,form.).Result;至HttpResponseMessage response=httpClient.PostAsync(“,form”);然后在下面的行中执行var content=response.content;并在上面设置一个断点。您可以检查response.Content,它应该包含您正在查找的返回数据