我在尝试构建一个VB.net应用程序,它将通过他们的Graph API批量请求方法从Facebook检索各种用户。我的问题似乎是在执行实际的请求。当我尝试检索响应时,服务器收到400错误,提示“请求错误”。我不确定这是否意味着我很难形成请求的主体,或者如果我在代码中附加了错误的话。Facebook批量请求
这里是我的请求代码:
Dim sURL As String
sURL = "https://graph.facebook.com"
Dim wrGETURL As WebRequest
wrGETURL = WebRequest.Create(sURL)
wrGETURL.Method = "POST"
wrGETURL.Proxy = WebRequest.DefaultWebProxy
Dim reqStream As IO.Stream = wrGETURL.GetRequestStream()
Dim access_token As String = "..." ' Access token removed, but it is valid
Dim jsonReq As String = """access_token"": """ + access_token + """"
jsonReq = jsonReq + """batch"": ["
AddBatchRequest(jsonReq, "[email protected]")
AddBatchRequest(jsonReq, "[email protected]")
jsonReq = jsonReq + "]"
Dim enc As New System.Text.UTF8Encoding()
Dim jsonBytes As Byte() = enc.GetBytes(jsonReq)
reqStream.Write(jsonBytes, 0, jsonBytes.Length)
reqStream.Close()
Dim oLabel As Label = output_label
Dim objStream As Stream
Try
objStream = wrGETURL.GetResponse.GetResponseStream() '<<<<Crashes here
Dim objReader As New StreamReader(objStream)
Dim sLine As String = ""
Dim i As Integer = 0
Dim ostr As String = ""
Do While Not sLine Is Nothing
i += 1
sLine = objReader.ReadLine
If Not sLine Is Nothing Then
ostr = ostr + sLine
End If
Loop
oLabel.Text = ostr
Catch ex As Exception
oLabel.Text = ex.Message
End Try
Private Sub AddBatchRequest(ByRef json As String, ByVal email As String)
If json.EndsWith("}") Then
json = json + ","
End If
json = json + "{""method: ""GET"", ""relative_url"": ""search?q=" + email + "&type=user""}"
End Sub
我试过一吨就如何形成串身体的变化,但他们都不工作,领导让我相信,我不加入正确的身体。
两个难题:第一,总是在流的周围使用'Using'块,而不是手动关闭它们。其次,“不是x是y”可以(也应该)缩短为“x不是y”。 –
我对VB.net真的很陌生,我的大部分经验都是在c/C++中,所以我不知道VB中的很多特殊语法。 “Using”块与手动关闭流有什么关系? –
如果在流打开时引发异常(或者您离开函数),则手动关闭不会发生。无论使用什么,使用'都会关闭流。 –