首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

利用httpURLConnection发送post请求时报错解决办法

2012-01-15 
利用httpURLConnection发送post请求时报错各位大哥,大姐们,我在后台用httpURLConnection发送post请求时报4

利用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 {
  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)

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

热点排行