简体   繁体   English

从Java程序调用Web服务

[英]Calling an web service from java program

I am trying to call an web service by sending an xml file as input and i should be receiving an xml as a reply but whenever i send the xml i get proxy authentication error so i thought i was sending the wrong xml but the same xml works fine when i use SOAP UI so i guess there is some problem with my code. 我试图通过发送一个xml文件作为输入来调用Web服务,我应该收到一个xml作为答复,但是每当我发送xml时,我都会收到代理身份验证错误,所以我认为我发送的是错误的xml,但相同的xml可以工作当我使用SOAP UI时很好,所以我猜我的代码有问题。

Here is the code below 这是下面的代码

URL url = null; URL 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);

Can anybode help me as to where i am going wrong. 有人可以帮我解决我的错吗。

If you get a proxyauthentication error, chances are that that really is the issue. 如果您收到代理身份验证错误,则很可能这就是问题所在。 Maybe soap ui is set up with the correct proxy info? 也许soap ui设置了正确的代理信息? Or gets it from the system settings? 还是从系统设置中获取? Your java code won't automatically pick up on these settings. 您的Java代码不会自动使用这些设置。 Check if you have a system proxy configured, or one in soapUI. 检查您是否配置了系统代理,或者是否在soapUI中配置了一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM