我在嘗試構建一個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」塊與手動關閉流有什麼關係? –
如果在流打開時引發異常(或者您離開函數),則手動關閉不會發生。無論使用什麼,使用'都會關閉流。 –