title: How to do a preemptive authentication with c#?
body: <p>Our clients have a Java Web Service that we want to cosume from c#.net application, but when i try to consume or call any method i get an <code>Internal Error</code> message. This web service needs an username and password to authenticate trough the server (where the WS is hosted). </p>
  
  <p>I test the web service with Soap UI and I get a response from the java WS but only if I choose Preemptive Autorizathion option. When I choose another option, I get an error, as I wrote above. </p>
  
  <p>The WS were developed in Java, while our application is developed in .NET Framework 2.0, so the question is: is there any way to authtenticate with preemptive authorization in c#? </p>
  
  <p>If it helps, the WS is hosted on a HTTPS site.</p>
  
  <p>Here is some code that i'm using to authenticate to the server.</p>
  
  <pre><code>NetworkCredential netCredential = new NetworkCredential(username, password); 
  Uri uri = new Uri(service.Url);
  ICredentials credentials = netCredential.GetCredential(uri, "Basic");
  service.Credentials = credentials;
  service.PreAuthenticate = true;
  </code></pre>
  
  <p>Also i tried with this code:</p>
  
  <pre><code>string usernamePassword = username + ":" + password;
  CredentialCache mycache = new CredentialCache();
  WebRequest myReq = WebRequest.Create(s.Url);
  myReq.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes((usernamePassword))));
  mycache.Add(new Uri(service.Url), "Basic", new NetworkCredential(username, password));
  myReq.Credentials = mycache;
  service.Credentials = myReq.Credentials;
  service.UserAgent = ".net FrameWork";  
  </code></pre>
  
  <p>Do you have any idea how to solve this problem?</p>