当我执行此PUT调用(vb.net)时,我收到“400”错误。
我做错了什么?
Dim wRequest As HttpWebRequest = DirectCast(HttpWebRequest.Create("https://localhost:8444/api/route/1802/state"), HttpWebRequest)
wRequest.Method = "PUT"
wRequest.ContentType = "text/plain"
Dim stringData As String = "STOP"
Dim data = Encoding.ASCII.GetBytes(stringData)
wRequest.ContentLength = data.Length
Dim newStream = wRequest.GetRequestStream()
newStream.Write(data, 0, data.Length)
newStream.Close()
wResponse = DirectCast(wRequest.GetResponse(), HttpWebResponse)
最后一行抛出400。
答案 0 :(得分:0)
X-CSRF-Token丢失了。
执行GET并存储响应中的CSRF令牌。
If(String.IsNullOrWhiteSpace(_csrfToken))
_csrfToken = wResponse.Headers("X-CSRF-Token")
End If
在" GetResponse"
之前将令牌添加到PUT请求If(not String.IsNullOrWhiteSpace(_csrfToken))
wRequest.Headers("X-CSRF-Token") = _csrfToken
End If
注意:X-CSRF-Token具有会话的生命周期。