CodeGo.net>如何从MIME内容创建图像?
作者:互联网
我试图在.NET Core 2.2框架的顶部使用C#编写一个小型控制台应用程序.
控制台应用程序将向外部API发出HTTP请求,以获取多个图像.我能够向服务器发出请求并获得响应.但是,服务器使用MIMI消息进行多部分响应.
我能够解析请求并为每条消息获取MIME正文.但是,我无法弄清楚如何根据正文内容创建文件.
我尝试将主体作为字符串写入文件,但是没有用
string body = GetMimeBody(message);
File.WriteAllText("image_from_string" + MimeTypeMap.GetExtension(contentType), bytes);
我也尝试像这样将字符串转换为byte [],但仍然无法正常工作
byte[] bytes = Encoding.ASCII.GetBytes(body);
File.WriteAllBytes("image_from_ascii_bytes" + MimeTypeMap.GetExtension(contentType), bytes);
byte[] bytes = Encoding.Default.GetBytes(body);
File.WriteAllBytes("image_from_default_bytes" + MimeTypeMap.GetExtension(contentType), bytes);
byte[] bytes = Encoding.UTF8.GetBytes(body);
File.WriteAllBytes("image_from_utf8_bytes" + MimeTypeMap.GetExtension(contentType), bytes);
“不工作”是指图像无法正确打开.图片查看器显示“图像似乎已损坏或损坏”.
我怎样才能正确地从邮件中做出好的形象?
更新
这是代码以及解析部分
var responseContentType = response.Content.Headers.GetValues("Content-Type").FirstOrDefault();
string splitter = string.Format("--{0}", GetBoundary(responseContentType));
string content = await response.Content.ReadAsStringAsync();
var messages = content.Split(splitter, StringSplitOptions.RemoveEmptyEntries);
foreach (var message in messages)
{
var mimiParts = message.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries);
if (mimiParts.Length == 0)
{
continue;
}
string contentId = Str.GetValue("Content-ID", mimiParts, ':');
string objectId = Str.GetValue("Object-ID", mimiParts, ':');
string contentType = Str.GetValue("Content-Type", mimiParts, ':');
if (string.IsNullOrWhiteSpace(contentId) || string.IsNullOrWhiteSpace(objectId) || string.IsNullOrWhiteSpace(contentType))
{
continue;
}
string body = mimiParts[mimiParts.Length - 1];
var filename = string.Format("{0}_{1}{2}", contentId, objectId, MimeTypeMap.GetExtension(contentType));
var decoded = System.Net.WebUtility.HtmlDecode(data);
File.WriteAllText("image_from_html_decoded_bytes" + filename, decoded);
}
这是解析消息的方法
public class Str
{
public static string GetValue(string startWith, string[] lines, char splitter = '=')
{
foreach (var line in lines)
{
var value = line.Trim();
if (!value.StartsWith(startWith, StringComparison.CurrentCultureIgnoreCase) || !line.Contains(splitter))
{
continue;
}
return value.Split(splitter)[1].Trim();
}
return string.Empty;
}
}
更新2
根据下面的反馈,我尝试使用MimeKit软件包,而不是尝试自己解析响应.以下是我尝试消耗响应的方式.但是,我仍然收到与上述相同的错误.写入图像文件时,出现图像损坏错误.
var responseContentType = response.Content.Headers.GetValues("Content-Type").FirstOrDefault();
if (!ContentType.TryParse(responseContentType, out ContentType documentContentType))
{
return;
}
var stream = await response.Content.ReadAsStreamAsync();
MimeEntity entity = MimeEntity.Load(documentContentType, stream);
Multipart messages = entity as Multipart;
if (messages == null)
{
throw new Exception("Unable to cast entity to Multipart");
}
foreach (MimeEntity message in messages)
{
string contentId = message.Headers["Content-ID"];
string objectId = message.Headers["Object-ID"];
string contentType = message.Headers["Content-Type"];
if (string.IsNullOrWhiteSpace(contentId) || string.IsNullOrWhiteSpace(objectId) || string.IsNullOrWhiteSpace(contentType))
{
continue;
}
var filename = string.Format("{0}_{1}{2}", contentId, objectId, MimeTypeMap.GetExtension(contentType));
message.WriteTo(filename);
}
解决方法:
MimeEntity.WriteTo(文件)将不幸地包含MIME标头,这是导致损坏的错误的原因.
您需要做的是将MimeEntity强制转换为MimePart,然后使用MimePart.Content.DecodeTo(流)保存解码后的内容:
var responseContentType = response.Content.Headers.GetValues("Content-Type").FirstOrDefault();
if (!ContentType.TryParse(responseContentType, out ContentType documentContentType))
{
return;
}
var stream = await response.Content.ReadAsStreamAsync();
MimeEntity entity = MimeEntity.Load(documentContentType, stream);
Multipart multipart = entity as Multipart;
if (multipart == null)
{
throw new Exception("Unable to cast entity to Multipart");
}
foreach (MimePart part in multipart.OfType<MimePart> ())
{
string contentType = part.ContentType.MimeType;
string contentId = part.ContentId;
string objectId = part.Headers["Object-ID"];
if (string.IsNullOrWhiteSpace(contentId) || string.IsNullOrWhiteSpace(objectId) || string.IsNullOrWhiteSpace(contentType))
{
continue;
}
var filename = string.Format("{0}_{1}{2}", contentId, objectId, MimeTypeMap.GetExtension(contentType));
using (var output = File.Create (filename))
part.Content.DecodeTo (output);
}
标签:mime-message,mime,c 来源: https://codeday.me/bug/20191024/1923398.html