0

我目前正在为 VLC 媒体播放器编写遥控器。我使用 http-web 接口连接和控制服务器。由于版本 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 试图显示弹出窗口,但失败了。再说一次,超时应该开始吗?

你们中有人有同样的问题吗?如果有,你们是如何解决的?

4

1 回答 1

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]);
于 2013-11-30T22:42:25.703 回答