从java程序调用Web服务

时间:2015-01-01 07:10:47

标签: java xml web-services soap

我试图通过发送xml文件作为输入来调用Web服务,我应该接收xml作为回复但是每当我发送xml我得到代理身份验证错误所以我以为我发送错误的xml但是当我使用SOAP UI时,同样的xml工作正常,所以我想我的代码存在一些问题。

以下是代码

网址url = null;

   String strUrl="<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope\" xmlns:soap=\"SoapAuthenticator\" xmlns:ship=\"http://ws.consignorsupport.no/ShipAdvisor\"><soapenv:Header> <soap:ServiceAuthenticationHeader><soap:Username>TDC43671</soap:Username> <soap:Password>hTiNMft/KaMfDDD</soap:Password><soap:IsEncrypted>false</soap:IsEncrypted></soap:ServiceAuthenticationHeader></soapenv:Header><soapenv:Body><ship:SearchForDropPoints><ship:productConceptID>92</ship:productConceptID><ship:installationID>00000000018</ship:installationID><ship:country>DK</ship:country><ship:address></ship:address><ship:postCode>6000</ship:postCode><ship:city></ship:city><ship:limit>5</ship:limit></ship:SearchForDropPoints></soapenv:Body></soapenv:Envelope>";

String ss="http://www.consignorsupport.no/ShipAdvisor/Main.asmx";

url = new URL(ss);

HttpURLConnection conn = (HttpURLConnection) url.openConnection();

conn.setDoOutput(true);
 conn.setDoInput(true);
conn.setRequestProperty("Content-type", "text/xml");
  conn.setRequestProperty("Accept", "text/xml, application/xml");
  conn.setRequestMethod("POST");

conn.connect();

OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());

   wr.write(strUrl);
   wr.flush();
   wr.close();


   int iHttpResponseCode = conn.getResponseCode();
  String  strErrorMessage = conn.getResponseMessage();

  System.out.println("Getting Response status");
  System.out.println(iHttpResponseCode);

  System.out.println(strErrorMessage);

Anybode可以帮助我解决我出错的地方。

1 个答案:

答案 0 :(得分:0)

如果您收到代理身份验证错误,则可能确实存在问题。也许soap ui设置了正确的代理信息?或者从系统设置中获取它?您的Java代码不会自动获取这些设置。检查您是否配置了系统代理,或者是否使用soapUI。