我在将 ConvertApi web 调用为 Pdf Api 时收到错误“(400) Bad Request”。
问问题
493 次
2 回答
0
ConvertAPI 和许多其他使用二进制数据操作的 Rest Api 支持 multipart 或 application/octet-stream(binary file) 响应,最好在 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);
}
于 2017-11-20T14:47:58.917 回答
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;
}
于 2017-10-24T06:53:11.077 回答