title: Java send soap request with basic authentication
body: <p>We are using following code to send soap request.</p>
  <pre><code>Authenticator.setDefault(new MyAuthenticator(&quot;userId&quot;, &quot;authenticatorkey&quot;));
  SOAPConnectionFactory scf = SOAPConnectionFactory.newInstance();
  SOAPConnection conn = scf.createConnection();
  URL urlval = new URL(null,
                          serverURL,
                          new URLStreamHandler() {
                              @Override
                              protected URLConnection openConnection(URL url) throws IOException {
                                  URL target = new URL(url.toString());
                                  URLConnection connection = target.openConnection();
  
                                  // Connection timeout settings
                                  int connectTimeout = 15000;
                                  connection.setConnectTimeout(connectTimeout);
  
                                  int readTimeout = 15000;
                                  connection.setReadTimeout(readTimeout);
                                  return (connection);
  SOAPMessage rp = conn.call(soapMsg, urlval);
  </code></pre>
  <p>This way we are able to send the request, but success rate is 70% only, rest of the time we receive following error:</p>
  <pre><code>com.sun.xml.messaging.saaj.SOAPExceptionImpl: java.security.PrivilegedActionException: com.sun.xml.messaging.saaj.SOAPExceptionImpl: Bad response: (401null
  at com.sun.xml.messaging.saaj.client.p2p.HttpSOAPConnection.call(HttpSOAPConnection.java:168) ~[saaj-impl-1.3.3.jar:1.3.3]
  Caused by: java.security.PrivilegedActionException
  at java.security.AccessController.doPrivileged(Native Method) ~[?:1.8.0_91]
  at com.sun.xml.messaging.saaj.client.p2p.HttpSOAPConnection.call(HttpSOAPConnection.java:162) ~[saaj-impl-1.3.3.jar:1.3.3]
  ... 9 more
  Caused by: com.sun.xml.messaging.saaj.SOAPExceptionImpl: Bad response: (401null
      at com.sun.xml.messaging.saaj.client.p2p.HttpSOAPConnection.post(HttpSOAPConnection.java:345) ~[saaj-impl-1.3.3.jar:1.3.3]
      at com.sun.xml.messaging.saaj.client.p2p.HttpSOAPConnection$PriviledgedPost.run(HttpSOAPConnection.java:191) ~[saaj-impl-1.3.3.jar:1.3.3]
      at java.security.AccessController.doPrivileged(Native Method) ~[?:1.8.0_91]
      at com.sun.xml.messaging.saaj.client.p2p.HttpSOAPConnection.call(HttpSOAPConnection.java:162) ~[saaj-impl-1.3.3.jar:1.3.3]
  </code></pre>
  <p>On some places it has written that authenticator caches the username and password, that's why such exception comes, however this code was working perfectly fine 3 months ago. After migrating the code to another class caused this issue.</p>
  <p>Please help.</p>