title: Spring WebService - Unable to call service that requires proxy with username and password
body: <p>I am trying to use the Spring WebServiceTemplate in my Java code to call a proteceted webservice offered by a vendor</p>
  
  <p>here is the spring config for my webserviceTemplate</p>
  
  <pre><code>    &lt;bean id="messageFactory" class="org.springframework.ws.soap.saaj.SaajSoapMessageFactory"/&gt;
  
      &lt;bean id="webServiceTemplate" class="org.springframework.ws.client.core.WebServiceTemplate"&gt;
          &lt;constructor-arg ref="messageFactory"/&gt;
          &lt;property name="messageSender"&gt;
              &lt;bean class="org.springframework.ws.transport.http.HttpComponentsMessageSender"&gt;
                   &lt;property name="httpClient" ref="httpClient"/&gt; 
                  &lt;property name="credentials" ref="credentials" /&gt;
              &lt;/bean&gt;
          &lt;/property&gt; 
      &lt;/bean&gt;
  
      &lt;bean id="credentials" class="org.apache.http.auth.UsernamePasswordCredentials"&gt;
              &lt;constructor-arg value="${wsdl.username}:${wsdl.password}"/&gt; 
  &lt;!--            &lt;constructor-arg index="0" value="${wsdl.username}"/&gt;
              &lt;constructor-arg index="1" value="${wsdl.password}"/&gt; --&gt;
       &lt;/bean&gt;
  
      &lt;bean id="proxy" class="org.apache.http.HttpHost"&gt;
          &lt;constructor-arg value="${http.proxy.host}" /&gt;
          &lt;constructor-arg value="${http.proxy.port}" /&gt;
      &lt;/bean&gt; 
  
  
  &lt;!-- See http://stackoverflow.com/questions/20340470/how-to-spring-ioc-and-httpclient-4-3-1-closeablehttpclient
  for configuring the HttpRequestBuilder in spring --&gt;
  &lt;bean id="requestConfigBuilder" class="org.apache.http.client.config.RequestConfig" factory-method="custom"&gt;
  &lt;property name="proxy" ref="proxy" /&gt; 
      &lt;property name="socketTimeout" value="50000" /&gt; 
      &lt;property name="connectTimeout" value="50000" /&gt;  
  &lt;/bean&gt;
  
  &lt;bean id="requestConfig" factory-bean="requestConfigBuilder" factory-method="build" /&gt;
  
  &lt;bean id="httpClientBuilder" class="org.apache.http.impl.client.HttpClientBuilder" factory-method="create"&gt;
      &lt;property name="defaultRequestConfig" ref="requestConfig" /&gt;
  &lt;/bean&gt;
  
  &lt;bean id="httpClient" factory-bean="httpClientBuilder" factory-method="build" /&gt;    
  </code></pre>
  
  <p>I need to use a proxy to get through to the service. Also the webserivces requires a username and password.
  I have verified numerous times that the proxy and credential details are valid</p>
  
  <p>The code is use to call this webservice is</p>
  
  <pre><code>public boolean getItems() {
  
      StringBuilder soapMessage = FileUtil.readFileContent(BASE_CONFIG_LOCATION + GET_ITEMS_XML_TEMPLTE);
      StreamSource source = new StreamSource(new StringReader(soapMessage.toString()));
      StreamResult result = new StreamResult(System.out);
  
      String endpoint = baseURL + FIND_SERVICE_ENDPOINT;
  
      boolean webserviceResult = webServiceTemplate.sendSourceAndReceiveToResult(endpoint,source, result);
  
  
      return webserviceResult;
  }
  </code></pre>
  
  <p>When  I call this code - I get the following exception</p>
  
  <pre><code> org.springframework.ws.client.WebServiceIOException: I/O error: null; nested exception is org.apache.http.client.ClientProtocolException
      at org.springframework.ws.client.core.WebServiceTemplate.sendAndReceive(WebServiceTemplate.java:543)
      at org.springframework.ws.client.core.WebServiceTemplate.doSendAndReceive(WebServiceTemplate.java:492)
      at org.springframework.ws.client.core.WebServiceTemplate.sendSourceAndReceiveToResult(WebServiceTemplate.java:436)
      at org.springframework.ws.client.core.WebServiceTemplate.sendSourceAndReceiveToResult(WebServiceTemplate.java:421)
      at com.test.api.WebServiceImpl.getChecklist(WebServiceImpl.java:69)
      at com.test.api.WebServiceRealCallsTest.testGetChecklist_Success(WebServiceRealCallsTest.java:35)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:497)
      at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
      at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
      at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
      at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
      at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
      at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:73)
      at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)
      at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:73)
      at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:224)
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:83)
      at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
      at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
      at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
      at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
      at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
      at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
      at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:68)
      at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:163)
      at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
      at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
  Caused by: org.apache.http.client.ClientProtocolException
      at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:186)
      at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
      at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:107)
      at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:55)
      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:622)
      at org.springframework.ws.client.core.WebServiceTemplate.doSendAndReceive(WebServiceTemplate.java:585)
      at org.springframework.ws.client.core.WebServiceTemplate.sendAndReceive(WebServiceTemplate.java:537)
      ... 35 more
  Caused by: org.apache.http.ProtocolException: Content-Length header already present
      at org.apache.http.protocol.RequestContent.process(RequestContent.java:96)
      at org.apache.http.protocol.ImmutableHttpProcessor.process(ImmutableHttpProcessor.java:132)
      at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:182)
      at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:88)
      at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
      at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
      ... 43 more
  </code></pre>
  
  <p>Can anyone offer any guidance on what I need to do to get this service up and running? Perhaps my configuration is not correct?</p>
  
  <p>Thank you</p>
  
  ################ UPDATE
  
  <p>If I use the following code - my webservice call returns successfully
  However I would rather do all of this in spring and also the httpClient getParams method is deprecated</p>
  
  <pre><code>    public boolean getItems() {
  
      HttpComponentsMessageSender sender = (HttpComponentsMessageSender) webServiceTemplate.getMessageSenders()[0];
           sender.getHttpClient().getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,
                                      proxy); 
  
      StringBuilder soapMessage = FileUtil.readFileContent(BASE_CONFIG_LOCATION + GET_ITEMS_XML_TEMPLTE);
      StreamSource source = new StreamSource(new StringReader(soapMessage.toString()));
      StreamResult result = new StreamResult(System.out);
  
      String endpoint = baseURL + FIND_SERVICE_ENDPOINT;
  
      boolean webserviceResult = webServiceTemplate.sendSourceAndReceiveToResult(endpoint,source, result);
  
      return webserviceResult;
  }
  </code></pre>
  
  #### Update 1pm GMT
  
  <p>I noticed that if I specify the httpClient in the xml for the message sender that the class org.apache.http.impl.client.InternalHttpClient is used</p>
  
  <p>If I dont specify the httpClient - the class org.apache.http.impl.client.DefaultHttpClient is used</p>
  
  <p>Also in another interesting point, in the class org.springframework.ws.transport.http.HttpComponentsMessageSender</p>
  
  <pre><code>/**
   * Create a new instance of the {@code HttpClientMessageSender} with a default {@link HttpClient} that uses a
   * default {@link org.apache.http.impl.conn.PoolingClientConnectionManager}.
   */
  public HttpComponentsMessageSender() {
      org.apache.http.impl.client.DefaultHttpClient defaultClient =
              new org.apache.http.impl.client.DefaultHttpClient(new org.apache.http.impl.conn.PoolingClientConnectionManager());
      defaultClient.addRequestInterceptor(new RemoveSoapHeadersInterceptor(), 0);
  
      this.httpClient = defaultClient;
      setConnectionTimeout(DEFAULT_CONNECTION_TIMEOUT_MILLISECONDS);
      setReadTimeout(DEFAULT_READ_TIMEOUT_MILLISECONDS);
  }
  
  /**
   * Create a new instance of the {@code HttpClientMessageSender} with the given
   * {@link HttpClient} instance.
   * &lt;p&gt;
   * This constructor does not change the given {@code HttpClient} in any way. As such,
   * it does not set timeouts, nor does it
   * {@linkplain org.apache.http.impl.client.DefaultHttpClient#addRequestInterceptor(org.apache.http.HttpRequestInterceptor) add}
   * the {@link RemoveSoapHeadersInterceptor}.
   *
   * @param httpClient the HttpClient instance to use for this sender
   */
  public HttpComponentsMessageSender(HttpClient httpClient) {
      Assert.notNull(httpClient, "httpClient must not be null");
      this.httpClient = httpClient;
  }
  </code></pre>
  
  <p>As you can see - for the DefaultClient - it calls a method to remove soapHeaders but not for the HttpClient. I looked at this method and thought about adding an intercepotor for doing the same job but the HttpRequest is not available on any of the interceptor methods</p>
  
  <p>Any thoughts on where I can proceed from here?</p>