将图像从asp.net(VB.net)发布到facebook graph api(错误400错误请求)

将图像从asp.net(VB.net)发布到facebook graph api(错误400错误请求),vb.net,facebook-graph-api,httpwebrequest,Vb.net,Facebook Graph Api,Httpwebrequest,我正在尝试使用asp.net将图像发布到facebook grap api, 我知道这里有一个很好的帖子 由于我需要vb.net中的代码,我刚刚转换了上面描述的代码,但在尝试将图片发布到facebook时,我遇到以下错误: 远程服务器返回错误:(400)请求错误 对我来说,调试这个错误有点困难,因为这是从face提交的唯一请求,所以我不知道这个错误是因为我的表单post代码没有正确生成,还是因为我试图发送的图像中有错误 我读了很多关于http form post的文章,但仍然无法找出错误所在。。

我正在尝试使用asp.net将图像发布到facebook grap api, 我知道这里有一个很好的帖子 由于我需要vb.net中的代码,我刚刚转换了上面描述的代码,但在尝试将图片发布到facebook时,我遇到以下错误:

远程服务器返回错误:(400)请求错误

对我来说,调试这个错误有点困难,因为这是从face提交的唯一请求,所以我不知道这个错误是因为我的表单post代码没有正确生成,还是因为我试图发送的图像中有错误

我读了很多关于http form post的文章,但仍然无法找出错误所在。。。根据要求,我相信我确实具备所有要求的参数

facebook示例中的代码:

      // 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>';  
//向用户显示照片上载表单并发布到图形URL
$graph_url=”https://graph.facebook.com/me/photos?"
. “access_token=”.$access_token;
回声';
回声';
echo“请选择一张照片:”;
回音“

”; echo“说说这张照片吧:”; 回音“

”; 回声“
”; 回声'; 回声';
我的表单后期生成代码(发送图像前)

-----------------------------8cf5b7942cad9d0 内容配置:表单数据;name=“访问令牌”

DummyAccessTokkenFNC98HZQdkEK7%2foEWpdyFu%2byHu%2BUKAFBTE54ABB5VDHFJAECGHPPGRLCRD5BEZWXLXVVKEJ0APDBJEZJKI8XVL28ETJRXH1LZCJP314RO5HJDBNBZJ -----------------------------8cf5b7942cad9d0 内容配置:表单数据;name=“消息”

伦巴第喜欢游泳 -----------------------------8cf5b7942cad9d0 内容配置:表单数据;name=“source”;filename=“~\img\taco.jpeg” 内容类型:应用程序/八位字节流

我们将非常感谢您的帮助

这是我的全部功能代码

 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
Private Function FB_UploadPhoto(ByVal相册_id作为字符串,ByVal消息作为字符串,ByVal文件名作为字符串,ByVal字节作为字节(),ByVal令牌作为字符串)作为字符串
Dim边界为String=“------------------------------------”+DateTime.Now.Ticks.ToString(“x”)
Dim路径作为字符串=”https://graph.facebook.com/“&FacebookID()&”/”
如果不是String.IsNullOrEmpty(唱片集id),则
路径+=相册\u id+“/”
如果结束
路径+=“照片”
Dim uploadRequest作为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(兼容;Windows NT)”
uploadRequest.ContentType=“多部分/表单数据;边界=“+边界”
uploadRequest.KeepAlive=False
'新字符串生成器
将sb设置为新System.Text.StringBuilder
'添加表单数据
Dim formdataTemplate作为字符串=“{0}”&vbCrLf&“内容处置:表单数据;名称=“{1}”“”&vbCrLf&vbCrLf&“{2}”&vbCrLf
'访问令牌
sb.AppendFormat(formdataTemplate,边界,“access_token”,HttpContext.Current.Server.UrlEncode(token))
“消息
sb.AppendFormat(formdataTemplate,边界,“消息”,消息)
'标题
Dim headerTemplate As String=“{0}”&vbCrLf&“内容处理:表单数据;name=“{1}”;filename=“{2}”&vbCrLf&“内容类型:{3}”&vbCrLf&vbCrLf
sb.AppendFormat(headerTemplate,边界,“源”,文件名,“应用程序/八位字节流”)
'sb.AppendFormat(headerTemplate,边界,“源”,文件名,“图像/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”)
将图像变暗为字节()
如果字节是空的,那么
image=System.IO.File.ReadAllBytes(HttpContext.Current.Server.MapPath(文件名))
其他的
图像=字节
如果结束
'内存流
Dim imageMemoryStream作为新系统。IO.MemoryStream()
imageMemoryStream.Write(图像,0,图像.长度)
'设置内容长度
Dim imageLength As Long=imageMemoryStream.Length
Dim contentLength As Long=formBytes.Length+imageLength+trailingBytes.Length
uploadRequest.ContentLength=ContentLength
'获取请求流
uploadRequest.AllowWriteStreamBuffering=False
Dim strm_out为System.IO.Stream=uploadRequest.GetRequestStream()
“写入流
strm_out.Write(formBytes,0,formBytes.Length)
Dim buffer As Byte()=新字节(CType(Math.Min)(4096,CType(imageLength,Integer)){}'新字节(CUInt(imageLength)){}'新字节(CUInt(Math.Min(4096,CInt(imageLength))){}''新字节(CUInt(Math.Min(4096,CInt(imageLength)))){}'新字节(CType(Math.Min(4096,CType(imageLength,Integer)),UInteger)){}
Dim字节读取为整数=0
Dim ByTestTotal为整数=0
imageMemoryStream.Seek(0,IO.SeekOrigin.Begin)
'而bytesRead=imageMemoryStream.Read(buffer,0,buffer.Length)0
'strm_out.Write(缓冲区,0,字节读取)
'ByTestTotal+=字节读取
"结束!
bytesRead=imageMemoryStream.Read(缓冲区,0,缓冲区.长度)
而字节读取0
strm_out.Write(缓冲区,0,字节读取)
ByTestTotal+=字节读取
bytesRead=imageMemoryStream.Read(缓冲区,0,缓冲区.长度)
结束时
strm_out.Write(trailingBytes,0,trailingBytes.Length)
"关流",
strm_out.Close()
'获取Web响应
Dim响应为System.Net.HttpWebResponse=uploadRequest.GetResponse()
'创建流读取器
Dim reader As System.IO.StreamReader=新建System.IO.StreamReader(response.GetResponseStream())
返回reader.ReadToEnd()
端函数

多棒的搅拌机工。。。问题是facebook令牌本身。。。实际上我正在加密令牌,似乎我的加密函数在解密时遇到了某种问题。