大家好我正在尝试使用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:
答案 0 :(得分:0)
我知道这已经很晚了,但我挣扎了很长一段时间,这让我的脑袋绕错了方向......但最终我做了以下的工作,这真的让我很烦恼,这就是问题
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + kvp.Value);
//client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", kvp.Value );