我试图使github OAuth,但它需要一些帖子和得到响应。如何向特定url发出get请求然后以json的形式获得响应,这是我尝试过的代码:
Dim logincookie As CookieContainer
Dim rqtp As Integer
Dim postReq As HttpWebRequest = DirectCast(WebRequest.Create(TextBox1.Text), HttpWebRequest)
Dim postData As String = TextBox1.Text
Dim encoding As New UTF8Encoding
Dim byteData As Byte() = encoding.GetBytes(postData)
Dim tempCookies As New CookieContainer
postReq.Method = "GET"
postReq.KeepAlive = True
postReq.CookieContainer = tempCookies
postReq.ContentType = "application/xml"
postReq.Referer = TextBox1.Text
postReq.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; ru; rv:1.9.2.3) Gecko/20100401 Firefox/4.0 (.NET CLR 3.5.30729)"
postReq.ContentLength = byteData.Length
Dim postreqstream As Stream = postReq.GetRequestStream()
postreqstream.Write(byteData, 0, byteData.Length)
postreqstream.Close()
Dim postresponse As HttpWebResponse
postresponse = DirectCast(postReq.GetResponse(), HttpWebResponse)
tempCookies.Add(postresponse.Cookies)
logincookie = tempCookies
Dim postreqreader As New StreamReader(postresponse.GetResponseStream())
Dim thepage As String = postreqreader.ReadToEnd
response.Text = thepage
Clipboard.Clear()
Clipboard.SetText(thepage)
MsgBox(thepage, vbInformation + vbOK, "result")
如果你愿意,你也可以看到repo,这样你就可以看到整个项目:)
理想情况下将该字符串反序列化为复合对象或动态对象。你可以使用JavaScriptSerializer或NewtonSoft库来实现这一点,并将该对象序列化为json。