Pregunta

Estoy jugando con Silverlight 4, y cuando mis cargas de la página, me llaman

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

que he definido como

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));
}

y continueLoadStamData ()

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

Mi problema es que cuando llego a e.Result, se produce una excepción. Es la misma excepción consigo como cuando traté de usar 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)}

¿Tiene usted alguna idea de lo que está pasando, cómo puedo asegurarse de autenticación básica se lleva a cabo y obtener mi solicitud va?

Saludos

Nik

¿Fue útil?

Solución

Con base en el post de marca Monster aquí que se está perdiendo algunas líneas de código en su método beginGet. Debe ser algo como:

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));
}

Además, si usted está tratando de obtener JSON desde el servidor, debe ser capaz de utilizar DownloadStringAsync en lugar de OpenReadAsync que podría simplificar las cosas.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top