以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.net/bbs/index.asp)
--  专家坐堂  (http://foxtable.net/bbs/list.asp?boardid=2)
----  如何用代码向第三方服务器发送Get请求,并获取返回的json  (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=132788)

--  作者:pc005637
--  发布时间:2019/3/29 16:51:00
--  如何用代码向第三方服务器发送Get请求,并获取返回的json
对方返回的是
{
    "message": "请求登录成功!",
    "status": "success",
    "sessionId": "4D78FB465F1137DCE9A4B1D9DD960D62"
}
这样子的数据,但我用HttpClient是会报错的。  说明文档是用单引号的 对方发来的是双引号的。 

--  作者:pc005637
--  发布时间:2019/3/29 16:53:00
--  之前有个获取post的例子。怎么样改成Get的?
Dim reqstr As String = Args(0)
Dim bodystr As String = Args(1)

Dim pos As object
try
    Dim body As String = bodystr
    Dim req = System.Net.WebRequest.Create(reqstr)
    req.Method = "POST"
    req.C
    req.UserAgent = "Mozilla/5.0 (Windows NT 6.3; rv:57.0) Gecko/20100101 Firefox/57.0"
    Dim aryBuf As Byte() = Encoding.UTF8.GetBytes(body)
    req.ContentLength = aryBuf.Length
    Dim writer = req.GetRequestStream()
    writer.Write(aryBuf, 0, aryBuf.Length)
    writer.Close()
    writer.Dispose()
    pos = req.GetResponse
catch ex As System.Net.WebException
    pos = ctype(ex.Response,System.Net.HttpWebResponse)
    \'Return "post错误"
End try
Dim stm As System.IO.Stream = pos.GetResponseStream()
Dim reader As New System.IO.StreamReader(stm)
Dim str As String = reader.ReadToEnd
pos.Close
stm.Close
reader.close
Return str

--  作者:有点甜
--  发布时间:2019/3/29 19:38:00
--  

1、用 HttpClient 为什么报错?报什么错?

 

2、直接用就行了吧

 

Dim pos As object
try
    Dim req = System.Net.WebRequest.Create("http://127.0.0.1/test.htm?key=123")
    req.Method = "get"
    pos = req.GetResponse
    Dim stm As System.IO.Stream = pos.GetResponseStream()
    Dim reader As New System.IO.StreamReader(stm)
    Dim str As String = reader.ReadToEnd
    pos.Close
    stm.Close
    reader.close
    msgbox(str)
catch ex As System.Net.WebException
    pos = ctype(ex.Response,System.Net.HttpWebResponse)
    \'Return "post错误"
End try