我们正在升级到ConvertAPI指定的新逻辑。但是我们使用的框架是4,因为没有找到using System.Net;
using System.IO;
string url = "GET https://api.companieshouse.gov.uk/search/companies";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Credentials = // here you have to set your authentication data
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream resStream = response.GetResponseStream();
,System.Net.Http(HttpResponseMessage
,HttpClient
,MultipartFormDataContent
,StreamContent
)类。甚至添加了StringContent
引用。但仍然存在同样的错误。
有人可以建议一些解决方法吗?如何在C#中使用ConvertApi for .Net Framework 4?
答案 0 :(得分:1)
只需使用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();
}
}