2017-04-25 28 views
0

當我執行此PUT調用(vb.net)時,出現「400」錯誤。如何構建需要CSRF令牌的有效PUT休息調用

我在做什麼錯了?

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

this call doc

回答

0

X-CSRF令牌丟了。

  1. 做一個GET並存儲來自響應的CSRF令牌。

    If(String.IsNullOrWhiteSpace(_csrfToken)) 
        _csrfToken = wResponse.Headers("X-CSRF-Token") 
    End If 
    
  2. 前 「的GetResponse」

    If(not String.IsNullOrWhiteSpace(_csrfToken)) 
        wRequest.Headers("X-CSRF-Token") = _csrfToken 
    End If 
    

添加令牌PUT請求注意:X-CSRF令牌具有會話的壽命。