title: HP ALM 12.21 REST API - 401 Unauthorized - C#
body: <p>I am trying to use the API against our ALM 12.21 server, but always ends up with "401 Unauthorized". It seems that I get the auth cookie back correctly, but when I try to do something after that I am unauthorized.</p>
  
  <p>I use this the get this to get auth cookie (seems to work):</p>
  
  <pre><code>HttpWebRequest myauthrequest = (HttpWebRequest)WebRequest.Create("https://server/qcbin/authentication-point/alm-authenticate");
  
              string AuthenticationXML = @"&lt;alm-authentication&gt;
                          &lt;user&gt;username&lt;/user&gt;                
                          &lt;password&gt;password&lt;/password&gt;       
                          &lt;/alm-authentication&gt;";
  
              byte[] Requestbytes = Encoding.UTF8.GetBytes(AuthenticationXML);
              myauthrequest.Method = "POST";
              myauthrequest.ContentType = "application/xml";
              myauthrequest.ContentLength = Requestbytes.Length;
              myauthrequest.Accept = "application/xml";
              Stream RequestStr = myauthrequest.GetRequestStream();
              RequestStr.Write(Requestbytes, 0, Requestbytes.Length);
              RequestStr.Close();
              HttpWebResponse myauthres = (HttpWebResponse)myauthrequest.GetResponse();
              var AuthenticationCookie = myauthres.Headers.Get("Set-Cookie");
              AuthenticationCookie = AuthenticationCookie.Replace(";Path=/;HTTPOnly", "");
  </code></pre>
  
  <p>I am not sure if the .Replace is needed. Just read it somewhere. I get 401 both with or without it though, when trying to do subsequent requests.</p>
  
  <p>Trying e.g. this after getting auth cookie:</p>
  
  <pre><code> HttpWebRequest req = (HttpWebRequest)WebRequest.Create("https://server/qcbin/rest/domains/FS/projects/P3602_SLS_Project/defects/1");
              req.Method = "GET";
              req.ContentType = "application/xml";
              req.Accept = "application/octet-stream";
              req.Headers.Set(HttpRequestHeader.Cookie, AuthenticationCookie);
              HttpWebResponse res = (HttpWebResponse)req.GetResponse();
              Stream RStream2 = res.GetResponseStream();
              XDocument doc = XDocument.Load(RStream2);
  </code></pre>
  
  <p>Which fails with 401.</p>
  
  <p>Anyone have complete working code for the ALM 12.21 REST API?</p>