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)只要有帮助的回复都有份,我可以另开贴,只要有帮助多少分都可以给
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)只要有帮助的回复都有份,我可以另开贴,只要有帮助多少分都可以给
// _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中可用
// 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的 应该怎么写呢?
_httpURLConnection.setRequestProperty("Content-Length", Integer.toString(lenBody));
这两句是必须的吗?
post请求体的长度这样写是否正确?
出现在这一句
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();有经验的给说一下吧