我玩的Silverlight 4,我当我的页面加载,我称之为

beginGet("my/people/", new OpenReadCompletedEventHandler(continueLoadStamData));

,我已定义为

private void beginGet(string endpoint, OpenReadCompletedEventHandler callback)
{
  WebClient wc = new WebClient();
  wc.Credentials = new NetworkCredential(username, password);
  wc.OpenReadCompleted += callback;
  wc.OpenReadAsync(new Uri(baseURL + endpoint));
}

和continueLoadStamData()

void continueLoadStamData(object sender, OpenReadCompletedEventArgs e)
{
  JsonObject root = (JsonObject)JsonObject.Load(e.Result);
}

我的问题是,当我到达e.Result,它抛出一个异常。这是相同的例外,我得到当我试图用WebRequest req = ...; req.Credentials = new NetworkCredential(username, password)

{System.Reflection.TargetInvocationException: An exception occurred during the operation, making the result invalid.  Check InnerException for exception details. ---> System.Net.WebException: An exception occurred during a WebClient request. ---> System.NotImplementedException: This property is not implemented by this class.
   at System.Net.WebRequest.set_Credentials(ICredentials value)
   at System.Net.WebClient.GetWebRequest(Uri address)
   at System.Net.WebClient.OpenReadAsync(Uri address, Object userToken)
   --- End of inner exception stack trace ---
   --- End of inner exception stack trace ---
   at System.ComponentModel.AsyncCompletedEventArgs.RaiseExceptionIfNecessary()
   at System.Net.OpenReadCompletedEventArgs.get_Result()
   at JSONSample.MainPage.continueLoadStamData(Object sender, OpenReadCompletedEventArgs e)
   at System.Net.WebClient.OnOpenReadCompleted(OpenReadCompletedEventArgs e)
   at System.Net.WebClient.OpenReadOperationCompleted(Object arg)}

你有什么事情,我怎么能保证基本的身份验证实施,并得到了我的要求去的任何想法?

干杯

的Nik

有帮助吗?

解决方案

根据马克怪物张贴在这里你错过了一些代码在你beginGet方法。应该是这样的:

private void beginGet(string endpoint, OpenReadCompletedEventHandler callback)
{
  WebRequest.RegisterPrefix("http://", System.Net.Browser.WebRequestCreator.ClientHttp);  
  WebClient wc = new WebClient();  
  wc.Credentials = new NetworkCredential(username, password);
  wc.UseDefaultCredentials = false; 
  wc.OpenReadCompleted += callback;  
  wc.OpenReadAsync(new Uri(baseURL + endpoint));
}

此外,如果你只是想从服务器获取JSON,你应该能够使用DownloadStringAsync代替OpenReadAsync这可能会简化事情。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top