0

大家好,我正在尝试使用 silverlight 5 发送 getasync 请求。后异步工作没有任何问题,但是 getasync 在添加授权标头时抛出该异常。当不添加授权标头时,这无一例外只是 401。我应该怎么做才能修复它?

编码:

    client.DefaultRequestHeaders.Add("Authorization", "Bearer secrettoken");

var getasynctask = await client.GetAsync("http://api.enteroption.com/api/UserDetails/GetUserIdByToken/");

例外:

System.ArgumentException occurred
  Message=""
  StackTrace:
       at Microsoft.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
       at Microsoft.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccess(Task task)
       at Microsoft.Runtime.CompilerServices.TaskAwaiter.ValidateEnd(Task task)
       at Microsoft.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
       at ConsoleApplication3.Class1.<Func>d__1.MoveNext()
  InnerException: System.ArgumentException
       Message=Value does not fall within the expected range.
       StackTrace:
            at System.Net.Browser.BrowserHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult)
            at System.Net.Browser.BrowserHttpWebRequest.<>c__DisplayClassa.<EndGetResponse>b__9(Object sendState)
            at System.Net.Browser.AsyncHelper.<>c__DisplayClass4.<BeginOnUI>b__0(Object sendState)
       InnerException:
4

1 回答 1

0

我知道这已经晚了,但是我挣扎了很长一段时间,这让我的头绕错了方向..但最终我做了以下事情并且它奏效了,这真的让我很恼火这是问题

  client.DefaultRequestHeaders.Add("Authorization", "Bearer " + kvp.Value);
  //client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", kvp.Value );
于 2016-07-26T13:20:16.693 回答