【问题标题】:Microsoft Graph API: HttpStatusCode 200, Content-Length -1?Microsoft Graph API:HttpStatusCode 200,内容长度 -1?
【发布时间】:2016-12-13 07:47:47
【问题描述】:

我正在尝试使用 Ms Graph API 连接到 Outlook 并下载附件。我到现在写的是

 private static async Task<HttpWebRequest> createHttpRequestWithToken(Uri uri)
 {
        HttpWebRequest newRequest = (HttpWebRequest)HttpWebRequest.Create(uri);

        string clientId = "myClientId";
        string clientSecret = "myClientSecret";
        ClientCredential creds = new ClientCredential(clientId, clientSecret);

        AuthenticationContext authContext = new AuthenticationContext("https://login.windows.net/myAzureAD/oauth2/token");
        AuthenticationResult authResult = await authContext.AcquireTokenAsync("https://graph.microsoft.com/", creds);
        newRequest.Headers.Add(HttpRequestHeader.Authorization, "Bearer " + authResult.AccessToken);
        newRequest.ContentType = "application/json";

        return newRequest;
  } 

我正在使用它来调用我需要的图形 API。因此,首先,我尝试调用此 URL:

Uri uri = new Uri(("https://graph.microsoft.com/v1.0/users/myEmailId/messages"));
HttpWebRequest request = createHttpRequestWithToken(uri).Result;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

运行代码后,我收到 HttpStatusCode 200 的响应,但 Content-Length 为 -1。我目前被困在这里。有人可以帮我解决哪里出错/如何进一步调试这段代码。

提前致谢。

【问题讨论】:

    标签: c# azure outlook office365 microsoft-graph-api


    【解决方案1】:

    API 使用“Transfer-Encoding: chunked”,因此不会返回 Content-Length 标头。这就是为什么您看到 response.ContentLength 属性的默认值为 -1 的原因。要读取响应体,只需读取通过 response.GetResponseStream() 方法获得的响应流。

    【讨论】:

    • 天啊...你是对的,删除内容长度检查就可以了。谢谢
    猜你喜欢
    • 1970-01-01
    • 2021-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-12
    • 2020-07-11
    相关资源
    最近更新 更多