读取 onedrive api 错误

read onedrive api errors

我目前正在使用以下方法尝试使用 onedrive 创建上传会话 api

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.Headers["Authorization"] = "Bearer " + nODUserTokenObj.Access_Token;
request.ContentType = "application/json";
string data =
        @"{
            ""@microsoft.graph.conflictBehavior"": ""replace""
        }";
byte[] postBytes = Encoding.ASCII.GetBytes(data);
request.ContentLength = postBytes.Length;
request.Accept = "application/json";

request.GetRequestStream().Write(postBytes, 0, postBytes.Length);

using (HttpWebResponse httpWebResponse = (HttpWebResponse)request.GetResponse())
{
    using(StreamReader sr = new StreamReader(httpWebResponse.GetResponseStream()))
    {
        string theData = sr.ReadToEnd();

        JObject test = JObject.Parse(theData);
    }
}

目前我收到异常说明

The remote server returned an error: (409) Conflict.

但是查看 Microsoft 文档,我应该会返回一个包含更多信息的 json 对象。

我在遇到 (HttpWebResponse)request.GetResponse() 时抛出异常,而不是收到包含更多错误详细信息的 json 对象,我做错了什么?

我遇到了两个解决方案:

  1. 我最初通过 Fiddler 发送请求并在那里读取响应来解决这个问题。这对我的情况来说很好,因为我正在开发并且只是想弄清楚 onedrive api 是如何工作的。
  2. 未来的解决方案是遵循这样的方法 post 建议 ()

总结post:

catch (WebException ex)
{
    using (var stream = ex.Response.GetResponseStream())
    using (var reader = new StreamReader(stream))
    {
        Console.WriteLine(reader.ReadToEnd());
    }
}
catch (Exception ex)
{
    // Something more serious happened
    // like for example you don't have network access
    // we cannot talk about a server exception here as
    // the server probably was never reached
}