提问人:LukaGer 提问时间:9/4/2023 更新时间:9/4/2023 访问量:53
错误 401:未经授权将 json 数据发布到 URL
Error 401 Unauthorized in posting json data to a URL
问:
我必须将 json 发送到 url 以更新数据。 我使用了我在网络上找到的一些代码,我更改了它,但我从服务器中出错: 错误 401 未经授权。
这是我的代码:
Dim jsonPost As New JsonPost("https://url/api/UpdateData")
jsonPost.postData()
Public Class JsonPost
Private urlToPost As String = ""
Public Sub New(ByVal urlToPost As String)
Me.urlToPost = urlToPost
End Sub
Public Function postData() As Boolean
Dim _user As String = "USER"
Dim _password As String = "PASSWORD"
Dim _json As String = ""
_json = "{ ""Dato"":""AAA"""
_json &= " "",""Comm"": [ "
_json &= "{ ""Code"":""1"", ""Descr"":""Des1""} "
_json &= "]}"
ServicePointManager.SetTcpKeepAlive(True, 100000, 100000)
ServicePointManager.Expect100Continue = True
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12
ServicePointManager.DefaultConnectionLimit = 100000000
Dim webClient As New WebClient()
Dim resByte As Byte()
Dim resString As String
Dim reqString() As Byte
Try
webClient.Credentials = New System.Net.NetworkCredential(_user, _password, "https://DOMAIN")
reqString = System.Text.Encoding.Unicode.GetBytes(_json)
webClient.Headers("content-type") = "application/json"
reqString = Encoding.Default.GetBytes(JsonConvert.SerializeObject(_json, Formatting.Indented))
resByte = webClient.UploadData(Me.urlToPost, "post", reqString)
resString = Encoding.Default.GetString(resByte)
Console.WriteLine(resString)
webClient.Dispose()
Return True
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Return False
End Function
End Class
我可以检查什么? 谢谢
答: 暂无答案
评论