此代码有效。它创建一个文档类型条目。我只将变量的内容:body(term='http://schemas.google.com/docs/2007#document' 更改为 term='http://schemas.google.com/docs/2007#file')。发送请求后,服务器返回响应<400 Bad Request>。这很奇怪。我跟踪了 Google Data API 的源代码,并找到了那种(http://schemas.google.com/docs/2007#file)。如何修改代码以创建文件类型条目。谢谢。
HttpWebRequest call = null;
HttpWebResponse echo = null;
StreamWriter send = null;
StreamReader read = null;
string data = string.Empty;
string body = string.Empty;
body = "<?xml version='1.0' encoding='UTF-8'?>
<entry xmlns='http://www.w3.org/2005/Atom'
xmlns:docs='http://schemas.google.com/docs/2007'>
<category scheme='http://schemas.google.com/g/2005#kind'
term='http://schemas.google.com/docs/2007#document'/>
<title>test.txt</title>
</entry>";
call = HttpWebRequest.Create("https://docs.google.com/feeds/default/private/full") as HttpWebRequest;
call.Method = "POST";
call.Headers.Add("GData-Version: 3.0");
call.Headers.Add("Authorization: Bearer " + Access_Token);
call.ContentType = "application/atom+xml";
call.ContentLength = Encoding.UTF8.GetBytes(body).Length;
call.Headers.Add("X-Upload-Content-Length: 0");
send = new StreamWriter(call.GetRequestStream());
send.Write(body);
send.Close();
send.Dispose();
echo = call.GetResponse() as HttpWebResponse;
if (echo.StatusCode == HttpStatusCode.Created)
{
read = new StreamReader(echo.GetResponseStream());
data = read.ReadToEnd();
MessageBox.Show("entry.xml:" + Enviroment.NewLine + data);
read.Close();
read.Dispose();
}
else
{
MessageBox.Show("entry not created. " + echo.StatusCode.ToString() + "(" + echo.StatusDescription + ")");
}
echo.Close();
然后,我想创建一个文件类型条目。我修改了代码。
body = "<?xml version='1.0' encoding='UTF-8'?>
<entry xmlns='http://www.w3.org/2005/Atom'
xmlns:docs='http://schemas.google.com/docs/2007'>
<category scheme='http://schemas.google.com/g/2005#kind'
term='http://schemas.google.com/docs/2007#file'/>
<title>test.exe</title>
</entry>";
call = HttpWebRequest.Create("https://docs.google.com/feeds/default/private/full?convert=false") as HttpWebRequest;
call.Method = "POST";
call.Headers.Add("GData-Version: 3.0");
call.Headers.Add("Authorization: Bearer " + Access_Token);
call.ContentType = "application/atom+xml";
call.ContentLength = Encoding.UTF8.GetBytes(body).Length;
call.Headers.Add("X-Upload-Content-Length: 0");
send = new StreamWriter(call.GetRequestStream());
send.Write(body);
send.Close();
send.Dispose();
echo = call.GetResponse() as HttpWebResponse;
返回 400 错误请求
body = "<?xml version='1.0' encoding='UTF-8'?>
<entry xmlns='http://www.w3.org/2005/Atom'
xmlns:docs='http://schemas.google.com/docs/2007'>
<category scheme='http://schemas.google.com/g/2005#kind'
term='http://schemas.google.com/docs/2007#document'/>
<title>test.exe</title>
</entry>";
call = HttpWebRequest.Create("https://docs.google.com/feeds/default/private/full?convert=false") as HttpWebRequest;
call.Method = "POST";
call.Headers.Add("GData-Version: 3.0");
call.Headers.Add("Authorization: Bearer " + Access_Token);
call.ContentType = "application/atom+xml";
call.ContentLength = Encoding.UTF8.GetBytes(body).Length;
call.Headers.Add("X-Upload-Content-Length: 0");
send = new StreamWriter(call.GetRequestStream());
send.Write(body);
send.Close();
send.Dispose();
echo = call.GetResponse() as HttpWebResponse;
返回 403 禁止