title: Web Service - C# Client and Apache Server Basic Authentication - Error HTTP 400 Bad Request
body: <p>I write here because I'm working to transform a PHP webservice client to C# (VS2010).</p>
  
  <p>Project are in Framework4 but the web service was added in VS2010 like a web service compatible with Framework2 (Web Reference with WSDL).</p>
  
  <p>Actually I'm working only on a classical webservice (a Helloworld test).</p>
  
  <p>With the client C# I have no problem I send a text to the web service and the web service responds by returing the text. I have no error in the Apache Server.</p>
  
  <p>If I activate the basic authentification in the .htaccess apache server (host the webservice server), so in the client C# I have this error :</p>
  
  <p>Exception : WebException
  Bad Request with state HTTP 400 : Bad Request.</p>
  
  <p>In Apache error log I see this line :</p>
  
  <pre><code>[Thu Feb 02 23:52:06 2012] [error] [client XX.XX.XX.XX] Invalid URI in request
  &lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"     
  xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" 
  xmlns:tns="https://www.domaine.com/api/helloworld/webservices.php?wsdl" 
  xmlns:types="https://www.domaine.com/api/helloworld/webservices.php?wsdl/encodedTypes" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org
  /2001/XMLSchema"&gt;
  &lt;soap:Body soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"&gt;
  &lt;tns:bonjour&gt;&lt;prenom xsi:type="xsd:string"&gt;Toto&lt;/prenom&gt;&lt;/tns:bonjour&gt;
  &lt;/soap:Body&gt;
  &lt;/soap:Envelope&gt;
  POST /api/helloworld/webservices.php HTTP/1.1
  </code></pre>
  
  <p>.</p>
  
  <p>I'm sure is the credentials who are not working because when I try with a false user and password I have always the 400 Bad Request.</p>
  
  <p>The code of C# Client are :
      // We create the webservice
      WebService mywebservice = new WebService();</p>
  
  <pre><code>// We define BASIC Authentication             
  CredentialCache myCredentials = new CredentialCache();         
  NetworkCredential netCred = new NetworkCredential("User1", "Pass1");     
  myCredentials.Add(new Uri(mywebservice.Url), "Basic", netCred);
  myCredentials.PreAuthenticate = true;
  mywebservice.Credentials = myCredentials;
  
  // We define the UserAgent
  mywebservice.UserAgent = ".NET Framework";
  
  // We call the function of webservice
  string retour = mywebservice.bonjour("Tata");
  
  // We show the return
  MessageBox.Show(retour, "Retour de l'API", MessageBoxButtons.OK, MessageBoxIcon.Information);
  </code></pre>
  
  <p>.</p>
  
  <p>The .htaccess :
      # Don't show directory listings for URLs which map to a directory.
      Options -Indexes</p>
  
  <pre><code># Se logue avec un compte AD
  AuthType Basic
  AuthBasicProvider ldap
  AuthName "Acces au webservice"
  AuthLDAPURL ldap://127.0.0.1:389/ou=clients,dc=domaine,dc=com?mail
  Require valid-user 
  </code></pre>
  
  <p>.</p>
  
  <p>If I delete the .htaccess file everything it's working ... but without authentication :(</p>
  
  <p>I post also the request and response with .htaccess (not working) :</p>
  
  <pre><code>POST https://www.domaine.com/api/login_ovh_pnp/webservices.php HTTP/1.1
  User-Agent: .NET Framework
  VsDebuggerCausalityData: uIDPo9/Tb/xbJxtKvSNra7boyJsAAAAAQcQin1OhXXXXXXXXXXX/KqROJW0w1FhAcrrhI1sGQACQAA
  Content-Type: text/xml; charset=utf-8
  SOAPAction: "https://www.domaine.com/api/login_ovh_pnp/webservices.php?wsdl#bonjour"
  Authorization: Basic bHZlaXJtYW5AYXF1aWxhLWXXXXXXXXXXXbmcuZnI6ZHluYXRlcmE3OA==
  Host: www.domaine.com
  Content-Length: 623
  Expect: 100-continue
  
  &lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
  xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" 
  xmlns:tns="https://www.domaine.com/api/login_ovh_pnp/webservices.php?wsdl" 
  xmlns:types="https://www.domaine.com/api/login_ovh_pnp/webservices.php?wsdl
  /encodedTypes" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
  &lt;soap:Body soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"&gt;
  &lt;tns:bonjour&gt;
  &lt;prenom xsi:type="xsd:string"&gt;Tata&lt;/prenom&gt;
  &lt;/tns:bonjour&gt;
  &lt;/soap:Body&gt;    
  &lt;/soap:Envelope&gt;
  
  =====================================
  =====================================
  
  HTTP/1.1 400 Bad Request
  Date: Mon, 06 Feb 2012 13:40:32 GMT
  Server: Apache
  Vary: Accept-Encoding
  Content-Length: 226
  Connection: close
  Content-Type: text/html; charset=iso-8859-1
  
  &lt;!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"&gt;
  &lt;html&gt;&lt;head&gt;
  &lt;title&gt;400 Bad Request&lt;/title&gt;
  &lt;/head&gt;&lt;body&gt;
  &lt;h1&gt;Bad Request&lt;/h1&gt;
  &lt;p&gt;Your browser sent a request that this server could not understand.&lt;br /&gt;
  &lt;/p&gt;
  &lt;/body&gt;&lt;/html&gt;
  </code></pre>
  
  <p>.</p>
  
  <p>And for finish the request without .htaccess :</p>
  
  <pre><code>POST https://www.domaine.com/api/login_ovh_pnp/webservices.php HTTP/1.1
  User-Agent: .NET Framework
  VsDebuggerCausalityData:    
  uIDPo4r/MO3TgmdMkQiWHfkXoWwAAAAA1krYOM0NBkS9vLzFQe3Vmln8F3GXClFNrTWXL/L622YACQAA
  Content-Type: text/xml; charset=utf-8
  SOAPAction: "https://www.domaine.com/api/login_ovh_pnp/webservices.php?wsdl#bonjour"
  Host: www.domaine.com
  Content-Length: 623
  Expect: 100-continue
  Connection: Keep-Alive
  
  &lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" 
  xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" 
  xmlns:tns="https://www.domaine.com/api/login_ovh_pnp/webservices.php?wsdl" 
  xmlns:types="https://www.domaine.com/api/login_ovh_pnp/webservices.php?wsdl
  /encodedTypes" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
  &lt;soap:Body soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"&gt;
  &lt;tns:bonjour&gt;
  &lt;prenom xsi:type="xsd:string"&gt;Tata&lt;/prenom&gt;
  &lt;/tns:bonjour&gt;
  &lt;/soap:Body&gt;&lt;/soap:Envelope&gt;
  
  =====================================
  =====================================
  
  HTTP/1.1 200 OK
  Date: Mon, 06 Feb 2012 13:51:31 GMT
  Server: Apache
  X-SOAP-Server: NuSOAP/0.9.5 (1.123)
  Content-Length: 575
  Vary: Accept-Encoding
  Keep-Alive: timeout=15, max=100
  Connection: Keep-Alive
  Content-Type: text/xml; charset=ISO-8859-1
  
  &lt;?xml version="1.0" encoding="ISO-8859-1"?&gt;
  &lt;SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"&gt;
  &lt;SOAP-ENV:Body&gt;
  &lt;ns1:bonjourResponse xmlns:ns1="https://supervision.dynatera.net/api/login_ovh_pnp/webservices.php?wsdl"&gt;
  &lt;return xsi:type="xsd:string"&gt;Bonjour Tata&lt;/return&gt;&lt;/ns1:bonjourResponse&gt;
  &lt;/SOAP-ENV:Body&gt;
  &lt;/SOAP-ENV:Envelope&gt;
  </code></pre>
  
  <p>.</p>
  
  <p>Thank you very much for your help on this complex problem because I searched  many times without found anything to fix this issue.</p>