初步猜测原因:
okhttp并非原生的http请求,它在header里面并没有真正的User-Agent,而是“okhttp/版本号”这样的字符串;
解决方案:
为okhttp设置User-Agent可以解决问题。
OkHttpClient.Builder builder = new OkHttpClient().newBuilder() ;
builder.addInterceptor(new Interceptor() { @Override public Response intercept(Chain chain) throws IOException { Request newRequest = chain.request().newBuilder() .removeHeader("User-Agent") .addHeader("User-Agent","Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:0.9.4)").build() ; return chain.proceed(newRequest); }
}) ;
OkHttpClient client = builder.build() ; Retrofit retrofit = new Retrofit.Builder() .baseUrl(baseUrl) .addConverterFactory(FastJsonConverterFactory.create()) .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .client(client) .build();