title: Trying to access a WS server with Spring-WS - "Connection timed out", but the server respond when tested with SOAP UI
body: <h1>The problem</h1>
  <p>I am taking over the development of a java web service client and in order to test an evolution I have to request a method from a distant web service server (which is embedded in an Apache Axis2 running in a tomcat instance).</p>
  <p>In the first place, I requested the method through SOAP UI, using the WSDL provided by the server. It works fine.</p>
  <p>Now I try to request the method through my Java web service client, but I cannot connect to the server even though the test with SOAP UI proves that everything is fine about it.</p>
  <p>The java web service client relies on Spring-WS.</p>
  <hr />
  <h3>Calling the web service from the client</h3>
  <pre><code>// Setting the kycScoreRequest
  ...
  
  // Trying to connect and to get the kycScoreResponse 
  KycScoreResponse kycScoreResponse = (KycScoreResponse) getWebServiceTemplate().marshalSendAndReceive(kycScoreRequest);
  </code></pre>
  <h3>The resulting error</h3>
  <pre><code>[main] DEBUG com.foo.AbstractMain - org.springframework.ws.client.WebServiceIOException: I/O error: Connection timed out: connect; nested exception is java.net.ConnectException: Connection timed out: connect
      at org.springframework.ws.client.core.WebServiceTemplate.sendAndReceive(WebServiceTemplate.java:545)
      at org.springframework.ws.client.core.WebServiceTemplate.marshalSendAndReceive(WebServiceTemplate.java:386)
      at org.springframework.ws.client.core.WebServiceTemplate.marshalSendAndReceive(WebServiceTemplate.java:380)
      at org.springframework.ws.client.core.WebServiceTemplate.marshalSendAndReceive(WebServiceTemplate.java:372)
      at com.foo.MainKycScore.getReturn(MainKycScore.java:37)
      at com.foo.MainKycScore.main(MainKycScore.java:244)
  Caused by: java.net.ConnectException: Connection timed out: connect
      at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
      at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
      at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
      at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
      at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
      at java.net.PlainSocketImpl.connect(Unknown Source)
      at java.net.SocksSocketImpl.connect(Unknown Source)
      at java.net.Socket.connect(Unknown Source)
      at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:121)
      at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:180)
      at org.apache.http.impl.conn.ManagedClientConnectionImpl.open(ManagedClientConnectionImpl.java:326)
      at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:610)
      at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:445)
      at org.apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:835)
      at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83)
      at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:108)
      at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:56)
      at org.springframework.ws.transport.http.HttpComponentsConnection.onSendAfterWrite(HttpComponentsConnection.java:119)
      at org.springframework.ws.transport.AbstractWebServiceConnection.send(AbstractWebServiceConnection.java:47)
      at org.springframework.ws.client.core.WebServiceTemplate.sendRequest(WebServiceTemplate.java:624)
      at org.springframework.ws.client.core.WebServiceTemplate.doSendAndReceive(WebServiceTemplate.java:587)
      at org.springframework.ws.client.core.WebServiceTemplate.sendAndReceive(WebServiceTemplate.java:539)
      ... 5 more
  </code></pre>
  <hr />
  <h1>Hypothesis 1 : Do I use the right web service URL ?</h1>
  <p>I'm kinda sure Spring is given the right URL, because it is the same as before I took over the code and nothing changed about the wsdl nor the server (<strong>and our client never complained, it was working for him, but never on our machines at work</strong>. So don't ask me how the developer before me tested it although it was not working, because idk) :</p>
  <pre><code>WebServiceGatewaySupport.setDefaultUri(&quot;http://82.36.138.182:8080/axis2/services/KYCService01.KYCService01HttpSoap11Endpoint/&quot;)
  </code></pre>
  <p>I have the following in the WSDL :</p>
  <pre><code>&lt;wsdl:service name=&quot;KYCService01&quot;&gt;
      &lt;wsdl:port name=&quot;KYCService01HttpSoap11Endpoint&quot; binding=&quot;ns:KYCService01Soap11Binding&quot;&gt;
          &lt;soap:address location=&quot;http://82.36.138.182:8080/axis2/services/KYCService01.KYCService01HttpSoap11Endpoint/&quot;/&gt;
      &lt;/wsdl:port&gt;
  &lt;/wsdl:service&gt;
  </code></pre>
  <p>I also tried the EPR instead as the url, but it doens't change anything :</p>
  <pre><code>WebServiceGatewaySupport.setDefaultUri(&quot;http://82.36.138.182:8080/axis2/services/KYCService01&quot;)
  </code></pre>
  <hr />
  <h1>Hypothesis 2 : Is it the proxy I'm behind ?</h1>
  <p>Then, I thought that the error must have been caused because of the proxy which I'm behind. Indeed, when I tried to write a little piece of code to connect to the wsdl url, at first I couldn't and got a &quot;connection timed out&quot;. To resolve this I set the proxy as java System properties, allowing me to now access the wsdl from the web service client.</p>
  <h3>Setting the proxy</h3>
  <pre><code>System.setProperty(&quot;http.proxyHost&quot;, &quot;&lt;my_proxy_hostname&gt;&quot;);
  System.setProperty(&quot;http.proxyPort&quot;, &quot;&lt;my_proxy_port&gt;&quot;);
  </code></pre>
  <h3>Connecting to the WSDL URL</h3>
  <pre><code>URL wsdlAddress = null;
  HttpURLConnection connection = null;
  
  wsdlAdress = new URL(&quot;&lt;wsdl_url&gt;&quot;);
  connection = (HttpURLConnection) wsdlAdress.openConnection();
  connection.connect(); // It works if the proxy is set as System properties
  </code></pre>
  <p>Unfortunately, when I try to call the web service server with my proxy set, I still have the exact same error as mentioned above. Now I am already out of idea. <strong>How is it possible that my Java client can access the web service WSDL but not its methods ?</strong></p>
  <hr />
  <h1>(edit) Hypothesis 3 : Is it the http header ?</h1>
  <p>I intercepted the HTTP msg from SOAP UI and the WS client thanks to membrane interceptor. See the headers :</p>
  <p>SOAP UI</p>
  <pre><code>POST http://82.36.138.182:8080/axis2/services/KYCService01.KYCService01HttpSoap11Endpoint/ HTTP/1.1
  Accept-Encoding: gzip,deflate
  Content-Type: text/xml;charset=UTF-8
  SOAPAction: &quot;urn:kycScore&quot;
  Content-Length: 1301
  Host: 84.37.138.182:8080
  Proxy-Connection: Keep-Alive
  User-Agent: Apache-HttpClient/4.1.1 (java 1.5)
  </code></pre>
  <p>WS client (the destination URL is a SOAP UI mock server, because obviously if I use the URL I really try to connect to there is nothing sent and thus nothing to intercept)</p>
  <pre><code>POST /mock HTTP/1.1
  Accept-Encoding: gzip
  Accept: text/xml, text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2
  SOAPAction: &quot;&quot;
  Content-Type: text/xml; charset=utf-8
  Content-Length: 1032
  Host: localhost:8088
  Connection: Keep-Alive
  User-Agent: Apache-HttpClient/4.5.3 (Java/1.8.0_101)
  </code></pre>
  <p>I don't really know anything about http headers, but the main difference seems to be SOAPAction. I set the SOAPAction attribute of my ws client message to the same value as SOAP UI, but it didn't change anything.</p>
  <pre><code>public Object marshalWithSoapActionHeader(KycScore o) {
  
          return getWebServiceTemplate().marshalSendAndReceive(o, new WebServiceMessageCallback() {
  
              public void doWithMessage(WebServiceMessage message) {
                  ((SoapMessage) message).setSoapAction(&quot;urn:kycScore&quot;);
              }
          });
      }
  </code></pre>
  <p>Otherwise, is it possible that the java version we can see in the User-Agent attribute is important ? (edit) I don't think so because I tried to use a 1.6 jre and the version does change in the User-Agent attribute but I still have the same error.</p>