我收到错误"(400)错误请求"将ConvertApi Web调用到Pdf Api。
答案 0 :(得分:0)
using (var client = new WebClient())
{
client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
var response = client.UploadString("https://v2.convertapi.com/web/to/pdf?secret=" + Secret + "&Url=" + value + "&ConversionDelay=" + ConversionDelay, "");
var ocontent3 = JsonConvert.DeserializeObject<FileList>(response);
byte[] result = ocontent3.Files[0].FileData;
}
答案 1 :(得分:0)
ConvertAPI和许多其他使用二进制数据操作的Rest Api支持multipart或application / octet-stream(二进制文件)响应,最好在C#中使用二进制响应而不是json(textual)。它会更快 - 响应体将更小,下载时间更短,无需解码来自JSON Base64的二进制数据。
所以代码可能是
const string secret = "<YourSecret>";
const string url = "http://www.google.com";
const int conversionDelay = 1;
const string fileToSave = @"C:\Projects\_temp\test1.pdf";
using (var client = new WebClient())
{
client.Headers.Add("accept", "application/octet-stream");
var response = new byte[] { };
try
{
response = client.UploadValues("https://v2.convertapi.com/web/to/pdf?secret=" + secret, "POST", new NameValueCollection
{
{ "Url", url },
{ "ConversionDelay", conversionDelay.ToString() }
});
}
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());
}
if (response != null)
File.WriteAllBytes(fileToSave, response);
}