title: Consuming .net WCF service from Java
body: <p>I new to Java, and thought it'd be a nice learning exercise to implement a client application on top of a .NET WCF service I'm already familiar with.</p>
  
  <p>I ran wsimport.bat from the latest JAX-WS (<a href="https://jax-ws.java.net/" rel="nofollow">https://jax-ws.java.net/</a>) to generate the client proxies, However I'm stuck with the authentication part. The WCF service uses Application level Username/Password to authenticate. I don't know where to supply the credentials.</p>
  
  <p>In C#, for this WCF Service I just need to add a service reference, and do the following:</p>
  
  <pre><code>var client = new ServiceClient();
  client.ClientCredentials.UserName.UserName = "username";
  client.ClientCredentials.UserName.Password = "password";
  client.DoSomething();
  </code></pre>
  
  <p>From Fiddler, calling client.DoSomething() produces the following request:</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"&gt;
    &lt;s:Header&gt;
      &lt;o:Security xmlns:o="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" s:mustUnderstand="1"&gt;
        &lt;u:Timestamp u:Id="_0"&gt;
          &lt;u:Created&gt;2013-05-20T01:34:28.353Z&lt;/u:Created&gt;
          &lt;u:Expires&gt;2013-05-20T01:39:28.353Z&lt;/u:Expires&gt;
        &lt;/u:Timestamp&gt;
        &lt;o:UsernameToken u:Id="uuid-da5b7b57-dbb4-4c54-b529-f5b41fc728b4-1"&gt;
          &lt;o:Username&gt;username&lt;/o:Username&gt;
          &lt;o:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText"&gt;password&lt;/o:Password&gt;
        &lt;/o:UsernameToken&gt;
      &lt;/o:Security&gt;
    &lt;/s:Header&gt;
    &lt;s:Body&gt;
      &lt;DoSomething xmlns="http://tempuri.org/"/&gt;
    &lt;/s:Body&gt;
  &lt;/s:Envelope&gt;
  </code></pre>
  
  <p>Here is the Java I'm currently using:</p>
  
  <pre><code>SomeService service = new SomeService();
  ISomeService port = service.getBasicHttpBindingISomeService();
  DoSomethingResponse response = port.getDoSomething();
  </code></pre>
  
  <p>This produces the following request:</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"&gt;
    &lt;S:Body&gt;
      &lt;ns6:DoSomething xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays" xmlns:ns6="http://tempuri.org/" xmlns:ns7="http://schemas.microsoft.com/2003/10/Serialization/"/&gt;
    &lt;/S:Body&gt;
  &lt;/S:Envelope&gt;
  </code></pre>
  
  <p>It's missing the Security node, so I'm not sure how to set that up on the Java side. What am I missing?</p>
  
  <p>I'm not 100% bound to using JAX-WS, if there is a better alternative out there for my needs. However, the WCF service can not be modified to reconfigured, as it's out of my reach.</p>
  
  <p>WSDL:</p>
  
  <pre><code>&lt;wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:tns="http://tempuri.org/" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:wsp="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsap="http://schemas.xmlsoap.org/ws/2004/08/addressing/policy" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:msc="http://schemas.microsoft.com/ws/2005/12/wsdl/contract" xmlns:wsa10="http://www.w3.org/2005/08/addressing" xmlns:wsx="http://schemas.xmlsoap.org/ws/2004/09/mex" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" name="SomeService" targetNamespace="http://tempuri.org/"&gt;
    &lt;wsp:Policy wsu:Id="BasicHttpBinding_ISomeService_policy"&gt;
      &lt;wsp:ExactlyOne&gt;
        &lt;wsp:All&gt;
          &lt;sp:TransportBinding xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy"&gt;
            &lt;wsp:Policy&gt;
              &lt;sp:TransportToken&gt;
                &lt;wsp:Policy&gt;
                  &lt;sp:HttpsToken RequireClientCertificate="false"/&gt;
                &lt;/wsp:Policy&gt;
              &lt;/sp:TransportToken&gt;
              &lt;sp:AlgorithmSuite&gt;
                &lt;wsp:Policy&gt;
                  &lt;sp:Basic256/&gt;
                &lt;/wsp:Policy&gt;
              &lt;/sp:AlgorithmSuite&gt;
              &lt;sp:Layout&gt;
                &lt;wsp:Policy&gt;
                  &lt;sp:Lax/&gt;
                &lt;/wsp:Policy&gt;
              &lt;/sp:Layout&gt;
              &lt;sp:IncludeTimestamp/&gt;
            &lt;/wsp:Policy&gt;
          &lt;/sp:TransportBinding&gt;
          &lt;sp:SignedSupportingTokens xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy"&gt;
            &lt;wsp:Policy&gt;
              &lt;sp:UsernameToken sp:IncludeToken="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy/IncludeToken/AlwaysToRecipient"&gt;
                &lt;wsp:Policy&gt;
                  &lt;sp:WssUsernameToken10/&gt;
                &lt;/wsp:Policy&gt;
              &lt;/sp:UsernameToken&gt;
            &lt;/wsp:Policy&gt;
          &lt;/sp:SignedSupportingTokens&gt;
          &lt;sp:Wss10 xmlns:sp="http://schemas.xmlsoap.org/ws/2005/07/securitypolicy"&gt;
            &lt;wsp:Policy/&gt;
          &lt;/sp:Wss10&gt;
        &lt;/wsp:All&gt;
      &lt;/wsp:ExactlyOne&gt;
    &lt;/wsp:Policy&gt;
    &lt;wsdl:types&gt;
      &lt;xsd:schema targetNamespace="http://tempuri.org/Imports"&gt;
        &lt;xsd:import schemaLocation="https://someservice.com/service.svc?xsd=xsd0" namespace="http://tempuri.org/"/&gt;
        &lt;xsd:import schemaLocation="https://someservice.com/service.svc?xsd=xsd1" namespace="http://schemas.microsoft.com/2003/10/Serialization/"/&gt;
        &lt;xsd:import schemaLocation="https://someservice.com/service.svc?xsd=xsd6" namespace="http://schemas.microsoft.com/2003/10/Serialization/Arrays"/&gt;
      &lt;/xsd:schema&gt;
    &lt;/wsdl:types&gt;
    &lt;wsdl:message name="ISomeService_DoSomething_OutputMessage"&gt;
      &lt;wsdl:part name="parameters" element="tns:DoSomethingResponse"/&gt;
    &lt;/wsdl:message&gt;
    &lt;wsdl:portType name="ISomeService"&gt;
      &lt;wsdl:operation name="DoSomething"&gt;
        &lt;wsdl:input wsaw:Action="http://tempuri.org/ISomeService/DoSomething" message="tns:ISomeService_DoSomething_InputMessage"/&gt;
        &lt;wsdl:output wsaw:Action="http://tempuri.org/ISomeService/DoSomethingResponse" message="tns:ISomeService_DoSomething_OutputMessage"/&gt;
      &lt;/wsdl:operation&gt;
    &lt;/wsdl:portType&gt;
    &lt;wsdl:binding name="BasicHttpBinding_ISomeService" type="tns:ISomeService"&gt;
      &lt;wsp:PolicyReference URI="#BasicHttpBinding_ISomeService_policy"/&gt;
      &lt;soap:binding transport="http://schemas.xmlsoap.org/soap/http"/&gt;
      &lt;wsdl:operation name="DoSomething"&gt;
        &lt;soap:operation soapAction="http://tempuri.org/ISomeService/DoSomething" style="document"/&gt;
        &lt;wsdl:input&gt;
          &lt;soap:body use="literal"/&gt;
        &lt;/wsdl:input&gt;
        &lt;wsdl:output&gt;
          &lt;soap:body use="literal"/&gt;
        &lt;/wsdl:output&gt;
      &lt;/wsdl:operation&gt;
    &lt;/wsdl:binding&gt;
    &lt;wsdl:service name="SomeService"&gt;
      &lt;wsdl:port name="BasicHttpBinding_ISomeService" binding="tns:BasicHttpBinding_ISomeService"&gt;
        &lt;soap:address location="https://someservice.com/service.svc"/&gt;
      &lt;/wsdl:port&gt;
    &lt;/wsdl:service&gt;
  &lt;/wsdl:definitions&gt;
  </code></pre>
  
  <p>I'm using the latest JDK on Windows 8 x64</p>