利用httpURLConnection发送post请求时报错

陈鋆 2011-12-13 12:02:47
各位大哥,大姐们,
我在后台用httpURLConnection发送post请求时报400,服务器无法理解请求的错误。
但是我发送时参数为空的话就没有错误,
但是我用浏览器访问时候地址,和参数都是正确的,这个是什么原因啊,急急急,
package com.smapp.geoportal.data.dao;

import java.io.*;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

public class HttpInvoker{
public static final String POST_URL = "http://126.33.8.251:7001/geonetwork/srv/cn/csw";
public static void readContentFromPost() throws IOException {
URL postUrl = new URL(POST_URL);
HttpURLConnection connection = (HttpURLConnection) postUrl
.openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
connection.connect();
DataOutputStream out = new DataOutputStream(connection
.getOutputStream());
/*
String content ="<?xml version='1.0'?>"+
"<csw:GetCapabilities xmlns:csw='http://www.opengis.net/cat/csw/2.0.2' service='CSW'>"+
"<ows:AcceptVersions xmlns:ows='http://www.opengis.net/ows'>"+
"<ows:Version>2.0.2</ows:Version>"+
"</ows:AcceptVersions>"+
"<ows:AcceptFormats xmlns:ows='http://www.opengis.net/ows'>"+
"<ows:OutputFormat>application/xml</ows:OutputFormat>"+
"</ows:AcceptFormats>"+
"</csw:GetCapabilities>";
*/
// content = URLEncoder.encode(content, "utf-8");
// String content = URLEncoder.encode("<?xml version=\"1.0\" encoding=\"UTF-8\"?><smmd:CommandArgs xmlns:smmd=\"http://data.sbsm.gov.cn/smmd/2007\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><smmd:ParentID>330000000000/330000000000410000/330000000000410100</smmd:ParentID><smmd:MetaFile>D://</smmd:MetaFile></smmd:CommandArgs>","utf-8");
//content = URLEncoder.encode(content,"utf-8");
// String content = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><smmd:CommandArgs xmlns:smmd=\"http://data.sbsm.gov.cn/smmd/2007\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><smmd:ParentID>330000000000/330000000000410000/330000000000410100</smmd:ParentID><smmd:MetaFile>D://</smmd:MetaFile></smmd:CommandArgs>";
String content = "";
System.out.println(content);
out.writeBytes(content);
out.flush();
out.close();
BufferedReader reader = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
connection.disconnect();
}
public static void main(String[] args) {
try {
readContentFromPost();
} catch (IOException e){
e.printStackTrace();
}
}

}
这个是程序代码
下面的时候不带参数时访问后台报的
<?xml version="1.0" encoding="UTF-8"?>
<ows:ExceptionReport xmlns:ows="http://www.opengis.net/ows" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0.0" xsi:schemaLocation="http://www.opengis.net/ows http://schemas.opengis.net/ows/1.0.0/owsExceptionReport.xsd">
<ows:Exception exceptionCode="MissingParameterValue" locator="request" />
</ows:ExceptionReport>

这个是带上参数报的错
<?xml version='1.0'?><csw:GetCapabilities xmlns:csw='http://www.opengis.net/cat/csw/2.0.2' service='CSW'><ows:AcceptVersions xmlns:ows='http://www.opengis.net/ows'><ows:Version>2.0.2</ows:Version></ows:AcceptVersions><ows:AcceptFormats xmlns:ows='http://www.opengis.net/ows'><ows:OutputFormat>application/xml</ows:OutputFormat></ows:AcceptFormats></csw:GetCapabilities>
java.io.IOException: Server returned HTTP response code: 400 for URL: http://126.33.8.251:7001/geonetwork/srv/cn/csw
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1441)
at com.smapp.geoportal.data.dao.HttpInvoker.readContentFromPost(HttpInvoker.java:46)
at com.smapp.geoportal.data.dao.HttpInvoker.main(HttpInvoker.java:55)
...全文
686 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
qianyecq 2011-12-14
  • 打赏
  • 举报
回复
connection.setRequestProperty("Content-Type", "text/xml; charset=GB2312");
加上这个试试。
dracularking 2011-12-14
  • 打赏
  • 举报
回复
总之是请求时总的请求数据不被服务器所解析

HttpURLConnection httpConn = (HttpURLConnection)connection;
InputStream is;
if (httpConn.getResponseCode() >= 400) {
is = httpConn.getInputStream();
} else {
is = httpConn.getErrorStream();
}

用这方法可能可以看到更具体隐藏在400下的报错信息,查看输入流is
或者用http消息查看工具(fiddler, wireshark...)看能否看到更具体信息
陈鋆 2011-12-14
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 qianyecq 的回复:]

connection.setRequestProperty("Content-Type", "text/xml; charset=GB2312");
加上这个试试。
[/Quote]

这个方法可行,谢了,哥们
陈鋆 2011-12-13
  • 打赏
  • 举报
回复
各位大侠,有空看下这个,帮忙解决下,这个问题是我在请求csw服务中所遇到的问题。急急急啊。
陈鋆 2011-12-13
  • 打赏
  • 举报
回复
各位大神么,帮忙看看了

81,122

社区成员

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

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