title: 401: Unauthorized Exception occurred with an apache axis client(java) to invoke a webservice(.net) with an NTLM Authentication Technique
body: <p>I am calling a web service written in .net located remotely running under IIS Server.  </p>
  
  <p>I created its respective stub using <code>apache axis 1.4</code> with eclipse IDE and created a respective web service client. This is just a test client in actual it will be my web application going to call this web service. </p>
  
  <p>We have kept its two different endpoint for keeping security credential enable/disable.  </p>
  
  <ol>
  <li>"ip:port/pigeon/pigeon.svc;" // authentication disabled</li>
  <li><p>"ip:port/pwa/pigeon.svc; // authentiction enabled</p>
  
  <p>So now when I am using endpoint no (1) I am able to call web service and gets things done, but since I want to apply security credential mandatory so when I use endpoint no (2) I am getting below exception </p></li>
  </ol>
  
  <blockquote>
    <p>(401)Unauthorized<br>
    (401)Unauthorized AxisFault<br>
    faultCode: {<a href="http://xml.apache.org/axis/" rel="nofollow">http://xml.apache.org/axis/</a>}HTTP<br>
    faultSubcode:<br>
    faultString: (401)Unauthorized<br>
    faultActor:<br>
    faultNode:<br>
    faultDetail: {}:return code:  401</p>
  </blockquote>
  
  <p>I want to pass credential which are in this format :<br>
  1) domain\username<br>
  2) password</p>
  
  <p>I tried adding suggestion of other post over here which says set the respective before call method in stub but I am getting the same above mentioned exception. </p>
  
  <blockquote>
    <p>(mystub)._setProperty(javax.xml.rpc.Stub.USERNAME_PROPERTY, domain +
    "\" + username);<br>
    (mystub)._setProperty(javax.xml.rpc.Stub.PASSWORD_PROPERTY, password);</p>
  </blockquote>
  
  <p><strong>Hhowever with some search now I am able to do NTML authentication with java stand alone program invoking my remote .net web service by doing this :</strong>   </p>
  
  <pre><code>public static void main(String[] args) throws Exception {
      String urlStr = “http://example.com/root/action.dll?p1=value1″;
      String domain = “”; // May also be referred as realm
      String userName = “CHANGE_ME”;
      String password = “CHANGE_ME”;      
  
      String responseText = getAuthenticatedResponse(urlStr, domain, userName, password);
  
      System.out.println(”response: ” + responseText);
  }
  
  private static String getAuthenticatedResponse(final String urlStr, final String domain, final String userName, final String password) throws IOException {
  
      StringBuilder response = new StringBuilder();
  
      Authenticator.setDefault(new Authenticator() {
          @Override
          public PasswordAuthentication getPasswordAuthentication() {
              return new PasswordAuthentication(domain + “\\” + userName, password.toCharArray());
          }
      });
  
      URL urlRequest = new URL(urlStr);
      HttpURLConnection conn = (HttpURLConnection) urlRequest.openConnection();
      conn.setDoOutput(true);
      conn.setDoInput(true);
      conn.setRequestMethod(”GET”);
  
      InputStream stream = conn.getInputStream();
      BufferedReader in = new BufferedReader(new InputStreamReader(stream));
      String str = “”;
      while ((str = in.readLine()) != null) {
          response.append(str);
      }
      in.close();     
  
      return response.toString();
  }
  </code></pre>
  
  <p>But I am not able to do so with my axis client as stub are generated with wsdl provided by .net web service in my web service client. I tried changing @stub level before invoke() call by modifying according to above demo but it throws same unauthorized exception.  </p>
  
  <p>This is just fyi/all that remote IIS server using NTLM authentication technique.</p>
  
  <p>Help expected on windows authentication using java in order to pass security credential to IIS.  </p>
  
  <p><strong>[Note : my axis client(java) passes domain\user with password ,this is configured with IIS server on the otherside properly]</strong></p>