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)

只要有帮助的回复都有份,我可以另开贴,只要有帮助多少分都可以给
...全文
465 9 打赏 收藏 转发到动态 举报
写回复
用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中可用
程声明:该课程是教学使用,视频内涉及漏洞利用方法,请勿在互联网环境中使用;维护互联网安全,人人有责。实验所需环境:vmware;kali虚拟机一台;windows server一台;有docker环境的Linux虚拟机环境下载地址在购买课程后单独发送 【课程配套资源】1、Python脚本(Margin老师自研,不光能学漏洞,还能学Python,实在是划算)2、与Margin老师实时互动3、免费的CISP-PTE考试技巧指导(Margin老师与CISP-PTE的负责人很熟的,非常多的一手消息^o^)4、Margin老师的内部直播可以优先参加5、Margin老师的课程基于CISP-PTE的知识体系进一步扩展,使课程内容更贴近实战   【课程主要解决问题】1、CSRF、SSRF搞不清楚?2、SSRF原理是什么?危害大小?如何利用SSRF获取主机权限?如果使用Python提高挖洞效率?3、Gopher协议、Dict协议?完全没听过啊,没关系,看完课程后你门清。4、SSRF渗透Redis数据库,Redis客户端和服务器端怎么通信?通信报文是怎么样的?看这里就行。5、SSRF渗透Struts2总是失败?不知道如何编码?不知道如何使用Gopher协议?来这里。6、SSRF表面简单,实则有无数坑,通过视频提高学习效率吧。 【CISP-PTE介绍】1、CISP-PTE是进入网络安全行业的TOP1认证,能帮你梳理完整的网络安全知识体系2、有PTE证书在网络安全公司是免技术笔试的,怎么样?是不是很棒。3、Margin老师的课程基于CISP-PTE的知识体系进一步扩展,使课程内容更贴近实战本课程属于CISP-PTE渗透测试工程师认证体系的课程,但内容更加丰富。CISP-PTE是国内第一个以动手实操为主的网络安全认证,该注册考试是为了锻炼考生世界解决网络安全问题的能力,持续增强我国的网络安全水平和防御能力,促进国内网络防御能力的不断提高。考试内容从多个层面进行,考点和网络安全动态相结合,真实的反应出真实的网络环境中发现的各种问题。如果要考取CISP-PTE证书需要掌握以下内容:1、Web安全基础,注入漏洞、上传漏洞、跨站脚本漏洞、访问控制漏洞、会话管理漏洞哦等。2、中间件的安全知识,如:Apache,IIS,Tomcat,以及 JAVA 开发的中间件 Weblogic,Jboss, Websphere 等,且要了解中间件加固方法,在攻与防的能力上不断提升。3、操作系统安全,包含Windows和Linux操作系统,从账户管理、文件系统权限、日志审计等方面讲解,了解常见的漏洞方式和加固方法。4、数据库安全,包含MSSQL、MYSQL、ORACLE、REDIS数据,了解常用的数据库漏洞和题全方法,保证数据库的安全性。 【关于Margin老师】· Margin/教育系统网络安全保障人员认证首批讲师/高级讲师· 擅长CTF/Web安全/渗透测试 /系统安全· 3年研发/擅长Java/Python/某银行现金循环机业务系统开发者· 曾参与开发网络安全认证教材· 知乎专栏/CISP-PTE渗透测试工程师学习· 4年线下网络安全讲师/2000+线下学员/100000+线上学员

62,614

社区成员

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

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