Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
尝试使用C#Receive 400连接到Dropbox API v2时出现所有尝试错误_C#_Json_Dropbox - Fatal编程技术网

尝试使用C#Receive 400连接到Dropbox API v2时出现所有尝试错误

尝试使用C#Receive 400连接到Dropbox API v2时出现所有尝试错误,c#,json,dropbox,C#,Json,Dropbox,新的Dropbox API文档位于: 我正在尝试执行一个简单的元数据调用,但几乎没有成功。以下是我使用的代码: private void go() { var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://api.dropbox.com/2-beta/files/get_metadata"); httpWebRequest.ContentType = "text/json"

新的Dropbox API文档位于:

我正在尝试执行一个简单的元数据调用,但几乎没有成功。以下是我使用的代码:

    private void go()
    {
        var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://api.dropbox.com/2-beta/files/get_metadata");
        httpWebRequest.ContentType = "text/json";
        httpWebRequest.Method = "POST";
        httpWebRequest.Headers.Add("Authorization: Bearer xxxxxxxxxxxxxxxxxxx");

        using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
        {
            string json = "{\"path\": \"/avatar_501.png\"}";

            streamWriter.Write(json);
            streamWriter.Flush();
            streamWriter.Close();
        }

        var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
        using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
        {
            var result = streamReader.ReadToEnd();
            this.TextBox1.Text = result;
        }
    }

任何帮助都将不胜感激

如果您尝试此代码,您将看到400响应的主体,它告诉您
text/json
不是有效的
内容类型。我将您的代码转换为控制台应用程序,并使用Newtonsoft.Json进行Json序列化。否则,您的代码和我的代码之间的唯一区别就是添加了异常处理以获得400的主体

class Program
{
    static void Main(string[] args)
    {
        var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://api.dropbox.com/2-beta/files/get_metadata");
        httpWebRequest.ContentType = "text/json";
        httpWebRequest.Method = "POST";
        httpWebRequest.Headers.Add("Authorization: Bearer <REDACTED>");

        using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
        {
            streamWriter.Write(JsonConvert.SerializeObject(new {
                path = "/avatar_501.png"
            }));
        }

        HttpWebResponse response;

        try
        {
            response = (HttpWebResponse)httpWebRequest.GetResponse();
        }
        catch (WebException e)
        {
            response = (HttpWebResponse)e.Response;
        }

        Console.WriteLine("Status code: {0}", (int)response.StatusCode);
        using (var streamReader = new StreamReader(response.GetResponseStream()))
        {
            Console.WriteLine(streamReader.ReadToEnd());
        }

        Console.ReadLine();
    }
}

内容类型
更改为
应用程序/json
会导致调用成功。

400响应的主体是什么?只是猜测一下,但是您的
内容类型
标题是
text/json
,而不是
application/json
。我不确定这是否允许,但错误响应的主体可能会告诉您确切的问题。我还建议您在使用API时使用web请求工具(例如)。这确实简化了测试。您可以先尝试手动发送请求,当您验证请求是否正常工作时,用代码实现它。这就解决了问题——同时,将我引向Newtonsoft肯定会有很大帮助。我对使用JSON还很陌生,而且我很确定我一直在艰难地使用JSON。非常感谢。
Status code: 400
Error in call to API function "files/get_metadata": Bad HTTP "Content-Type" header: "text/json".  Expecting one of "application/json", "application/json; charset=utf-8", "text/plain; charset=dropbox-cors-hack".