【发布时间】:2021-01-14 05:22:04
【问题描述】:
我想通过 httpClient 将 json 值发布到 API 中。不幸的是,我是一个初学者,无法处理它。 GET 完美运行: 这里是 GET 的代码:
Dim request As HttpWebRequest
Dim response As HttpWebResponse = Nothing
Dim reader As StreamReader
' Wird für https-Requests benötigt
System.Net.ServicePointManager.SecurityProtocol = DirectCast(3072, SecurityProtocolType)
request = DirectCast(WebRequest.Create(url), HttpWebRequest)
response = DirectCast(request.GetResponse(), HttpWebResponse)
reader = New StreamReader(response.GetResponseStream())
Dim s As String
s = reader.ReadToEnd
Return s
但是我如何实现 POST 呢? 我的 API-URL 如下所示: https://mydomaine.net/api/auth?token=467445892587458542..。 我将发送一个 JSON 字符串 {"test":"value1":"test1":"value2"} 我已经为这个问题工作了好几个小时。 有人可以帮忙吗???
【问题讨论】:
-
设置
request.ContentType = "application/json"、request.Method = WebRequestMethods.Http.Post并将 JSON 作为 UTF-8 字节写入请求流 (request.GetRequestStream())。此 API 是否有任何与发布数据相关的文档? -- 为什么要将 SecurityProtocolType 转换为Tls12Enum 值?您还在使用 4.5 之前的 .Net Framework 版本吗?是时候更新了。 -
非常感谢您的帮助。我现在有一个可行的解决方案。