我有一个方法应该接受序列化的json并将其发布到带有元数据的Google Drive Rest API V2,以命名该文件并返回HttpWebResponse对象 . 但是我得到了一个未处理的异常:

System.Net.WebException:远程服务器返回错误:(400)错误请求 .

private HttpWebResponse HttpUploadRequestMaker(string json, string accessToken)
    {
        string body = "--foo_bar_baz Content-Type: application/json; charset=UTF-8; " +
            " {\"title\":\"MyFile\"} --foo_bar_baz " +
            " Content-Type: application/json; charset=UTF-8; " + json + " --foo_bar_baz--";
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart&access_token=" + accessToken);
        req.Method = "POST";
        req.ContentType = "multipart/related; boundary=foo_bar_baz;";

        using (var streamWriter = new StreamWriter(req.GetRequestStream()))
        {
            streamWriter.Write(body);
            streamWriter.Close();
        }

        return (HttpWebResponse)req.GetResponse();
    }

但是,如果我将内容类型/ MIME类型更改为“application / json”,它将生成一个带有整个正文内容的无 Headers 文件(以“--foo_bar_baz”开头,以“--foo_bar_baz--”结尾)

我一直在使用Googles示例进行分段上传:https://developers.google.com/drive/api/v2/multipart-upload

我已经看过其他有关它的问题了:Http multipart request using Google Drive API

Upload files with HTTPWebrequest (multipart/form-data)

我想也许我需要将文件作为对象传递,但我不确定如何在Xamarin的上下文中执行此操作 . 几个月前我才开始使用Visual Studio . 任何方向将不胜感激!