• Advertisement
Sign in to follow this  

[.net] HttpWebResqust to post an image

This topic is 2616 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all,

I'm developing a PC application in VC++ .NET and I'm trying to publish a photo using HttpWebRequest. When trying to POST a photo, I get a 400 (Bad Request) error. I've been able to publish status updates, so I think the problem lies in how I'm trying to attach the image.

What I have so far (commented code reflects other ways I've tried):

[source lang=cpp]

String^ req = "https://graph.facebook.com/123456789/photos?access_token=" + access_code;
req += "&source=./Untitled-3.jpg";
req += "&message=HelloWorld";

array<unsigned char>^ cstr = System::IO::File::ReadAllBytes("C:\\ar3.jpg");
System::Text::UTF8Encoding^ utf8 = gcnew System::Text::UTF8Encoding;
array<Byte>^ encodedBytes = utf8->GetBytes( req->ToString() );
//String^ headerTemplate = "Content-Disposition: form-data; name=\"ar3.jpg\"; filename=\"C:\\ar3.jpg\"\r\n Content-Type: image/jpeg\r\n\r\n;Content-Transfer-Encoding: binary\r\n";
//array<Byte>^ hdr = utf8->GetBytes( headerTemplate->ToString() );

//String ^bdry = "--------";
array<Byte>^ bbytes = utf8->GetBytes( bdry->ToString() );

HttpWebRequest^ hwr = (HttpWebRequest^)HttpWebRequest::Create(req);

//hwr->ContentLength = encodedBytes->Length;
//hwr->ContentLength += hdr->Length;
//hwr->ContentLength += cstr->Length;

hwr->Method = "POST";
hwr->ContentType = "multipart/form-data";
hwr->UserAgent = "11112222333344444";

hwr->KeepAlive = true;

System::IO::Stream^ stream = hwr->GetRequestStream();
stream->Write(encodedBytes, 0, encodedBytes->Length);
//stream->Write(bbytes, 0, bbytes->Length);
//stream->Write(hdr, 0, hdr->Length);
stream->Write(cstr, 0, cstr->Length);
//stream->Write(bbytes, 0, bbytes->Length);

try {
stream->Close();
WebResponse^ webResponse = hwr->GetResponse();

textBox1->Text = webResponse->ToString();
} catch(WebException^ e) {
textBox1->Text = e->ToString();
}




Like I said, this works for posting status updates, so I think it's how I'm attaching the image. Any thoughts?

Share this post


Link to post
Share on other sites
Advertisement
Please change your source tags, I can't read the code this way.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement