【发布时间】:2015-06-02 08:36:45
【问题描述】:
我正在尝试使用 C# 中的 Picasa Web Album Data API v2 上传带有元数据的图像,如文档 here 中所述。
http post 请求失败并出现 400 Bad Request 错误,没有详细说明我的请求丢失了什么或格式错误。
以下是我的代码:
byte[] image = System.IO.File.ReadAllBytes("fullimagepath");
int imglenth= image.Length;
string text = Encoding.ASCII.GetString(image);
string rawImgXml="<entry xmlns=\'http://www.w3.org/2005/Atom\'>"+"\n"+
"<title>plz-to-love-realcat.png</title>"+"\n"+
"<summary>Real cat wants attention too</summary>"+"\n"+
"<category scheme=\"http://schemas.google.com/g/2005#kind\"\n"+
"term=\"http://schemas.google.com/photos/2007#photo\"/>"+"\n"+
"</entry>";
string data = "";
data ="\nMedia multipart posting\n"+
"--END_OF_PART\n"+
"Content-Type: application/atom+xml\n\n"+
rawImgXml + "\n"
+"--END_OF_PART\n"
+"Content-Type: image/png\n"+
text+"\n"+
"--END_OF_PART--";
int length=data.Length;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://picasaweb.google.com/data/feed/api/user/"+tc.userId+"/albumid/"+"6150508893827643953"+"?access_token="+tc.auth_Token);
byte[] bytes;
bytes = System.Text.Encoding.ASCII.GetBytes(data);
request.ContentType = "multipart/related; boundary=END_OF_PART";
request.ContentLength = length;
request.Headers.Add("GData-Version","2");
request.Headers.Add("MIME-Version","1.0");
request.Method = "POST";
Stream requestStream = request.GetRequestStream();
int start = 0;
int chunkSize = 1024 * 4; // changed form 100 to 4
double percentage = 0;
while (start <= bytes.Length)
{
if (start + chunkSize > bytes.Length)
requestStream.Write(bytes, start, bytes.Length - start);
else
requestStream.Write(bytes, start, chunkSize);
start += chunkSize;
}
HttpWebResponse response;
response = (HttpWebResponse)request.GetResponse();
Stream responseStream = response.GetResponseStream();
string responseStr = new StreamReader(responseStream).ReadToEnd();
正在将图像数据转换为 ASCII,然后附加到数据字符串中。在过去 3 天尝试了许多排列和组合后,它以同样的错误失败。完全无法弄清楚请求哪里出错了?
上传没有元数据的图像效果很好,但我也想在我的应用程序中上传视频,这在没有元数据的情况下无法完成。
任何方向或指针都会有很大帮助!提前致谢!
【问题讨论】:
标签: c# picasa google-oauth