Skip to content

Instantly share code, notes, and snippets.

@deadprogram
Created October 29, 2008 20:30
Show Gist options
  • Save deadprogram/20820 to your computer and use it in GitHub Desktop.
Save deadprogram/20820 to your computer and use it in GitHub Desktop.
private string Post(string username, string password, string url, string xml)
{
string basicAuth = Convert.ToBase64String(Encoding.ASCII.GetBytes(String.Format("{0}:{1}", username, password)));
//create the web request
WebRequest request = WebRequest.Create(url);
request.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
request.Headers.Add("Authorization", "Basic " + basicAuth);
request.ContentType = "application/xml";
request.ContentLength = xml.Length;
request.Method = "POST";
//get the request stream
Stream stream = request.GetRequestStream();
//convert the xml to an array of bytes
System.Text.Encoding enc = System.Text.Encoding.ASCII;
byte[] xmlArray = enc.GetBytes(xml);
//write the array to the request straem
stream.Write(xmlArray, 0, xmlArray.Length);
//get the response
WebResponse response = request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
return reader.ReadToEnd();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment