Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8511783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:06:09+00:00 2026-06-11T04:06:09+00:00

I am trying to post an image to facebook grap api using asp.net ,

  • 0

I am trying to post an image to facebook grap api using asp.net ,
I know that there is a great post about it here
Posting image from .NET to Facebook wall using the Graph API since I need the code in vb.net I just converted the code described over there but while trying to post he picture to facebook I am getting the following error:

The remote server returned an error: (400) Bad Request.

For me is a litle bit hard to debug this error since this is the only request comming from face therefore I don’t know if the error is because my form post code is not generated properly or the error is whithin the image I’m trying to send.

I was reading a lot about http form post but still I am not able to figure out where my error is … acordingly to the Example provided by facebook I believe I do have all the requiered parameters.

Code in facebook example:

      // Show photo upload form to user and post to the Graph URL
     $graph_url= "https://graph.facebook.com/me/photos?"
     . "access_token=" .$access_token;
     echo '<html><body>';
     echo '<form enctype="multipart/form-data" action="'
     .$graph_url .' "method="POST">';
     echo 'Please choose a photo: ';
     echo '<input name="source" type="file"><br/><br/>';
     echo 'Say something about this photo: ';
     echo '<input name="message" 
         type="text" value=""><br/><br/>';
     echo '<input type="submit" value="Upload"/><br/>';
     echo '</form>';
     echo '</body></html>';  

my form post generated code (before sending image)

—————————–8cf5b7942cad9d0
Content-Disposition: form-data; name=”access_token”

DummyAccessTokkenFNC98HZQdkEK7%2foEWpdyFu%2byHu%2bUKAfbTE54aBB5vdHFJaecGHPpGrLCrd5bEZWxlXvVKej0ApDbjEzjki8xzvl28etjRxH1LzcJP314RO5HJDbNbZJ
—————————–8cf5b7942cad9d0
Content-Disposition: form-data; name=”message”

Lombardi likes stackoverflow
—————————–8cf5b7942cad9d0
Content-Disposition: form-data; name=”source”; filename=”~\img\taco.jpeg”
Content-Type: application/octet-stream

Any help will be highly appreciated .

And this is my full function code

 Private Function FB_UploadPhoto(ByVal album_id As String, ByVal message As String, ByVal filename As String, ByVal bytes As Byte(), ByVal Token As String) As String

    Dim boundary As String = "---------------------------" + DateTime.Now.Ticks.ToString("x")
    Dim path As String = "https://graph.facebook.com/" '& FacebookID() & "/"

    If Not String.IsNullOrEmpty(album_id) Then
        path += album_id + "/"
    End If
    path += "photos"


    Dim uploadRequest As System.Net.HttpWebRequest
    uploadRequest = CType(System.Net.HttpWebRequest.Create(path), System.Net.HttpWebRequest)
    uploadRequest.ServicePoint.Expect100Continue = False
    uploadRequest.Method = "POST"
    uploadRequest.UserAgent = "Mozilla/4.0 (compatible; Windows NT)"
    uploadRequest.ContentType = "multipart/form-data; boundary=" + boundary
    uploadRequest.KeepAlive = False


    'New string builder

    Dim sb As New System.Text.StringBuilder


    'Add Form Data
    Dim formdataTemplate As String = "--{0}" & vbCrLf & "Content-Disposition: form-data; name=""{1}""" & vbCrLf & vbCrLf & "{2}" & vbCrLf
    'Access Token
    sb.AppendFormat(formdataTemplate, boundary, "access_token", HttpContext.Current.Server.UrlEncode(Token))
    ' Message
    sb.AppendFormat(formdataTemplate, boundary, "message", message)
    'header
    Dim headerTemplate As String = "--{0}" & vbCrLf & "Content-Disposition: form-data; name=""{1}""; filename=""{2}""" & vbCrLf & "Content-Type: {3}" & vbCrLf & vbCrLf
    sb.AppendFormat(headerTemplate, boundary, "source", filename, "application/octet-stream")
    'sb.AppendFormat(headerTemplate, boundary, "source", filename, "image/jpeg")

    Dim formString As String = sb.ToString()
    Dim formBytes As Byte() = Encoding.UTF8.GetBytes(formString)
    Dim trailingBytes As Byte() = Encoding.UTF8.GetBytes("" & vbCrLf & "--" & boundary + "--" & vbCrLf)
    Dim image As Byte()

    If bytes Is Nothing Then
        image = System.IO.File.ReadAllBytes(HttpContext.Current.Server.MapPath(filename))
    Else
        image = bytes
    End If

    'memory stream
    Dim imageMemoryStream As New System.IO.MemoryStream()
    imageMemoryStream.Write(image, 0, image.Length)


    ' Set Content Length
    Dim imageLength As Long = imageMemoryStream.Length
    Dim contentLength As Long = formBytes.Length + imageLength + trailingBytes.Length
    uploadRequest.ContentLength = contentLength


    'Get Request Stream
    uploadRequest.AllowWriteStreamBuffering = False
    Dim strm_out As System.IO.Stream = uploadRequest.GetRequestStream()


    'Write to Stream

    strm_out.Write(formBytes, 0, formBytes.Length)

    Dim buffer As Byte() = New Byte(CType(Math.Min(4096, CType(imageLength, Integer)), UInteger)) {} 'New Byte(CUInt(imageLength)) {} ' New Byte(CUInt(Math.Min(4096, CInt(imageLength)))) {} ' 'New Byte(CUInt(Math.Min(4096, CInt(imageLength)))) {} 'New Byte(CType(Math.Min(4096, CType(imageLength, Integer)), UInteger)) {}
    Dim bytesRead As Integer = 0
    Dim bytesTotal As Integer = 0
    imageMemoryStream.Seek(0, IO.SeekOrigin.Begin)

    'While bytesRead = imageMemoryStream.Read(buffer, 0, buffer.Length) <> 0
    '    strm_out.Write(buffer, 0, bytesRead)
    '    bytesTotal += bytesRead
    'End While
    bytesRead = imageMemoryStream.Read(buffer, 0, buffer.Length)
    While bytesRead <> 0
        strm_out.Write(buffer, 0, bytesRead)
        bytesTotal += bytesRead
        bytesRead = imageMemoryStream.Read(buffer, 0, buffer.Length)
    End While

    strm_out.Write(trailingBytes, 0, trailingBytes.Length)

    'Close Stream
    strm_out.Close()


    'Get Web Response
    Dim response As System.Net.HttpWebResponse = uploadRequest.GetResponse()



    ' Create Stream Reader
    Dim reader As System.IO.StreamReader = New System.IO.StreamReader(response.GetResponseStream())


    Return reader.ReadToEnd()
End Function
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T04:06:11+00:00Added an answer on June 11, 2026 at 4:06 am

    What a blender man … the problem was the facebook token itself… actually I am encrypting the token seems that my encryption functions are having some sort of issue while decrypting. Anyways I will leave the code over here hopefuly someone else will find useful.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to post an image to a Facebook album using a form
I am trying to POST an image to imageshack using their API and Play
I am trying to upload an image using ASP.NET with C#.I do not how
I'm trying to post a Facebook status update, using the Facebook iOS API, with
I'm trying to post an image to TwitPic.com using their API. However, I have
i'm trying to post an image to my users's facebook wall once they hit
What I am trying to do is post an image that has been created
I am trying to post an image to an album on facebook and place
I'm trying to post an image from an iphone app to a .Net webservice
I used the Facebook API to post an image or a comment on the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.