Here is the code which I used with a specified url from the “https://dev.twitter.com/docs/api/1/post/account/update_profile_background_image”. But in this, I need to pass NetworkCredential, which I don’t have as I am approaching the OAuth approach(which generates a verfier to verify the Credentials)
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.ServicePoint.Expect100Continue = false;
request.PreAuthenticate = true;
request.AllowWriteStreamBuffering = true;
request.Timeout = int.MaxValue;
string boundary = System.Guid.NewGuid().ToString();
request.ContentType = string.Format("multipart/form-data;boundary={0}", boundary);
request.Method = "POST";
request.Credentials = new NetworkCredential(LoginUserObject._UserName, LoginUserObject._Password);
//Build Contents for Post
string header = string.Format("--{0}", boundary);
string footer = string.Format("--{0}--", boundary);
StringBuilder contents = new StringBuilder();
//Image
contents.AppendLine(header);
contents.AppendLine(string.Format("Content-Disposition:form-data); name=\"image\"); filename=\"{0}\"", filename));
contents.AppendLine("Content-Type: image/jpeg");
contents.AppendLine();
contents.AppendLine(System.Text.Encoding.GetEncoding("iso-8859-1").GetString(photo));
// Footer
contents.AppendLine(footer);
// Data that is sent with the post
byte[] bytes = Encoding.GetEncoding("iso-8859-1").GetBytes(contents.ToString());
request.ContentLength = bytes.Length;
using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(bytes, 0, bytes.Length);
requestStream.Flush();
requestStream.Close();
using (WebResponse response = request.GetResponse())
{
receiveStream = response.GetResponseStream();
readStream = new StreamReader(receiveStream, Encoding.UTF8);
Result = readStream.ReadToEnd();
}
}