手动命中Java中的SOAP服务,获取IO.FileNotFound异常

时间:2013-02-02 04:54:43

标签: java soap filenotfoundexception manual

我需要手动访问.Net SOAP服务。所有导入器都有其WSDL问题,因此我只是手动创建XML消息,使用HttpURLConnection进行连接,然后解析结果。我已经将Http / SOAP调用包装到一个函数中,该函数应该将结果作为字符串返回。这就是我所拥有的:

  //passed in values: urlAddress, soapAction, soapDocument

  URL u = new URL(urlAddress);
  URLConnection uc = u.openConnection();
  HttpURLConnection connection = (HttpURLConnection) uc;

  connection.setDoOutput(true);
  connection.setDoInput(true);
  connection.setRequestMethod("POST");
  connection.setRequestProperty("SOAPAction", soapAction);
  connection.setRequestProperty("User-Agent","Mozilla/5.0 ( compatible ) ");
  connection.setRequestProperty("Accept","[star]/[star]");      
  connection.setRequestProperty("Content-Type", "text/xml; charset=utf-8");

  OutputStream out = connection.getOutputStream();
  Writer wout = new OutputStreamWriter(out);

  //helper function that gets a string from a dom Document
  String xmldata = XmlUtils.GetDocumentXml(soapDocument);
  wout.write(xmldata);        
  wout.flush();
  wout.close();

  // Response
  int responseCode = connection.getResponseCode();
  BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  String responseString = "";
  String outputString = "";
  //Write the SOAP message response to a String.
  while ((responseString = rd.readLine()) != null) {
      outputString = outputString + responseString;
  }

  return outputString;        

我的问题是在BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));行上我得到了一个带有我正在使用的地址的“java.io.FileNotFoundException”(即urlAddress)。如果我将该地址粘贴到浏览器中,它会很好地提取肥皂服务网页(地址为http://protectpaytest.propay.com/API/SPS.svc)。根据我的阅读,FileNotFoundException是HttpURLConnection返回400+错误消息。我添加了行getResponseCode()只是为了看看确切的代码是什么,它是404.我从其他一些页面添加了User-Agent和Accept标题,说它们是必需的,但我仍然得到404.

我还缺少其他标题吗?我需要做些什么来使这个调用工作(因为它在浏览器中工作)?

-shnar

1 个答案:

答案 0 :(得分:0)

当我尝试使用wsdl broser时似乎工作正常: http://wsdlbrowser.com/soapclient?wsdl_url=http%3A%2F%2Fprotectpaytest.propay.com%2FAPI%2FSPS.svc%3Fwsdl

也许您需要将?wsdl添加到服务URL?