Here is the function I'm using - basically a slightly modified version of the KB article at
http://support.microsoft.com/kb/908573private string SendANote(Uri uriLocation, string strUsername, string strPassword, string strBody)
{
try
{
HttpWebRequest WRequest;
HttpWebResponse WResponse;
//preAuth the request
WRequest = (HttpWebRequest)HttpWebRequest.Create(uriLocation);
// Set the username and the password.
WRequest.Credentials = new NetworkCredential(strUsername, strPassword);
WRequest.PreAuthenticate = true;
WRequest.UserAgent = "BrightKiteMobile";
WRequest.Method = "HEAD";
WRequest.Timeout = 10000;
WResponse = (HttpWebResponse)WRequest.GetResponse();
WResponse.Close();
// Make the real request.
WRequest = (HttpWebRequest)HttpWebRequest.Create(uriLocation);
// Set the username and the password.
WRequest.Credentials = new NetworkCredential(strUsername, strPassword);
//WRequest.PreAuthenticate = true;
WRequest.UserAgent = "BrightKiteMobile";
WRequest.Method = "POST";
WRequest.AllowWriteStreamBuffering = false;
WRequest.Timeout = -1;
StringBuilder data = new StringBuilder();
data.Append(strBody);
// Create a byte array of the data we want to send
byte[] byteData = UTF8Encoding.UTF8.GetBytes(data.ToString());
// Set the content length in the request headers
WRequest.ContentLength = byteData.Length;
// Write data
using (Stream postStream = WRequest.GetRequestStream())
{
postStream.Write(byteData, 0, byteData.Length);
}
// Read your response data here.
// Get response
using (WResponse = WRequest.GetResponse() as HttpWebResponse)
{
// Get the response stream
StreamReader reader = new StreamReader(WResponse.GetResponseStream());
// Console application output
string strOutput = reader.ReadToEnd();
return strOutput;
}
WResponse.Close();
}
catch (Exception ex)
{
string strErrorMessage = "";
switch (ex.Message)
{
case "The remote server returned an error: (401) Unauthorized.":
strErrorMessage = "Unable to contact BrightKite - please make sure your username and password is correct on the Settings page!";
MessageBox.Show(strErrorMessage, "Authentication error");
return strErrorMessage;
break;
case "Could not establish connection to network.":
strErrorMessage ="Unable to establish a data connection. Please try again when you have sufficient network signal.";
MessageBox.Show(strErrorMessage, "Network error");
return strErrorMessage;
break;
default:
strErrorMessage = "An error occurred contacting BrightKite.com. Please try again later.";
MessageBox.Show(strErrorMessage, "Error!");
return strErrorMessage;
break;