2017-04-25 64 views
0

你好,我有這行代碼我試圖分裂從HTTP readerGetResponse流得到令牌,長度不能小於零。參數名稱:長度在試圖分裂令牌

whle打破它顯示了這個錯誤代碼「長度不能小於零參數名:長度試圖分裂令牌」

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") 

這裏我強調是哪裏出錯時。謝謝

回答

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