如何 post 图像正确地 URL ?
How to post image to URL properly?
我正在尝试 post 将图像发送到 StockTwits API,但收到 422 错误作为响应,表示无法识别图像格式。该图像是 png 文件,因此它应该是有效的文件格式。我试过使用具有相同响应的 jpeg。
我是否对图像编码不正确,或者 HttpClient
中缺少某些配置?
public void Share(string text, string imageFilePath)
{
using(HttpClient client = new HttpClient())
using (MultipartFormDataContent formData = new MultipartFormDataContent())
{
string url = stocktwitsCreateMessageUrl + "&" +
"body=" + Core.Globals.UrlEncode(twit) + "&" +
"sentiment=" + StockTwitsSentiment.ToString().ToLower();
byte[] imageBytes = File.ReadAllBytes(imageFilePath);
HttpContent imageContent = new ByteArrayContent(imageBytes);
string fileName = Path.GetFileName(imageFilePath);
formData.Add(imageContent, "chart", fileName);
HttpResponseMessage stockTwitsResponse = await client.PostAsync(url, formData);
Stream responseContent = await stockTwitsResponse.Content.ReadAsStreamAsync();
string result = new StreamReader(responseContent).ReadToEnd();
Print(result); // helper method
if (!stockTwitsResponse.IsSuccessStatusCode)
{
LogErrorResponse(result, stockTwitsResponse);
return;
}
else
LogAndPrint(typeof(Custom.Resource), "ShareStockTwitsSentSuccessfully", new[] { Name }, Cbi.LogLevel.Information);
}
}
回复:
{
"response": { "status": 422 },
"errors": [ { "message": "We couldn't recognize the image format. Format must be one of: image/jpg image/jpeg image/pjpeg image/png image/x-png image/gif" } ]
}
别忘了将 Content-Type
header 设置为 "image/png"
!
参见:https://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.contenttype(v=vs.110).aspx
我正在尝试 post 将图像发送到 StockTwits API,但收到 422 错误作为响应,表示无法识别图像格式。该图像是 png 文件,因此它应该是有效的文件格式。我试过使用具有相同响应的 jpeg。
我是否对图像编码不正确,或者 HttpClient
中缺少某些配置?
public void Share(string text, string imageFilePath)
{
using(HttpClient client = new HttpClient())
using (MultipartFormDataContent formData = new MultipartFormDataContent())
{
string url = stocktwitsCreateMessageUrl + "&" +
"body=" + Core.Globals.UrlEncode(twit) + "&" +
"sentiment=" + StockTwitsSentiment.ToString().ToLower();
byte[] imageBytes = File.ReadAllBytes(imageFilePath);
HttpContent imageContent = new ByteArrayContent(imageBytes);
string fileName = Path.GetFileName(imageFilePath);
formData.Add(imageContent, "chart", fileName);
HttpResponseMessage stockTwitsResponse = await client.PostAsync(url, formData);
Stream responseContent = await stockTwitsResponse.Content.ReadAsStreamAsync();
string result = new StreamReader(responseContent).ReadToEnd();
Print(result); // helper method
if (!stockTwitsResponse.IsSuccessStatusCode)
{
LogErrorResponse(result, stockTwitsResponse);
return;
}
else
LogAndPrint(typeof(Custom.Resource), "ShareStockTwitsSentSuccessfully", new[] { Name }, Cbi.LogLevel.Information);
}
}
回复:
{
"response": { "status": 422 },
"errors": [ { "message": "We couldn't recognize the image format. Format must be one of: image/jpg image/jpeg image/pjpeg image/png image/x-png image/gif" } ]
}
别忘了将 Content-Type
header 设置为 "image/png"
!
参见:https://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.contenttype(v=vs.110).aspx