vikas nakil

vikas nakil

  • NA
  • 8
  • 4.1k

how to login basic autheintication to an url(website) from window base application ,c#?

May 8 2012 10:32 AM

guys ,

i dont know whether to post based on C# over here or not..?

by sum reason, i want to retrieve report from an url   through my  window base app.

but i want do login to an url from my windows based app with login id and password ?

as i got code that works on web app but it give empty string while excuting on windows based app.

using System.Data.SqlClient;
using System.IO;
using System.Xml;
using System.Net;
using System.Text.RegularExpressions;

     string username = "abc";
            string pass = "xyz";
            string requestUrl = "http://qwerty.com";          

HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(requestUrl);
            string usernamePassword = username + ":" + pass;
            myReq.Timeout = 20000;
            myReq.UserAgent = "sample";

            //Use the CredentialCache so we can attach the authentication to the request           
            CredentialCache mycache = new CredentialCache();

            //this perform Basic auth
            mycache.Add(new Uri(requestUrl), "Basic", new NetworkCredential(username, pass));
           
            myReq.Credentials = mycache;
            myReq.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(usernamePassword)));
           
     //Send and receive the response
            WebResponse wr = myReq.GetResponse();
            Stream receiveStream = wr.GetResponseStream();
            StreamReader reader = new StreamReader(receiveStream, Encoding.UTF8);
            string content = reader.ReadToEnd();
            Console.Write(content);


but it doesnt work on windows app.?