Windows Phone 7, download xml over ssl with authen

2020-07-23 06:42发布

问题:

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:

  • [System.Net.WebException] {"[HttpWebRequest_WebException_RemoteServer]\r\nArguments:NotFound\r\nDebugging resource strings are unavailable. Often the key and arguments provide sufficient information to diagnose the problem. See http://go.microsoft.com/fwlink/?linkid=106663&Version=3.7.50308.0&File=System.Windows.dll&Key=HttpWebRequest_WebException_RemoteServer "} System.Net.WebException

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

回答1:

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


回答2:

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.