For any one interested, here is a completed function.

public static void UploadProfileImage(byte[] photo, string username,
string pwd)
        {
            //photo is just a byte array of the image data

            //A recent change to Twitter's api requires this line to
be included for .NET clients because
            //of how the HttpWebRequest object formats the header.  I
had to set this in the new for my class, it
            //seemed to be too late if I set it in this function.
You'll get a 417 error without setting this.
            //  System.Net.ServicePointManager.Expect100Continue =
false;

            HttpWebRequest request = (HttpWebRequest)
HttpWebRequest.Create(@"http://twitter.com/account/
update_profile_image.xml");

            request.PreAuthenticate = true;
            request.AllowWriteStreamBuffering = true;

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

            request.Credentials = new NetworkCredential(username,
pwd);
            request.ContentType = string.Format("multipart/form-data;
boundary={0}", boundary);
            request.Method = "POST";

            // Build Contents for Post
            string header = "--" + boundary;
            string footer = "--" + boundary + "--";

            StringBuilder contents = new StringBuilder();

            // Image
            contents.AppendLine(header);
            contents.AppendLine(string.Format("Content-Disposition:
form-data); name=\"image\"); filename=\"{0}\"",
"twitterProfilePhoto.jpg"));
            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())
                {
                    using (StreamReader reader = new
                    StreamReader(response.GetResponseStream()))
                    {
                        string s = reader.ReadToEnd();
                    }
                }
            }
        }

On Dec 9 2008, 9:50 am, Sean <sean.er...@gmail.com> wrote:
> Thanks Lien.
>
> I was able to get this figured out.  It was a problem with the way I
> was encoding the image data.  I needed to be using iso-8859-1.
> I really appreciate your help
>
> On Dec 8, 5:00 pm,Sean<sean.er...@gmail.com> wrote:
>
>
>
> > Thanks Lien,
>
> > I am trying to get this done using c#.NET and I think I am getting
> > closer.  What is happening with my request is it is getting truncated
> > only a few characters in to the actualimagedata so I don't have a
> > footer boundary.  The post completes successfully, but theimagethat
> > gets uploaded to the server isn't formatted correctly.
>
> > Here is the full request body -
> > POST /account/update_profile_image.xml HTTP/1.1
>
> > Content-Type: multipart/form-data;
> > boundary=125e2d3d-97d3-44fc-8267-9a8ef2d79644
>
> > Authorization: Basic <removed>
>
> > Host:twitter.com
>
> > Content-Length: 201010
>
> > Expect: 100-continue
>
> > --125e2d3d-97d3-44fc-8267-9a8ef2d79644
>
> > Content-Disposition: form-data; name="image"; filename="seantest.jpg"
>
> > Content-Type:image/jpeg
>
> > ÿØÿà
>
> > Any ideas what could be causing theimagedata to be truncated?
> > Thanks again for your help
>
> > On Dec 8, 3:24 pm, Lien Tran <lientra...@gmail.com> wrote:
>
> > > Here's what my request body looks like:
>
> > > POST /account/update_profile_image.xml HTTP/1.1
> > > Authorization: Basic <removed>
> > > Content-Type: multipart/form-data;
> > > boundary=-----------------------------1228771270538
> > > User-Agent: Java/1.6.0_02
> > > Host:twitter.com
> > > Accept: text/html,image/gif,image/jpeg, *; q=.2, */*; q=.2
> > > Connection: keep-alive
> > > Content-Length: 71380
>
> > > -------------------------------1228771270538
> > > Content-Disposition: form-data; name="image"; filename="Sunset.jpg"
> > > Content-Type:image/jpeg
>
> > > <binary data here>
> > > -------------------------------1228771270538--HTTP/1.1 200 OK
>
> > > On Dec 8, 8:11 am,Sean<sean.er...@gmail.com> wrote:
>
> > > > Would you mind posting a sample of your correctly formatted request
> > > > here?  I  am running windows and haven't been able to get curl up and
> > > > running yet.
>
> > > > Thanks
>
> > > >Sean
>
> > > > On Dec 8, 12:06 am, Lien Tran <lientra...@gmail.com> wrote:
>
> > > > > Thanks Alex.  I used curl to see what the request should look like and
> > > > > then coded up my request accordingly.  It's working for me now.
>
> > > > > On Dec 6, 11:37 am, "Alex Payne" <a...@twitter.com> wrote:
>
> > > > > > The test we use for this method is to use curl:
>
> > > > > > curl -F 'ima...@path/to/test/image.jpg' -u 
> > > > > > USERNAME:PASSWORDhttp://twitter.com/account/update_profile_image.xml
>
> > > > > > If you use an HTTP proxy, you can see it generating the appropriate
> > > > > > request and response.
>
> > > > > > On Sat, Dec 6, 2008 at 00:09, Lien Tran <lientra...@gmail.com> 
> > > > > > wrote:
>
> > > > > > > Hello,
>
> > > > > > > I've been trying to update myprofileimageusing the account method
> > > > > > > update_profile_image.  However, the server keeps returning the 
> > > > > > > error
> > > > > > > "There was a problem with your picture. Probably too big."  The 
> > > > > > > photo
> > > > > > > I am trying touploadis a jpg less than 700 kilobytes in size.  
> > > > > > > Below
> > > > > > > is the request body and request response.
>
> > > > > > > Request body:
> > > > > > > POST /account/update_profile_image.xml HTTP/1.1
> > > > > > > Authorization: Basic <encoded credentials here>
> > > > > > > User-Agent: Jakarta Commons-HttpClient/3.1
> > > > > > > Host:twitter.com
> > > > > > > Content-Length: 71440
> > > > > > > Content-Type: multipart/form-data; boundary=tUGDGHg6-
> > > > > > > mbUEjVXYFhFWeb_NFmBUxiXOK
>
> > > > > > > --tUGDGHg6-mbUEjVXYFhFWeb_NFmBUxiXOK
> > > > > > > Content-Disposition: form-data; name="Sunset.jpg";
> > > > > > > filename="Sunset.jpg"
> > > > > > > Content-Type: application/octet-stream; charset=ISO-8859-1
> > > > > > > Content-Transfer-Encoding: binary
>
> > > > > > > <binary data here>
>
> > > > > > > --tUGDGHg6-mbUEjVXYFhFWeb_NFmBUxiXOK--
>
> > > > > > > Response body:
> > > > > > > HTTP/1.1 403 Forbidden
> > > > > > > Date: Sat, 06 Dec 2008 07:59:53 GMT
> > > > > > > Server: hi
> > > > > > > Last-Modified: Sat, 06 Dec 2008 07:59:53 GMT
> > > > > > > Status: 403 Forbidden
> > > > > > > Pragma: no-cache
> > > > > > > Cache-Control: no-cache, no-store, must-revalidate, pre-check=0, 
> > > > > > > post-
> > > > > > > check=0
> > > > > > > Content-Type: application/xml; charset=utf-8
> > > > > > > Content-Length: 183
> > > > > > > Expires: Tue, 31 Mar 1981 05:00:00 GMT
> > > > > > > Set-Cookie:
> > > > > > > _twitter_sess=BAh7BzoHaWQiJWRhOWNmNjI1MGM5MjRmYWIwOGEzOGQwNTQyYzNmZTNjIgpm
> > > > > > > %250AbGFzaElDOidBY3Rpb25Db250cm9sbGVyOjpGbGFzaDo6Rmxhc2hIYXNoewAG
> > > > > > > %250AOgpAdXNlZHsA--d9fe4dcadf2064553d3371c9fe767ff009f20c21;
> > > > > > > domain=.twitter.com; path=/
> > > > > > > Vary: Accept-Encoding
> > > > > > > Connection: close
>
> > > > > > > <?xml version="1.0" encoding="UTF-8"?>
> > > > > > > <hash>
> > > > > > >  <request>/account/update_profile_image.xml</request>
> > > > > > >  <error>There was a problem with your picture. Probably too big.</
> > > > > > > error>
> > > > > > > </hash>
>
> > > > > > > Does the request body look correct?  Does anyone have a sample of 
> > > > > > > what
> > > > > > > the request body should look like if this is not correct?
>
> > > > > > > Thanks.
>
> > > > > > --
> > > > > > Alex Payne - API Lead,Twitter, 
> > > > > > Inc.http://twitter.com/al3x-Hidequotedtext-
>
> > > > > - Show quoted text -- Hide quoted text -
>
> > > - Show quoted text -- Hide quoted text -
>
> > - Show quoted text -- Hide quoted text -
>
> - Show quoted text -

Reply via email to