当我尝试使用 Web 应用程序将文件上传到 google Drive api 时,出现错误,并且该文件包含带重音的字符,例如“ç”。
我使用两个请求上传文件,一个初始请求包含文件信息并将 uri 返回到文件应上传的位置,后续请求包含文件本身。
第一次请求时出现错误(带有文件信息的那个)。
返回的错误只是一个“400 - bad request”,我不知道如何上传带有这种字符的文件。
以下是当我尝试上传 gif 图像文件时我的应用程序生成的请求示例。
Content-Type: application/json; charset=UTF-8
X-Upload-Content-Length: 257
Authorization: Bearer ******
X-Upload-Content-Type: image/gif
Host: www.googleapis.com
Content-Length: 126
Expect: 100-continue
请求正文仅包含一个带有文件数据的 json 对象,以下是配置此初始请求的代码示例:
private WebRequest CreateInitialUploadRequest(String parentId, String title, String mimeType)
{
String uri = "https://www.googleapis.com/upload/drive/v2/files?uploadType=resumable&access_token=" + AUTH_KEY;
System.Net.WebRequest request = this.WebRequestFactory.CreateWebRequest(uri);
request.Method = "POST";
request.ContentType = "application/json; charset=UTF-8";
request.Headers.Add("X-Upload-Content-Length", length.ToString());
request.Headers.Add("Authorization", string.Format("Bearer {0}", AUTH_KEY));
request.Headers.Add("X-Upload-Content-Type", mimeType);
//Setup parent id folder for file
object[] parents = {new {id = parentId}};
object jsonObject = new
{
title = title,
mimeType = mimeType,
description = "foo bar",
parents = parents
};
String strData = new JavaScriptSerializer().Serialize(jsonObject);
byte[] data = Encoding.UTF8.GetBytes(strData);
request.ContentLength = data.Length;
var stream = request.GetRequestStream();
stream.Write(data, 0, data.Length);
stream.Close();
return request;
}
当我尝试像这样调用该方法时:
WebRequest initialRequest = CreateInitialUploadRequest("valid-parent-folder", "invalid file ç", "image/gif");
//I get an 'bad request' http error code 400 in the following line:
initialRequest.GetResponse();
我应该以某种方式对请求进行编码吗?