Windows Phone 7, download xml over ssl with authen

2020-07-23 06:11发布

I'm trying to download a file from my provider.

The url is protected with basic username and password, and everything is sent over ssl.

So I try to do this:

        WebClient proxy = new WebClient();

        proxy.DownloadStringCompleted += (o, dscea) => System.Diagnostics.Debugger.Break();
        proxy.Credentials = new NetworkCredential("username", "password");
        proxy.DownloadStringAsync(new Uri("https://..../.../data.xml"));

As you can see I try to validate. The url is correct, and the code works when I try to download something from twitter.

And the URL works when I type it in in Firefox / Internet Explorer

What am I forgetting to connect to this xml file?

The error I get is the following:

And I am using Visual Studio 2010 (the full, not Express), and the CTP refresh :)

2条回答
Melony?
2楼-- · 2020-07-23 06:28

Apparently it goes wrong when you try to do an SSL request. Authentication over SSL is not supported in Silverlight (throws a not notimplementedException) in REGULAR Silverlight.

So I'm pretty sure this is the same problem in WP7.

查看更多
smile是对你的礼貌
3楼-- · 2020-07-23 06:43

This appears to be fixed in the Beta tools release. I had to set the Authorization header directly though as .NET doesn't handle basic auth the way you might expect. Here's my working code snippet:

var client = new WebClient();

var token = Convert.ToBase64String(Encoding.UTF8.GetBytes(string.Format("{0}:{1}", username, password)));
var authHeader = string.Format("Basic {0}", token);

client.Headers["Authorization"] = authHeader;
client.DownloadStringCompleted += (s, e) =>
{
   // handle result
};

client.DownloadStringAsync(url);
查看更多
登录 后发表回答