在 Exchange Web 服务中缓存用户照片

Caching user photos in Exchange Web Services

根据联系人照片上的 MSDN 文档,以下内容过于含糊,如果熟悉该主题的人可以提供有关如何正确进行缓存的更多见解,我将不胜感激。

到目前为止,这是我的代码...

https://msdn.microsoft.com/en-us/library/office/jj190905(v=exchg.150).aspx#bk_EWSMA

Caching user photos

Exchange returns the data with a content type of image/jpeg, along with a collection of header values. The ETag header is similar to a change key. The value is a string that represents the last time the photo was updated. The ETag remains the same for the user photo until the photo is changed. You can send this ETag value to the server in the HTTPS GET request in an If-None-Match header. If the photo hasn’t changed since the last request, the server will respond with an HTTP 304 response that indicates as such. This means that you can use the user photo that you previously requested and saved rather than processing a new one.

    Dim oPictureRequest As HttpWebRequest
    Dim strHttpPhotoEndPoint As String = pExchangeContact.Service.Url.ToString & "/s/GetUserPhoto?email=" & pExchangeContact.Service.ImpersonatedUserId.Id & "&size=HR240x240"
    Dim strPictureFilePath As String = config.PrivateContactPicturesPath & "/" & pEmployeeId.ToString & "/" & pCRMContact.ContactId.ToString & ".jpg"
    Dim oCachePolicy As New HttpRequestCachePolicy(HttpRequestCacheLevel.Revalidate)

    If pExchangeContact.HasPicture Then
        oPictureRequest = DirectCast(WebRequest.Create(strHttpPhotoEndPoint), HttpWebRequest)
        oPictureRequest.CachePolicy = oCachePolicy
        Using oPictureResponse As HttpWebResponse = DirectCast(oPictureRequest.GetResponse(), HttpWebResponse)
            If oPictureResponse.StatusCode = HttpStatusCode.OK Then
                Dim oPicture As Bitmap = New Bitmap(oPictureResponse.GetResponseStream())
                oPicture.Save(strPictureFilePath)
            End If
        End Using

    End If

看起来您已经在使用 EWS Managed API 如果您从 github https://github.com/OfficeDev/ews-managed-api 获得最新版本,那么您可以使用 SOAP 操作和 Managed API 有代码来处理 headers 例如

        String ETag = "";
        GetUserPhotoResults grPhoto = service.GetUserPhoto("user@domain.com", "HR240x240", ETag);
        if (grPhoto.Status == GetUserPhotoStatus.PhotoReturned) 
        {
            ETag = grPhoto.EntityTag; 
        }
        grPhoto = service.GetUserPhoto("user@domain.com", "HR240x240", ETag);
        switch (grPhoto.Status) 
        {
            case GetUserPhotoStatus.PhotoReturned: ETag = grPhoto.EntityTag;
                break;
            case GetUserPhotoStatus.PhotoUnchanged:
                Console.WriteLine("Photo Unchanged");
                break;
        }