我们正在升级到ConvertAPI指定的新逻辑。但是我们使用的框架是4,因此找不到System.Net.Http(HttpResponseMessage、HttpClient、MultipartFormDataContent、StreamContent、StringContent)类。甚至添加了System.Net.Http引用。但同样的错误依然存在。有人能提出一些解决办法吗?如何在ConvertApi框架4的C#中使用.Net?
发布于 2017-10-12 14:54:35
只需使用WebClient而不是HttpClient来快速上传文件。要运行下面的演示程序,您需要替换文件路径并设置秘密,而不是端点url中的xxxxx。另外,我们应该将accept:application/octet-stream头设置为数据流而不是json来获得响应。
class Program
{
static void Main(string[] args)
{
const string fileToConvert = @"C:\Projects\_temp\test1.docx";
const string fileToSave = @"C:\Projects\_temp\test1.pdf";
try
{
using (var client = new WebClient())
{
client.Headers.Add("accept", "application/octet-stream");
var resultFile = client.UploadFile(new Uri("https://v2.convertapi.com/docx/to/pdf?Secret=xxxxx"), fileToConvert);
File.WriteAllBytes(fileToSave, resultFile );
Console.WriteLine("File converted successfully");
}
}
catch (WebException e)
{
Console.WriteLine("Status Code : {0}", ((HttpWebResponse)e.Response).StatusCode);
Console.WriteLine("Status Description : {0}", ((HttpWebResponse)e.Response).StatusDescription);
Console.WriteLine("Body : {0}", new StreamReader(e.Response.GetResponseStream()).ReadToEnd());
}
Console.ReadLine();
}
}https://stackoverflow.com/questions/46710530
复制相似问题