android https 403,android okhttp3 or okhttp2 has a 403 Forbidden question

Thank you for answer me, this is my first to submit issues, so maybe is not so good, hope you forgive.

Ok,here is my test case.Maybe the layout is not so good.But you just need to copy and paste it.

@Test public void testDoGetByOkHttp() { String[] urls = new String[]{ "http://c.m.163.com/nc/video/list/V9LG4B3A0/n/0-10.html", "http://c.m.163.com/nc/video/list/V9LG4CHOR/n/0-10.html", "http://c.m.163.com/nc/video/list/V9LG4E6VR/n/0-10.html", "http://c.m.163.com/nc/video/list/00850FRB/n/0-10.html" }; for (int i = 0; i < urls.length; i++) { String value = OkHttp3Utils.doGetByOkHttp(urls[i]); Assert.assertNotNull(value); } }

@Test public void testDoGetByConnection() { String[] urls = new String[]{ "http://c.m.163.com/nc/video/list/V9LG4B3A0/n/0-10.html", "http://c.m.163.com/nc/video/list/V9LG4CHOR/n/0-10.html", "http://c.m.163.com/nc/video/list/V9LG4E6VR/n/0-10.html", "http://c.m.163.com/nc/video/list/00850FRB/n/0-10.html" }; for (int i = 0; i < urls.length; i++) { String value = HttpURLConnectionUtils.doGetByConnection(urls[i]); Assert.assertNotNull(value); } }

My OkHttp3Utils class source code is:

public static String doGetByOkHttp(String url) { Request request = new Request.Builder().url(url).build(); try { Response response = new OkHttpClient.Builder() .connectTimeout(10000, TimeUnit.MILLISECONDS) .readTimeout(10000, TimeUnit.MILLISECONDS) .build().newCall(request).execute(); if (response.isSuccessful()) return response.body().string(); } catch (IOException e) { e.printStackTrace(); } return null; }

My HttpURLConnectionUtils class source code is:

public static String doGetByConnection(String url) { BufferedReader reader = null; try { HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection(); connection.setDoInput(true); connection.setDoOutput(true); connection.setConnectTimeout(10000); connection.setReadTimeout(10000); connection.setRequestMethod("GET"); int code = connection.getResponseCode(); if (code == 200) { reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); StringBuilder builder = new StringBuilder(); int len = 2048; char[] cbuf = new char[len]; while ((len = reader.read(cbuf)) != -1) builder.append(cbuf, 0, len); return builder.toString(); } } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } } return null; }

And you can use the browser to test urls, you will find that you can still get the datas.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值