java post请求 error400(google docs应用)

「已注销」 2010-04-23 08:30:09
ByteArrayOutputStream buffer = null;
HttpURLConnection _httpURLConnection = null;
URL _url = null;

String resourceURL = "https://docs.google.com/feeds/default/private/full/folder%3Aroot/contents";
// 下面三个是三个不同的url
// https://docs.google.com/feeds/default/private/full/folder%3Aroot/contents
// https://docs.google.com/feeds/default/private/full
// https://docs.google.com/feeds/default/private/full/folder%3A0B10bslXoeyplZGRiNzAyNWUtYmNjNy00YTczLTlmNGQtMjA4MTVmYTBlN2E4/contents

// postData是请求体 下面两种写法哪一个对呢?还是都行?
//String postData = "<?xml version='1.0' encoding='UTF-8'?>\r\n"
// + "<entry xmlns=\"http://www.w3.org/2005/Atom\">\r\n"
// + "<category scheme=\"http://schemas.google.com/g/2005#kind\"term=\"http://schemas.google.com/docs/2007#folder\"/>\r\n"
// + "<title>lpf66fpl</title>\r\n"
// + "</entry>\r\n";

String postData = "<?xml version='1.0' encoding='UTF-8'?><entry xmlns=\"http://www.w3.org/2005/Atom\"><category scheme=\"http://schemas.google.com/g/2005#kind\"term=\"http://schemas.google.com/docs/2007#folder\"/><title>lpf66fpl</title></entry>";

int lenBody = postData.length();
System.out.println(lenBody);

_url = new URL(resourceURL);
_httpURLConnection = (HttpURLConnection)_url.openConnection();
//设置连接属性
_httpURLConnection.setDoOutput(true); //使用 URL 连接进行输出
_httpURLConnection.setDoInput(true); //使用 URL 连接进行输入
_httpURLConnection.setUseCaches(false); //忽略缓存

System.out.println(postData);
System.out.println(auth);
auth = "GoogleLogin auth=" + auth; // GoogleLogin
System.out.println(auth);

_httpURLConnection.setRequestMethod("POST");
_httpURLConnection.setRequestProperty("Host", "docs.google.com");
_httpURLConnection.setRequestProperty("GData-Version", "3.0");
_httpURLConnection.setRequestProperty("Authorization", auth);
_httpURLConnection.setRequestProperty("Content-Length", "" + Integer.toString(lenBody));
_httpURLConnection.setRequestProperty("Content-Type", "application/atom+xml");
_httpURLConnection.setRequestProperty("Expect", "100-continue");

// OutputStreamWriter output = null;
// output = new OutputStreamWriter(_httpURLConnection.getOutputStream(), "utf-8");
// int contentlen = postData.toCharArray().length; // .length();
// output.write(postData.toCharArray(), 0, contentlen);

OutputStream output = _httpURLConnection.getOutputStream();
output.write(postData.getBytes());
//output.write(postData.getBytes(), 0, lenBody);
output.flush();
output.close();

_httpURLConnection.connect();
System.out.println(_httpURLConnection.getResponseCode());

byte[] buff = new byte[1024];
int len = -1;

buffer = new ByteArrayOutputStream();
InputStream in = _httpURLConnection.getInputStream();
while((len = in.read(buff))!= -1)
{
buffer.write(buff, 0, len);
}
//buffer
String b = buffer.toString();
System.out.println(buffer.toString());

大家看一下这个程序有没有大的错误?就是哪里写得不对

下面是原文
Creating a folder
To create a folder, send an HTTP POST request with an <atom:entry> containing a category element with http://schemas.google.com/g/2005#kind scheme and http://schemas.google.com/docs/2007#folder term. The folder name will be determined by the value of the atom:title element if present. If no atom:title element is submitted, a default name will be chosen.

Here is an example of creating a folder which will be called Example Folder on Google Documents.

POST /feeds/default/private/full HTTP/1.1
Host: docs.google.com
GData-Version: 3.0
Authorization: <your authorization header here>
Content-Length: 245
Content-Type: application/atom+xml

<?xml version='1.0' encoding='UTF-8'?>
<entry xmlns="http://www.w3.org/2005/Atom">
<category scheme="http://schemas.google.com/g/2005#kind"
term="http://schemas.google.com/docs/2007#folder"/>
<title>Example Folder</title>
</entry>If the request is successful, a 201 Created response will be returned along with a <atom:entry> describing the folder on the server.

Creating a subfolder


我得到
400
java.io.IOException: Server returned HTTP response code: 400 for URL: https://docs.google.com/feeds/default/private/full
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection$6.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at sun.net.www.protocol.http.HttpURLConnection.getChainedException(Unknown Source)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
at protocol.CreateFolder(protocol.java:297)
at Documents.main(Documents.java:50)

只要有帮助的回复都有份,我可以另开贴,只要有帮助多少分都可以给
...全文
477 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
「已注销」 2010-04-25
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 gao125210 的回复:]

把\r去掉就行了
[/Quote]

正解

C++里\r\n
java里\n
gao125210 2010-04-25
  • 打赏
  • 举报
回复
把\r去掉就行了
「已注销」 2010-04-25
  • 打赏
  • 举报
回复
我又开了一个贴 大家帮顶呀
http://topic.csdn.net/u/20100425/15/1ee77fe5-a1fd-4cfe-94e2-15c43cfd4a0a.html?seed=224879460&r=64958117#r_64958117

100散分啦
「已注销」 2010-04-24
  • 打赏
  • 举报
回复
请求错误Connection reset
出现在这一句
OutputStream output = _httpURLConnection.getOutputStream();

请求错误
Server returned HTTP response code: 400 for URL: https://docs.google.com/feeds/default/private/full/folder%3Aroot/contents
出现在这一句
_httpURLConnection.connect();

有经验的给说一下吧
「已注销」 2010-04-24
  • 打赏
  • 举报
回复
_httpURLConnection.setRequestProperty("Host", "docs.google.com");
_httpURLConnection.setRequestProperty("Content-Length", Integer.toString(lenBody));
这两句是必须的吗?
post请求体的长度这样写是否正确?
  • 打赏
  • 举报
回复
Connection reset 连接被重置

Google doc 被 GFW!
wjz748305545 2010-04-23
  • 打赏
  • 举报
回复
接分ing
「已注销」 2010-04-23
  • 打赏
  • 举报
回复
// OutputStreamWriter out = null;
// out = new OutputStreamWriter(_httpURLConnection.getOutputStream(), "utf-8");
// int contentlen = postData.toCharArray().length; // .length();
// out.write(postData.toCharArray(), 0, contentlen);
//
// out.flush();
// out.close();
//int contentlen = postData.length();
OutputStream output = _httpURLConnection.getOutputStream();
output.write(postData.getBytes());
//output.write(postData.getBytes(), 0, contentlen);
output.flush();
output.close();

post的请求体用utf-8
这个请求体是 xml的 应该怎么写呢?
「已注销」 2010-04-23
  • 打赏
  • 举报
回复
// _httpURLConnection.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
// _httpURLConnection.setRequestProperty("Accept", "text/xml,application/xml,application/xhtml+xml,text/html,text/plain,image/png,*/*");
// _httpURLConnection.setRequestProperty("Accept-Language", "en-us");
// //_httpURLConnection.setRequestProperty("Keep-Alive", "false");
// _httpURLConnection.setRequestProperty("Connection","Keep-Alive");

这几句有用吗? 我之前有个登录的post请求没有用

// OutputStreamWriter out = null;
// out = new OutputStreamWriter(_httpURLConnection.getOutputStream(), "utf-8");
// int contentlen = postData.toCharArray().length; // .length();
// out.write(postData.toCharArray(), 0, contentlen);
这个请求体的写法对吗? 还是我原来那个好? 原来那个在登录post中可用

62,635

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧