Blog de Francisco Velázquez

otro blog personal

Basic Authentication en HTTP Post

leave a comment »

string uri = string.Empty, user = string.Empty, password = string.Empty, postData = string.Empty, responseData = string.Empty;
            string encodedAuthorization = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(user + ":" + password));
HttpWebRequest webRequest = WebRequest.Create(uri) as HttpWebRequest;
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.Headers = new WebHeaderCollection();
webRequest.Headers.Add("Authorization: Basic "+encodedAuthorization);

using (StreamWriter rw = new StreamWriter(webRequest.GetRequestStream()))
{
            rw.Write(postData);
}

using (StreamReader sr = new StreamReader(webRequest.GetResponse().GetResponseStream()))
{
            responseData = sr.ReadToEnd();
}

Written by fravelgue

February 10, 2010 at 10:58 pm

Posted in development

Tagged with , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: