WP8 C#通过SSL与Web服务器通信

时间:2014-02-15 17:10:04

标签: c# .net ssl windows-phone-8 windows-phone

我一直在努力让我的C#Windows Phone 8通过SSL(HTTPS)与我的PHP Web服务器通信。我的服务器实现的方式是它读取发送给它的GET参数,然后回显适当的响应。它已经在Windows Phone互联网浏览器中正常工作,转到https://sub.mysite.com/mypage.php?param=value,但对于我这样的人,我无法使用我的应用程序。

现在我应该提一下,服务器有一个自签名证书,我已将其包含在我的应用程序中,首次启动时,我提示用户使用以下命令安装证书:

await Launcher.LaunchFileAsync(await Package.Current.InstalledLocation.GetFileAsync("server.cer"));

这似乎工作正常,因为在安装证书后,设备上的Internet Explorer不再警告我有关未经认证的证书。

事情是,尝试通过WebClient或WebRequest访问服务器都会给我这个错误:

An exception of type 'System.Net.WebException' occurred in System.Windows.ni.dll and wasn't handled before a managed/native boundary An exception of type 'System.Net.WebException' occurred in System.Windows.ni.dll and wasn't handled before a managed/native boundary 'TaskHost.exe' (CLR C:\windows\system32\coreclr.dll: Silverlight AppDomain): Loaded 'C:\windows\system32\en-US\System.debug.resources.DLL'. Module was built without symbols. A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in System.ni.dll An exception of type 'System.Reflection.TargetInvocationException' occurred in System.ni.dll and wasn't handled before a managed/native boundary System.Reflection.TargetInvocationException: An exception occurred during the operation, making the result invalid. Check InnerException for exception details. ---> System.Net.WebException: The remote server returned an error: NotFound. ---> System.Net.WebException: The remote server returned an error: NotFound. at System.Net.Browser.ClientHttpWebRequest.InternalEndGetResponse(IAsyncResult asyncResult) at System.Net.Browser.ClientHttpWebRequest.<>c_DisplayClasse.b_d(Object sendState) at System.Net.Browser.AsyncHelper.<>c_DisplayClass1.b_0(Object sendState) --- End of inner exception stack trace --- at System.Net.Browser.AsyncHelper.BeginOnUI(SendOrPostCallback beginMethod, Object state) at System.Net.Browser.ClientHttpWebRequest.EndGetResponse(IAsyncResult asyncResult) at System.Net.WebClient.GetWebResponse(WebRequest request, IAsyncResult result) at System.Net.WebClient.DownloadBitsResponseCallback(IAsyncResult result) --- End of inner exception stack trace --- at System.ComponentModel.AsyncCompletedEventArgs.RaiseExceptionIfNecessary() at System.Net.DownloadStringCompletedEventArgs.get_Result() at HttpsDing.MainPage.client_DownloadStringCompleted(Object sender, DownloadStringCompletedEventArgs e)

The implementation for both of them though work fine when accessing a normal site like http://bing.com or even https://bing.com.

The code for the webclient method is:

WebClient client = new WebClient();
client.DownloadStringAsync(new Uri("https://sub.mysite.com"));
client.DownloadStringCompleted += client_DownloadStringCompleted;

And the code for the webrequest is:

public static class Extensions
{
    public static System.Threading.Tasks.Task<System.IO.Stream> GetRequestStreamAsync(this System.Net.WebRequest wr)
    {
        if (wr.ContentLength < 0)
        {
            throw new InvalidOperationException("The ContentLength property of the WebRequest must first be set to the length of the content to be written to the stream.");
        }

        return Task<System.IO.Stream>.Factory.FromAsync(wr.BeginGetRequestStream, wr.EndGetRequestStream, null);
    }

    public static System.Threading.Tasks.Task<System.Net.WebResponse> GetResponseAsync(this System.Net.WebRequest wr)
    {
        return Task<System.Net.WebResponse>.Factory.FromAsync(wr.BeginGetResponse, wr.EndGetResponse, null);
    }
}

And then:

WebClient client = new WebClient();
client.DownloadStringAsync(new Uri("https://sub.mysite.com"));
client.DownloadStringCompleted += client_DownloadStringCompleted;

这对我来说有点混乱,有什么想法吗?

0 个答案:

没有答案