利用httpURLConnection发送post请求时报错
各位大哥,大姐们,
我在后台用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 {
readContentFromPo