以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.net/bbs/index.asp)
--  专家坐堂  (http://foxtable.net/bbs/list.asp?boardid=2)
----  调用接口,post形式  (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=191823)

--  作者:bzqlyj
--  发布时间:2024/5/10 10:49:00
--  调用接口,post形式

使用以下代码,运行错误,未能创建 SSL/TLS 安全通道。如何处理


Dim name As String="张三"
Dim id As String="abc"
Dim msg As String = "username=" & name &"&idcard=" & id 
msgbox(msg)
Dim req = System.Net.WebRequest.Create("https://certificate/query_json.xhtml")
req.Method = "POST"
req.Timeout = 5000
req.ContentType = "application/x-www-form-urlencoded; charset=UTF-8"
req.UserAgent = "Mozilla/5.0 (Windows NT 6.3; rv:66.0) Gecko/20100101 Firefox/66.0"
Dim aryBuf As Byte() = Encoding.GetEncoding("utf-8").GetBytes(msg)
req.ContentLength = aryBuf.Length
Dim writer = req.GetRequestStream()
writer.Write(aryBuf, 0, aryBuf.Length)
writer.Close()
writer.Dispose()
Dim 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)


--  作者:有点蓝
--  发布时间:2024/5/10 11:00:00
--  
Net.ServicePointManager.SecurityProtocol = Net.SecurityProtocolType.Tls11 \'不行就Tls12、Tls13都试一遍
Dim hc As New HttpClient("https://certificate/query_json.xhtml")
hc.UserAgent = "Mozilla/5.0 (Windows NT 6.3; rv:66.0) Gecko/20100101 Firefox/66.0"
hc.ContentType = "application/x-www-form-urlencoded"
hc
.Timeout = 5 \'设置超时为5秒
hc.FormData.Add("username",name )
hc.FormData.Add("idcard",id)
Dim ret As String = hc.GetData()
MessageBox
.Show(ret)

--  作者:bzqlyj
--  发布时间:2024/5/10 11:11:00
--  回复:(有点蓝)Net.ServicePointManager.SecurityPr...
用以上Net.ServicePointManager.SecurityProtocol = Net.SecurityProtocolType.Tls1 
运行错误,必须先将 ContentLength 字节写入请求流,然后再调用 [Begin]GetResponse。
Net.ServicePointManager.SecurityProtocol = Net.SecurityProtocolType.Tls13  此处修改为Tls12、Tls13,远程服务器返回错误: (415) Unsupported Media Type。

--  作者:有点蓝
--  发布时间:2024/5/10 11:17:00
--  
https://www.baidu.com/s?wd=System.Net.WebRequest%20ssl