【问题标题】:Making non-secure http requests in Android nativescript在 Android nativescript 中发出不安全的 http 请求
【发布时间】:2017-05-12 07:34:55
【问题描述】:

我正在尝试从 nativescript 向常规 http(不是 https)服务器发出 http 请求。

    let headers = new Headers();
    headers.append("Content-Type", "application/json");

    this.http.request(
        "http://localhost:3050/register", 
        {
            method: "POST",
            headers: headers,
            body: JSON.stringify({
                username: user.username,
                email: user.email,
                password: user.password
            })
        })

当向 https 服务器发出请求时,该请求运行良好,但在向 http 服务器(即我当前正在运行的服务器)发出时,它将无法正常工作。

在 iOS 中,我必须在 info.plist 文件中添加以下内容以允许对 http 服务器的请求,然后它就可以正常工作了:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>

但是,在 Android 中,请求仍然无法正常工作,并且我找不到任何有关我需要进行更改以使请求工作到 http 服务器的文档。该请求甚至没有到达服务器。

感谢任何帮助。

谢谢。

【问题讨论】:

    标签: javascript android angular mobile nativescript


    【解决方案1】:

    我修改了位于app\App_Resources\Android\src\mainAndroidManifest.xml from

        <application
            android:name="com.tns.NativeScriptApplication"
            android:allowBackup="true"
            android:icon="@drawable/icon"
            android:label="@string/app_name"
            android:theme="@style/AppTheme">
    

        <application
            android:name="com.tns.NativeScriptApplication"
            android:allowBackup="true"
            android:icon="@drawable/icon"
            android:label="@string/app_name"
            android:theme="@style/AppTheme"
            android:usesCleartextTraffic="true">
    

    我添加了最后一行:android:usesCleartextTraffic="true"

    然后就成功了!

    见:https://www.nativescript.org/blog/secure-your-mobile-app-securing-data-in-transit

    【讨论】:

      【解决方案2】:

      Localhost 表示您正在运行的设备。使用 Android 时,您的回送地址不再是标准的 127.0.0.1(又名 localhost),而是类似 10.0.2.2(根据您使用的是 AVD 还是GenyMotion)

      更多here

      您也可以use similar solution进行跨平台解决方案

      【讨论】:

      • 谢谢你,做到了。我以为问题出在 https 但我错了:P
      • 谢谢!我试图弄清楚到底发生了几个小时!
      猜你喜欢
      • 2019-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-31
      • 2021-07-07
      • 2021-06-10
      • 2012-06-28
      相关资源
      最近更新 更多