我正在尝试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) 在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)
我究竟做错了什么?