I want to update image in status using c#. Here is my code… that returns (400) Bad Request…
Any help will be appreciated…
thanks…
public const string url = "https://api.twitter.com/1.1/statuses/update_with_media.json";
string contentType = fileAttachment.PostedFile.ContentType;
HttpPostedFile myFile = fileAttachment.PostedFile;
string fileName = myFile.FileName;
int nFileLen = myFile.ContentLength;
byte[] binaryImageData = new byte[nFileLen];
myFile.InputStream.Read(binaryImageData, 0, nFileLen);
HttpWebRequest webRequest = null;
StreamWriter requestWriter = null;
string responseData = “”;
webRequest = System.Net.WebRequest.Create(url) as HttpWebRequest;
webRequest.Method = method.ToString();
webRequest.ServicePoint.Expect100Continue = false;
webRequest.ContentType = "application/x-www-form-urlencoded";
if (binaryImageData != null)
{
string boundary = Guid.NewGuid().ToString();
string encoding = "iso-8859-1";
webRequest.PreAuthenticate = true;
webRequest.AllowWriteStreamBuffering = true;
webRequest.ContentType = string.Format("multipart/form-data; boundary={0}", boundary);
webRequest.Method = "POST";
string header = string.Format("--{0}", boundary);
string footer = string.Format("--{0}--", boundary);
StringBuilder contents = new StringBuilder();
contents.AppendLine(header);
string fileContentType = ContentType;
string fileHeader = String.Format("Content-Disposition: file; name=\"{0}\"; filename=\"{1}\"", "media", filename);
string fileData = Encoding.GetEncoding(encoding).GetString(binaryImageData);
contents.AppendLine(fileHeader);
contents.AppendLine(String.Format("Content-Type: {0}", fileContentType));
contents.AppendLine();
contents.AppendLine(fileData);
contents.AppendLine(footer);
byte[] bytes = Encoding.GetEncoding(encoding).GetBytes(contents.ToString());
webRequest.ContentLength = bytes.Length;
using (Stream requestStream = webRequest.GetRequestStream())
{
requestStream.Write(bytes, 0, bytes.Length);
}
WebResponse(webRequest);
using (var response = (HttpWebResponse)webRequest.GetResponse())
{
if (response.StatusCode == HttpStatusCode.OK)
{
//Posting Successfull
}
}