HttpURLConnection始终返回错误500

时间:2014-10-30 09:34:26

标签: java http

我试图在基本的http请求中运行soap请求...当然我尝试使用外部工具消息并且是正确的,就像我用作targetUrl的端点一样,wsdl在

之类的东西
http://00.00.00.00/a-ws/services/basic?wsdl

我的实际终点是

http://00.00.00.00/a-ws/services/basic.targetservice

并且我最后使用此目标网址

          URL url = new URL(targetUrl);
          connection = (HttpURLConnection)url.openConnection();
          connection.setRequestMethod("POST");
          connection.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
          connection.setRequestProperty("SOAPAction", action);
          connection.setRequestProperty("User-Agent", "myagent");
          connection.setRequestProperty("Host", "localhost");
          //connection.setRequestProperty("Content-Length", "" + Integer.toString(message.getBytes().length));

          connection.setUseCaches (false);
          connection.setDoInput(true);
          connection.setDoOutput(true);

          //Send request
          OutputStream wr = connection.getOutputStream ();
          wr.write (message.getBytes());
          wr.flush ();
          wr.close ();

          //Get Response    
          InputStream is = connection.getInputStream();
          BufferedReader rd = new BufferedReader(new InputStreamReader(is));
          String line=null;
          StringBuffer response = new StringBuffer(); 
          while( (line = rd.readLine()) != null) {
              if (line!=null)
                  response.append(line);
          }
          rd.close();
          return response.toString();

使用chrome插件测试原始消息,我唯一能测试的是标题,但结果始终是getInputStream的异常

java.io.IOException: Server returned HTTP response code: 500 for URL:

为什么?

1 个答案:

答案 0 :(得分:1)

这是一个非常愚蠢的编码问题(就像我想的那样)......我没有在信息中避免双重引用。

使用假的http服务器只能回显内容,可以看到问题的证据。

更新: 没有人指出的另一件事是,在检索

的异常情况下这是有用的

connection.getErrorStream()

包含错误时的响应!