您需要从 VKontakte 对话框下载图像。
对话是用户和社区之间的,我们作为一个社区来行动。
使用令牌登录
var api = new VkApi();
api.Authorize(new ApiAuthParams
{
AccessToken = "мой_токен",
});
我们收到未答复的社区对话:
var messagesGet = api.Messages.GetDialogs(new MessagesDialogsGetParams
{
Count = 200,
Unanswered = true
});
我们得到所有附件的列表(邮件附加的照片):
MessagesGetHistoryAttachmentsParams attachmentsParams = new MessagesGetHistoryAttachmentsParams();
attachmentsParams.MediaType = VkNet.Enums.SafetyEnums.MediaType.Photo;
foreach (var mes in messagesGet.Messages)
{
string str;
attachmentsParams.PeerId = 291249709;
List<HistoryAttachment>attachments =api.Messages.GetHistoryAttachments(attachmentsParams, out str).ToList();
foreach(HistoryAttachment attachment in attachments)
{
WebClient webClient = new WebClient();
Photo photo = (Photo)attachment.Attachment.Instance;
MessageBox.Show(photo.BigPhotoSrc.ToString()); //на этой строке получаем ошибку System.NullReferenceException
}
}
在尝试获取图像链接以供进一步下载 ( photo.BigPhotoSrc.ToString()) 时,我们收到以下文本的异常:
System.NullReferenceException:“对象引用未指向对象的实例。”
VkNet.Model.Attachments.Photo.BigPhotoSrc.get返回null。
通过调试器,我设法发现许多 Photo 字段被定义为 null,尤其是我请求的 PhotoSrc
如何解决这个问题?
从5.77 版本开始,API 改变了照片对象的形式,服务器不返回诸如
photo_***. 图像的所有大小和地址仍然可以从数组中sizes获取,我们可以通过type的值从中获取所需的大小。我们可以通过开发站点发送请求来查看服务器发送给我们的内容。
响应将是这样的 JSON:
如您所见,服务器并没有回馈给我们
photo_***,而是sizes当场回馈!我还想指出,您使用的messages.getDialogs方法自5.80 版以来已被弃用!我建议您切换到较新的messages.getConversations方法。