ASP.NET - how to make http authentication - Asked By abinav shankar on 14-Nov-11 04:16 AM

hi

i have to call a csw service for getting the login and logut service
and my code is as follows

 Stream stIn;
      StreamReader sr;
      string strbXMLSaverRequestBody = string.Empty;
 HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://localhost:8080/geonetwork/srv/en/csw");     
      req.Method = "POST";
      req.ContentType = "application/xml";
          string strSetlogindetails = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"+
              "<request>" +
              "<username>admin</username>" +
              "<password>admin</password>" +
              "</request>";
strbXMLSaverRequestBody += HttpUtility.UrlEncode(strSetlogindetails);
      byte[] bPostDataArray = Encoding.ASCII.GetBytes(strbXMLSaverRequestBody);
      req.ContentLength = bPostDataArray.Length;
      stIn = req.GetRequestStream();
      stIn.Write(bPostDataArray, 0, bPostDataArray.Length);
      stIn.Close();
      sr = new StreamReader(req.GetResponse().GetResponseStream());

i am getting the Badrequest error pl check where i went wrong

Ravinder Jamgotre replied to abinav shankar on 14-Nov-11 07:37 AM
Hi Abinav,

Try using Credential Cache
CredentialCache myCache = new CredentialCache(); myCache.Add(new Uri("http://URL/"),"Basic",new NetworkCredential(UserName,SecurelyStoredPassword)); myCache.Add(new Uri("http://URL/"),"Digest", new NetworkCredential(UserName,SecurelyStoredPassword,Domain)); req.Credentials = myCache;