可能的身份验证问题?在 Silverlight 4 中通过 WebClient 加载 JSON
我正在使用 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(用户名, 密码):
{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)}
您知道发生了什么吗?我如何确保实施基本身份验证并让我的请求继续进行?
干杯
尼克
I'm playing with Silverlight 4, and I when my page loads, I call
beginGet("my/people/", new OpenReadCompletedEventHandler(continueLoadStamData));
that I have defined as
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));
}
and continueLoadStamData()
void continueLoadStamData(object sender, OpenReadCompletedEventArgs e)
{
JsonObject root = (JsonObject)JsonObject.Load(e.Result);
}
My problem is that when I get to e.Result, it throws an exception. It is the same exception I get as when I tried to use 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)}
Do you have any idea of what's going on, how I can make sure basic authentication is implemented and get my request going?
Cheers
Nik
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
基于Mark Monster 在此发布的帖子 您的 beginGet 方法中缺少一些代码行。它应该类似于:
另外,如果您只是尝试从服务器获取 JSON,则应该能够使用 DownloadStringAsync 而不是 OpenReadAsync,这可能会简化事情。
Based on Mark Monster's post here you're missing some lines of code in your beginGet method. It should be something like:
Also, if you're just trying to get JSON from the server, you should be able to use DownloadStringAsync instead of OpenReadAsync which might simplify things.