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.