Microsoft Graph API:HttpStatusCode 200,内容长度 -1?

Microsoft Graph API: HttpStatusCode 200, Content-Length -1?

我正在尝试使用 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。我目前被困在这里。有人可以帮我解决我哪里出错/如何进一步调试这段代码。

提前致谢。

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