积极答复者
请教silverlight4 网络通信的问题

问题
-
public static string DoGet(string url, string account, string pwd)
{
string usernamePassword = account + ":" + pwd;
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
req.Method = "GET";
req.ContentType = "application/x-www-form-urlencoded;charset=utf-8";CredentialCache myCache = new CredentialCache();
myCache.Add(new Uri(url), "Basic", new NetworkCredential(account, pwd));
req.Credentials = myCache;
req.Headers.Add("Authorization", "Basic " + Convert.ToBase64String(new ASCIIEncoding().GetBytes(usernamePassword)));using (HttpWebResponse rsp = (HttpWebResponse)req.GetResponse())
{
string charSet = rsp.CharacterSet;
if (charSet == null || charSet.Length == 0)
{
charSet = "UTF-8";
}
Encoding encoding = Encoding.GetEncoding(charSet);
return GetResponseAsString(rsp, encoding);
}
}上面的方法,用来在c#.net上来与http 服务通信没有问题,但是在silverlight4 平台上无法正确使用,请教大家
在silverlight4平台上实现该功能,应该怎么实现2010年11月8日 15:11
答案
-
谢谢你提供这个视频,但这个视频演示的是silverlight作为客户端软件来运行的方案(不是运行于浏览器),这个方案的源代码如下
WebRequest.RegisterPrefix("http://", System.Net.Browser.WebRequestCreator.ClientHttp);
WebClient wc = new WebClient();
string username = "zhyiix7878@126.com";
string password = "wuuwwww";
string usernamePassword = username + ":" + password;
wc.Credentials = new NetworkCredential(username, password);
wc.Headers["Authorization"] = "Basic " + Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(usernamePassword));string url =http://api.t.sina.com.cn/statuses/friends.xml;
wc.DownloadStringAsync(new Uri(url), url);
现在我是想实现sivlerlight运行于浏览器,现在如果还采用上面的源码,则报错,错误信息如下:
"不支持自定义凭据"主要是wc.Credentials = new NetworkCredential(username, password);出的错,不知道如何解决.
- 已标记为答案 逍遥客 2010年11月20日 10:34
2010年11月15日 15:40
全部回复
-
WebClient wc = new WebClient();
string username = "zhyiix7878@126.com";
string password = "wuuwwww";
string usernamePassword = username + ":" + password;
wc.Credentials = new NetworkCredential(username, password);
wc.Headers["Authorization"] = "Basic " + Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(usernamePassword));string url =http://api.t.sina.com.cn/statuses/friends.xml;
wc.DownloadStringAsync(new Uri(url), url);
上面的代码是用WebClient 写的,但不知道为什么,总是报错,好像是报的 wc.Credentials = new NetworkCredential(username, password)的错,错误信息为
"没有实现该方法,WebClient 类出现异常",请高手给看看
2010年11月9日 5:13 -
http://www.silverlightchina.net/html/zhuantixilie/winphone7/2010/1022/2827.html
这里有一篇关于wp7如何开发新微的演示。它与sl是一样的。
另外还可以到mix09大会的网站查看关于webclient的视频,我记得有一个视频就有演示如何用silverlight4与twitter登陆的。
2010年11月9日 5:28 -
http://www.silverlightchina.net/html/zhuantixilie/winphone7/2010/1022/2827.html 这个地址的我也看了,但是在执行 wc.Credentials = new NetworkCredential(username, password);(注:wc 是WebClient 的实例)出错,提示该类没有实现这一属性.mix09大会的网站查看关于webclient的视,一直没有找到,麻烦各位,请帮助一下,不胜感激,
2010年11月13日 9:25 -
谢谢你提供这个视频,但这个视频演示的是silverlight作为客户端软件来运行的方案(不是运行于浏览器),这个方案的源代码如下
WebRequest.RegisterPrefix("http://", System.Net.Browser.WebRequestCreator.ClientHttp);
WebClient wc = new WebClient();
string username = "zhyiix7878@126.com";
string password = "wuuwwww";
string usernamePassword = username + ":" + password;
wc.Credentials = new NetworkCredential(username, password);
wc.Headers["Authorization"] = "Basic " + Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(usernamePassword));string url =http://api.t.sina.com.cn/statuses/friends.xml;
wc.DownloadStringAsync(new Uri(url), url);
现在我是想实现sivlerlight运行于浏览器,现在如果还采用上面的源码,则报错,错误信息如下:
"不支持自定义凭据"主要是wc.Credentials = new NetworkCredential(username, password);出的错,不知道如何解决.
- 已标记为答案 逍遥客 2010年11月20日 10:34
2010年11月15日 15:40