WinJS xhr可以发出HTTPS请求吗?

时间:2017-04-04 01:01:33

标签: javascript win-universal-app winjs windows-10-universal

例如,它需要调用托管SSL的Web服务。

如果可以,那么如何传递客户端证书?

非常感谢!!

      WinJS.xhr({
                    type: "GET",
                    url: "https://localhost:442/WebService1.asmx?op=Login",

                }).then(function success(res) {                       
                    var debug1 = res.responseText + res.responseURL; 

                }, function error(err) {
                    var debug2 = err.responseText + err.responseURL;                        
                }, function completed(result) {
                    if (result.status === 200) {                       
                      // do something
                    }
                });

调试点将跳转到“完成(结果)'功能,但状态代码为' 0'。即使我将网址更改为其他https网站(例如https://www.w3.org),结果也是一样。

-------------更新1 ---------------------

如果它在C#中我可以使用以下代码来传递客户端证书。但是,如果我想将原始WinJs.xhr更改为HttpClient,只需复制&粘贴似乎不起作用.js文件无法理解所有语法?

var certQuery = new CertificateQuery();     
var cert = (await CertificateStores.FindAllAsync(certQuery)).FirstOrDefault(c=>c.Issuer.StartsWith("xxxx",StringComparison.CurrentCultureIgnoreCase));
var filter = new HttpBaseProtocolFilter();
if (cert != null)  
{
filter.ClientCertificate = cert; 
filter.IgnorableServerCertificateErrors.Add(ChainValidationResult.Untrusted | ChainValidationResult.InvalidName);
}                    

var hc = new Windows.Web.Http.HttpClient(filter);
var uri = new Windows.Foundation.Uri(url);
hc.getStringAsync(uri).done({.......});

E.g。

1)如何写'使用....'在JS文件中?

2)如何使用" await"或"' FindAllAsync'"在这一行?等。

var cert = (await CertificateStores.FindAllAsync(certQuery)).FirstOrDefault(c=>c.Issuer.StartsWith("xxxx",StringComparison.CurrentCultureIgnoreCase));

1 个答案:

答案 0 :(得分:1)

#!/bin/bash -l # chkconfig: 2345 20 80 if [ "$system" = "redhat" ]; then ## source platform specific external scripts . /etc/init.d/functions ## set or override platform specific variables lockfile=${LOCKFILE-/var/lock/subsys/$SERVICE_NAME} ## set or override platform specific functions start_daemon() { daemon --user $1 --pidfile $2 "$3 $4" } log_daemon_msg() { echo -n $"$1" } echo_ok() { echo_success; echo } echo_fail() { echo_failure; echo } log_success_msg() { success $"$@" } log_failure_msg() { failure $"$@" echo $"$@" } log_action_msg() { echo $@ } fi if [ -n "$http_proxy" ] ; then echo "proxy found" echo $http_proxy | grep "@" if [ $? -eq 0 ]; then PROXY_HOST=$(echo $http_proxy | sed 's/http:\/\/.*@\(.*\):.*/\1/') PROXY_PORT=$(echo $http_proxy | sed 's/http:\/\/.*@.*:\(.*\)/\1/' | tr -d "/") USERNAME=$(echo $http_proxy | sed 's/http:\/\/\(.*\)@.*/\1/'|awk -F: '{print $1}') PASSWORD=$(echo $http_proxy | sed 's/http:\/\/\(.*\)@.*/\1/'|awk -F: '{print $2}') else PROXY_HOST=$(echo $http_proxy | sed 's/http:\/\/\(.*\):.*/\1/') PROXY_PORT=$(echo $http_proxy | sed 's/http:\/\/.*:\(.*\)/\1/' | tr -d "/") fi fi start() { echo "Starting $SERVICE_NAME ..." if [ ! -f $PID_PATH_NAME ]; then CMD="java " if [ -n "$PROXY_HOST" -a -n "$PROXY_PORT" ] ; then CMD="java -Dhttps.proxyHost=$PROXY_HOST -Dhttps.proxyPort=$PROXY_PORT" if [ -n "$USERNAME" -a -n "$PASSWORD" ]; then CMD="$CMD -Dhttps.proxyUser=$USERNAME -Dhttps.proxyPassword=$PASSWORD" fi fi #nohup $CMD -jar $PATH_TO_JAR /tmp 2>> /dev/null >> /dev/null & #echo "$PATH_TO_JAR" echo "$CMD" nohup $CMD -jar $PATH_TO_JAR 2>> /tmp/glog >> /tmp/glog & echo $! > $PID_PATH_NAME echo "$SERVICE_NAME started ..." else echo "$SERVICE_NAME is already running ..." fi } case "$1" in start) start ;; *) echo "Usage: $0 {start|stop|status|restart}" esac exit 0 WinJS.xhrhttps://msdn.microsoft.com/en-us/library/windows/apps/br229787.aspx)与XMLHttpRequest类似的界面(WinJS Promise,而不是ES6 Promise包裹起来,但是概念类似)。

Promise具有XMLHttpRequest属性,允许您指定是否应发送客户端证书(包括客户端证书) - 但是没有允许您指定的API应该使用哪个特定的客户端证书。

幸运的是,WinJS公开了withCredentials类型,它可以让您更好地控制客户端身份验证,包括客户端证书 - 但您的UWP应用程序必须具有"企业功能"使用用户的Windows.Web.Http.HttpClient证书存储区 - 否则非企业UWP应用程序只在其应用程序证书存储区中有证书:

https://blogs.windows.com/buildingapps/2015/11/23/demystifying-httpclient-apis-in-the-universal-windows-platform/#Dr3C9IMHv5pTPOrB.97

  

您必须先按照these instructions将其添加到应用的证书商店。具有企业功能的应用程序还可以使用用户“我的”商店中的现有客户端证书。