Ajax头基本身份验证:请求超时无法在wp8上运行

时间:2013-10-24 23:38:11

标签: jquery ajax cordova windows-phone-8

我目前正在为VLC媒体播放器编写遥控器。我使用http-webinterface连接并控制服务器。从版本2.1.0开始,VLC需要设置密码。这本身不是问题。我用以下Ajax-Request

解决了这个问题
checkConnection = function(id, folder){
$.ajax({
    url: 'http://' + data.ip + ":" + data.port + '/requests/status.xml',
    headers: {
        "Authorization" : "Basic " + data.authorization
    },
    timeout: 3000,
    success: function (data, status, jqXHR) {
        //Yeah do stuff
        }       
    },
    error: function(data){
        //Ohh, do stuff
    }
  });
};

如果我使用计算机连接到VLC http接口,则会出现此标准弹出窗口,询问用户名和密码。我现在的问题是,如果data.authorization中的令牌错误,应用程序(使用手机)崩溃。如果使用Ripple进行测试(使用Chrome),则会显示上述弹出窗口,但是超时工作正常,我的错误处理就开始了。在我的Windows Phone上并非如此 - 这里我的应用程序挂起(如上所述)。我怀疑,因为它是一个webview WP尝试显示弹出窗口,但失败了。然后,超时应该开始?

你们有没有遇到同样的问题,如果有的话你是怎么解决的?

1 个答案:

答案 0 :(得分:0)

终于解决了。这很简单,只需要在C#中编写插件。我会为可能遇到同样问题的人附上代码。

using System;
using System.IO;
using System.Net;
using System.Runtime.Serialization;
using System.Text;
using System.Windows.Threading;
using WPCordovaClassLib.Cordova;

namespace WPCordovaClassLib.Cordova.Commands
{
    public class BasicAuth : BaseCommand
    {
        //Create timer to control timeout
        DispatcherTimer timeoutTimer = new DispatcherTimer();
        WebClient webClient = new WebClient();

    public BasicAuth(){
        timeoutTimer.Interval = TimeSpan.FromSeconds(5);
        timeoutTimer.Tick += new EventHandler(timeout);
        timeoutTimer.Start();
    }

    public void get(string options)
    {
        //Parse data that gets passed into the plugin
        string[] passedData = JSON.JsonHelper.Deserialize<string[]>(options);

        string ip = passedData[0];
        string port = passedData[1];
        string username = passedData[2];
        string password = passedData[3];            

        try
        {
            webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);
            string credentials = String.Format("{0}:{1}", username, password);
            byte[] bytes = Encoding.UTF8.GetBytes(credentials);
            string base64 = Convert.ToBase64String(bytes);
            string authorization = String.Concat("Basic ", base64);
            webClient.Headers["Authorization"] = authorization;
            string url = //your url here
            var uri = new Uri(url);
            webClient.DownloadStringAsync(uri);
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.WriteLine(ex.Data);
            DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, ""));
            timeoutTimer.Stop();
        }
    }

    void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        try{
            DispatchCommandResult(new PluginResult(PluginResult.Status.OK, e.Result)); //e.Result will fail if the server couldn't be contacted
        } catch{
            DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR, ""));
        }
    }

    private void timeout(Object sender, EventArgs e)
    {
        webClient.CancelAsync(); //Cancel Async download
        timeoutTimer.Stop(); //Stop timer from beeing executed again
    }
}
}

以下是您从JavaScript调用的位:

cordova.exec(connectionSuccess, connectionError, "BasicAuth", "get", [data]);