postUpdate API Call from .NET

19 views
Skip to first unread message

JB

unread,
Feb 22, 2008, 1:19:03 AM2/22/08
to Twitxr API developers
I am writing a wrapper for the Twitxr API to use with .NET. I am
having some problems in getting the image to post with a text update.
The text message posts properly along with the location, but the image
is not making it through. I'm assuming I have my syntax wrong
somewhere. Any C# developers out there that want to take a shot at
this? Any help would be appreciated.

protected string ExecutePostUpdate(byte[] photo, string text,
string place)
{
HttpWebRequest request =
(HttpWebRequest)HttpWebRequest.Create(@"http://twitxr.com/api/rest/
postUpdate");
request.PreAuthenticate = true;
request.AllowWriteStreamBuffering = true;

string source = string.Empty;

if (!string.IsNullOrEmpty(Username) && !
string.IsNullOrEmpty(Password))
{
request.Credentials = new NetworkCredential(Username,
Password);
request.ContentType = "application/x-www-form-urlencoded";
request.Method = "POST";

source = string.Format("image={0}&text={1}&place={2}",
Encoding.UTF8.GetString(photo), text, place);
byte[] bytes = Encoding.UTF8.GetBytes(source);

request.ContentLength = bytes.Length;

using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(bytes, 0, bytes.Length);

using (WebResponse response = request.GetResponse())
{
using (StreamReader reader = new
StreamReader(response.GetResponseStream()))
{
return reader.ReadToEnd();
}
}
}
}

return null;
}

JB

unread,
Feb 23, 2008, 4:13:46 PM2/23/08
to Twitxr API developers
I'm getting closer and figured out that I have to do a multipart
post. It seems to post the text and the image, but the image is
actually missing when I look online. When I click on the image box
with the "X" in it, it comes back and says "Error: images are not
supported", so I'm not sure what is going on here. I'm looking for
anyone who can look at this updated code and tell me possibly what I
am doing wrong. Here's the new code:

protected string ExecutePostCommand(string filename, byte[] photo,
string
text, string place)
{
HttpWebRequest request =
(HttpWebRequest)HttpWebRequest.Create(@"http://testdomain.com/api/
post");
request.PreAuthenticate = true;
request.AllowWriteStreamBuffering = true;

string boundary = System.Guid.NewGuid().ToString();

if (!string.IsNullOrEmpty(Username) && !
string.IsNullOrEmpty(Password))
{
request.Credentials = new NetworkCredential(Username,
Password);
request.ContentType = string.Format("multipart/form-data;
boundary={0}", boundary);
request.Method = "POST";

// Build Contents for Post
string header = string.Format("--{0}", boundary);
string footer = header + "--";

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(Encoding.UTF8.GetString(photo));
// Text
contents.AppendLine(header);
contents.AppendLine("Content-Disposition: form-data; name=
\"text\"");
contents.AppendLine();
contents.AppendLine(text);
// Place
contents.AppendLine(header);
contents.AppendLine("Content-Disposition: form-data; name=
\"place\"");
contents.AppendLine();
contents.AppendLine(place);
// Footer
contents.AppendLine(footer);

// This is sent to the Post
byte[] bytes =
Encoding.UTF8.GetBytes(contents.ToString());

request.ContentLength = bytes.Length;

using (Stream requestStream = request.GetRequestStream())
{
requestStream.Write(bytes, 0, bytes.Length);
requestStream.Flush();
requestStream.Close();
Reply all
Reply to author
Forward
0 new messages