title: consuming webservice using https protocol
body: <p>I'm developing an application where i have to consume webservice developed in Java using http protocol.
  I'm developing the application using C#.NET winforms. Everything works fine until now. The webservice is now using SSL security hence the service protocol changed from http to https. I'm facing issues while accessing the https webservice.</p>
  
  <p>I tried accessing the https webservice from the SoapUI by providing the Authenticaion Parameters (UserName and Password) from the Auth tab as shown below:</p>
  
  <p><img src="https://i.stack.imgur.com/MW4BB.png" alt="enter image description here"></p>
  
  <p>It is working fine from SoapUI.</p>
  
  <p>but wen i provide the Authenticaion parameters from code as below its not working:</p>
  
  <pre><code>client.ClientCredentials.UserName.UserName = "admin";
  client.ClientCredentials.UserName.Password = "*******";
  </code></pre>
  
  <p>I'm using Security Mode as : <code>TransportWithMessageCredential</code>
   and
  <code>ClientCredentialTtype</code> as : <code>Basic</code></p>
  
  <p>My <code>App.Config</code> file is as below:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;configuration&gt;
      &lt;startup&gt; 
          &lt;supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /&gt;
      &lt;/startup&gt;
    &lt;system.serviceModel&gt;
      &lt;client&gt;
        &lt;endpoint address="https://xyz:8001/HelloWorldAPI/HelloWorldWebService"
         binding="wsHttpBinding"
          bindingConfiguration="myhttpsbinding" contract="API.HelloWorldWebService"
          name="HelloWorldWebServicePort" /&gt;
      &lt;/client&gt;
      &lt;bindings&gt;
        &lt;wsHttpBinding&gt;
          &lt;binding name="myhttpsbinding"&gt;
            &lt;security mode="TransportWithMessageCredential"&gt;
              &lt;transport clientCredentialType="Basic"/&gt;
            &lt;/security&gt;
          &lt;/binding&gt;
        &lt;/wsHttpBinding&gt;
      &lt;/bindings&gt;
    &lt;/system.serviceModel&gt;
    &lt;system.net&gt;
      &lt;defaultProxy useDefaultCredentials="true" /&gt;
    &lt;/system.net&gt;
  &lt;/configuration&gt;
  </code></pre>
  
  <p>My Code as below:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Net;
  using System.Net.Security;
  using System.Security.Cryptography.X509Certificates;
  using System.ServiceModel;
  using System.Text;
  using System.Threading.Tasks;
  using testingtool.API;
  
  namespace testingtool
  {
      class Program
      {
          static void Main(string[] args)
          {
              new APITool();
          }
      }
      class APITool
      {
          UserInfo userinfo = new UserInfo();
          HelloWorldWebServiceClient client = new HelloWorldWebServiceClient();
  
          private bool ValidationCallBack(object sender, X509Certificate cert, X509Chain chain, System.Net.Security.SslPolicyErrors error)
          {
              return true;
          }
  
          public APITool()
          {
              try
              {
                  //Authentication parameters
                  client.ClientCredentials.UserName.UserName = "admin";
                  client.ClientCredentials.UserName.Password = "*****";
                  ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(ValidationCallBack);
  
                  //client ClientCredentials @ Application level
                  userinfo.userid = "myusername";
                  userinfo.password = "*****";
                  GetHelloWorldAPIVersionRequest request = new GetHelloWorldAPIVersionRequest();
  
                  APIVersionInfo versioninfo = new APIVersionInfo();
                  versioninfo.userinfo = userinfo;
                  request.APIVersionInfo = versioninfo;
  
  
                  APIVersionInfoResponse response = client.GetHelloWorldAPIVersion(versioninfo);
                  Console.WriteLine(response.Version);
              }
              catch (Exception ex)
              {
                  Console.WriteLine(ex.ToString());
              }
              Console.ReadLine();
          }
       }
    } 
  </code></pre>
  
  <p>I'm getting following exception:</p>
  
  <blockquote>
    <p>System.ServiceModel.Security.MessageSecurityException: The HTTP
    request is unauthorized with client authentication scheme 'Anonymous'.
    The authentication header received from the server was 'Basic
    realm="EJBServiceEndpointServlet Realm"'. --->
    System.Net.WebException: The remote server returned an error: (401)
    Unauthorized.</p>
  </blockquote>
  
  <p><strong>EDIT:</strong> from the client i have verified with Fiddler the request window as below:
  from the AUth tab it is saying that there is no Autherization Header present.</p>
  
  <p><img src="https://i.stack.imgur.com/LnUBi.png" alt="enter image description here"></p>
  
  <p>Fiddler Raw Request as below:</p>
  
  <pre><code>CONNECT 10.10.10.110:8001 
  HTTP/1.1 Host: 10.10.10.110:8001 
  Connection: Keep-Alive 
  User-Agent: Apache-HttpClient/4.1.1 (java 1.5)
  </code></pre>
  
  <p>Any help wouldbe greatly appreciated.</p>