Okhttp+Retrofit下访问百度音乐api出现403 forbidden的解决方案
2018-03-10 11:19
537 查看
初步猜测原因:
okhttp并非原生的http请求,它在header里面并没有真正的User-Agent,而是“okhttp/版本号”这样的字符串;
解决方案:
为okhttp设置User-Agent可以解决问题。
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();
相关文章推荐
- Okhttp+Retrofit下访问百度音乐api出现403 forbidden的解决方案
- Apache访问index.html总是出现403 Forbidden错误的解决方案
- Apache访问index.html总是出现403 Forbidden错误的解决方案
- android用okhttp和retrofit访问网络的时候有时候出现EOFException异常
- 阿里云服务器公网ip访问Apache服务器出现403 Forbidden
- 访问网站时出现403 Forbidden的原因总结
- php部署后访问项目文件提示403,forbidden解决方案
- Tomcat7/8访问Server Status、Manager App、Host Manager出现403 forbidden
- apache 访问出现403 Forbidden
- lighttpd 访问php 403 forbidden解决方案
- apache 访问出现403 Forbidden
- 重点:访问Nginx出现状态码为403 forbidden原因及故障模拟
- apache开启虚拟主机VHOST后 localhost无法访问,出现Forbidden 403
- apache 访问出现403 Forbidden
- wamp2.5 更换默认web目录后访问localhost出现403 forbidden的解决办法
- 升级到yosemite后Apache下/~username/访问出现forbidden
- apache服务出现Forbidden 403问题的解决方法总结
- 从Eclipse启动tomcat服务器,访问出现 404 错误的解决方案
- 解决Nginx出现403 forbidden (13: Permission denied)报错的四种方法
- wamp安装后,apache访问出现403