相关疑难解决方法(0)

Android上的React Native&okhttp - 设置用户代理

我正在尝试User-Agent在Android上设置React Native.做了一些研究,看起来我应该使用okhttp 拦截器.我发现的一个例子解释了如何做到这一点(链接),但后来我不确定如何注册拦截器.

所以为了设置User-Agent我正在使用这个类:

public class CustomInterceptor implements Interceptor {
    @Override public Response intercept(Interceptor.Chain chain) throws IOException {
      Request originalRequest = chain.request();
      Request requestWithUserAgent = originalRequest.newBuilder()
          .removeHeader("User-Agent")
          .header("User-Agent", "Trevor")
          .build();
      return chain.proceed(requestWithUserAgent);
    }
}
Run Code Online (Sandbox Code Playgroud)

那么剩下的就是注册上面的拦截器,以便它应该在哪里完成?也许在MainActivity.java

OkHttpClient okHttp = new OkHttpClient();
okHttp.interceptors().add(new CustomInterceptor());
Run Code Online (Sandbox Code Playgroud)

构建应用程序时我没有遇到任何错误,所以我认为CustomInterceptor应该没问题 - 只需要让应用程序使用它.

更新:我目前正在尝试注册拦截器MainActivity但它不会接受它:

public class MainActivity extends ReactActivity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    OkHttpClient client = new OkHttpClient();
    client.networkInterceptors().add(new …
Run Code Online (Sandbox Code Playgroud)

java android okhttp react-native okhttp3

8
推荐指数
3
解决办法
6043
查看次数

NSExceptionAllowsInsecureHTTPLoads不适用于IP地址

在iOS 9上排除ATS不适合我.

我有一个测试服务器,它没有域名(仅限IP地址),没有SSL证书(因此它是HTTP而不是HTTPS)

尝试:

<key>52.24.145.252</key>
<dict>
    <key>NSExceptionAllowsInsecureHTTPLoads</key>
    <true/>
    <key>NSIncludesSubdomains</key>
    <true/>
</dict>
Run Code Online (Sandbox Code Playgroud)

但我仍然得到错误:

App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file.
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

https ios

2
推荐指数
2
解决办法
1万
查看次数

标签 统计

android ×1

https ×1

ios ×1

java ×1

okhttp ×1

okhttp3 ×1

react-native ×1