从Vb.net 4.0访问经过身份验证的REST API

时间:2015-10-13 13:18:52

标签: .net vb.net api rest

我正在尝试从vb.net登录REST api。

Dim pageSource As String
    Dim formUrl As String = "http://10.23.202.18:8080/usermgmt-0.1.0.BUILD-SNAPSHOT/resources/j_spring_security_check"
    ' NOTE: This is the URL the form POSTs to, not the URL of the form (you can find this in the "action" attribute of the HTML's form tag
    Dim formParams As String = String.Format("j_username={0}&j_password={1}", "administrator", "admin")
    Dim cookieHeader As String
    Dim req As WebRequest = WebRequest.Create(formUrl)
    req.ContentType = "application/x-www-form-urlencoded"
    req.Method = "POST"
    Dim bytes As Byte() = Encoding.ASCII.GetBytes(formParams)
    req.ContentLength = bytes.Length
    Using os As Stream = req.GetRequestStream()
        os.Write(bytes, 0, bytes.Length)
    End Using
    Dim resp As WebResponse = req.GetResponse()
    cookieHeader = resp.Headers("Set-cookie")

    Using sr As New StreamReader(resp.GetResponseStream())
        pageSource = sr.ReadToEnd()
    End Using

但作为回应我得到resp.Headers(“Set-cookie”),resp.Headers(“Location”)为空并且在响应url 变得像 您已尝试访问此应用程序的受保护区域。默认情况下,您可以以“admin”身份登录,密码为“admin” 所以请帮我从这个

出来

0 个答案:

没有答案