长度不能小于零。参数名称:尝试拆分令牌时的长度

时间:2017-04-25 11:48:30

标签: asp.net vb.net visual-studio

你好我有这行代码试图拆分从http readerGetResponse流中获取的标记,

打破代码时显示此错误"长度不能小于零。参数名称:尝试拆分令牌时的长度"

Dim tokens As New Dictionary(Of String, String)()
        Dim url As String = String.Format("https://graph.facebook.com/oauth/access_token?client_id={0}&redirect_uri={1}&scope={2}&code={3}&client_secret={4}", app_id, "http://localhost:28782/webform2.aspx", scope, Me.Request("code").ToString(), app_secret)
        Dim request As HttpWebRequest = TryCast(WebRequest.Create(url), HttpWebRequest)


        Using response As HttpWebResponse = TryCast(Request.GetResponse(), HttpWebResponse)
            Dim reader As New StreamReader(response.GetResponseStream())
            Dim vals As String = reader.ReadToEnd()
            For Each token As String In vals.Split("&"c)
                **tokens.Add(token.Substring(0, token.IndexOf("=")), token.Substring(token.IndexOf("=") + 1, token.Length - token.IndexOf("=") - 1))**
            Next
        End Using
        Dim access_token As String = tokens("access_token")
        Context.Session("AccessToken") = access_token
        Response.Redirect("http://localhost:28782/allproducts.aspx")

我突出显示的是错误的位置。谢谢

1 个答案:

答案 0 :(得分:0)

检查=是否存在&处理长度,如果它在另一个答案的评论中提到的第一个位置,它会处理它,尝试类似:

For Each token As String In vals.Split("&"c)
    dim i as integer = token.IndexOf("=")
    if (i <> -1) Then
        dim newLength as integer = token.length - i
        if newLength > 0 then
           newLength -= 1
        end if
        tokens.Add(token.Substring(0, i), token.Substring(i + 1, newLength))**
     end if
Next