各位大哥,大姐们,
我在后台用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)

解决方案 »

  1.   

    各位大侠,有空看下这个,帮忙解决下,这个问题是我在请求csw服务中所遇到的问题。急急急啊。
      

  2.   

    总之是请求时总的请求数据不被服务器所解析HttpURLConnection httpConn = (HttpURLConnection)connection;
    InputStream is;
    if (httpConn.getResponseCode() >= 400) {
        is = httpConn.getInputStream();
    } else {
        is = httpConn.getErrorStream();
    }用这方法可能可以看到更具体隐藏在400下的报错信息,查看输入流is
    或者用http消息查看工具(fiddler, wireshark...)看能否看到更具体信息
      

  3.   

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