Okhttp: android okhttp3 or okhttp2 has a 403 Forbidden question

Created on 28 Dec 2016  Â·  3Comments  Â·  Source: square/okhttp

I use okhttp3 and okhtt2 and Retrofit2 found they have a 403 Forbidden question to http://c.m.163.com/nc/video/list/V9LG4B3A0/n/0-10.html/ or http://c.m.163.com/nc/video/list/V9LG4CHOR/n/0-10.html/. but Apache HttpClient and HttpURLConnection and Browser is ok,can to get datas.

My okhttp3 of code is:

public String doGet(String url) { Request request = new Request.Builder().url(url).build(); try { Response response = mHttpClient.newCall(request).execute(); if (response.isSuccessful()) return response.body().string(); else LogCatUtils.log("【OkHttp3Util # doGet】response code is " + response.code() + " , message is " + response.message()); } catch (IOException e) { e.printStackTrace(); LogCatUtils.log("【OkHttp3Util # doGet】has a IOException , message is " + e.getMessage()); } return null; }

print log are:【OkHttp3Util # doGet】response code is 403 , message is Forbidden

My HttpURLConnection of code is:

`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"
};
for(int i=0;i<2;i++){
final String path = urls[i];
new Thread(new Runnable() {
@Override
public void run() {
try {
URL url = new URL(path);
HttpURLConnection connection = ((HttpURLConnection)url.openConnection());
InputStream input;
if (connection.getResponseCode() == 200)
input = connection.getInputStream();
else input = connection.getErrorStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
String msg;
while ((msg =reader.readLine()) != null)
Log.i("My",msg);
} catch (IOException e) {
System.err.println(e);
}

            }
        }).start();
    }`

print log are:{"V9LG4CHOR":[{"topicImg":"http://vimg1.ws.126.net/image/snapshot/2012/11/I/3/V8GIAB8I3.jpg","videosource":"新ćȘ’䜓","mp4Hd_url":"http://flv2.bn.netease.com/videolib3/1506/29/tUuas4099/HD/tUuas4099-mobile.mp4","topicDesc"...

Be careful,Browser can also to get datas!

so, you can watch logs is diffrent, i think is okhttp bug,how to do it,can you answer me?

Most helpful comment

year,I saw your url is "http://c.m.163.com/nc/video/list/V9LG4B3A0/n/0-10.html",I think you what to get data from neteast, but neteast refuse to allow OKHttp, so, you need to add head with code: .header("User-Agent", "a"). hope you can read Chinese

All 3 comments

If this is a big report please provide a failing test case. Otherwise please use StackOverflow for usage questions.

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.

year,I saw your url is "http://c.m.163.com/nc/video/list/V9LG4B3A0/n/0-10.html",I think you what to get data from neteast, but neteast refuse to allow OKHttp, so, you need to add head with code: .header("User-Agent", "a"). hope you can read Chinese

Was this page helpful?
0 / 5 - 0 ratings