Android HttpURLConnection: HTTP response always 403 - java
public static class GetTitleTask extends AsyncTask<String, Void, String> {
protected String doInBackground(String... urls) {
try {
String youtubeUrl = urls[0];
if (youtubeUrl != null) {
URL url = new URL("http://www.youtube.com/oembed?url=" + youtubeUrl + "&format=json");
HttpURLConnection connection = null;
BufferedReader reader = null;
try {
connection = (HttpURLConnection) url.openConnection();
connection.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:221.0) Gecko/20100101 Firefox/31.0");
connection.connect();
if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
Log.d(TAG, "HTTP response code is " + connection.getResponseCode());
return null;
}
InputStream stream = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer buffer = new StringBuffer();
String line = "";
while ((line = reader.readLine()) != null) {
buffer.append(line+"\n");
Log.d("Response: ", "> " + line);
}
return buffer.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (connection != null) {
connection.disconnect();
}
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String title) {
Log.v(TAG, "onPostExecute: " + title);
super.onPostExecute(title);
// ...
}
}
Input for example: https://www.youtube.com/watch?v=hT_nvWreIhg
This always returns null. The HTTP response code is 403. That is why I tried adding a user agent, but it didn't help.
Is there any way to fix this? I would like to get the video title without using the YouTube API.
I tried fetching it with curl. Your URL uses http instead of https://..., change it to https://www.youtube.com/oembed?url=.
Trying to fetch the URL when it starts with just http resulted in a 403 status for me. But https was successful.
URL url = new URL("http://www.youtube.com/oembed?url=" + youtubeUrl + "&format=json");
Should be changed to:
URL url = new URL("https://www.youtube.com/oembed?url=" + youtubeUrl + "&format=json");
The server does not agree to fulfil the request (error code 403). This is probably due to the protocol. Try using HttpsURLConnection instead of HttpURLConnection.
Related
Login to Synology via Quickconnect ID via webapi not working
When I use this link in PC browser : "http://QuickconnectID.quickconnect.to/webapi/entry.cgi?api=SYNO.API.Auth&version=6&method=login&account=&passwd=&session=FileStation&format=sid" Result will be: {"data":{"did":"","is_portal_port":false,"sid":"FnNj2gZlk1JKCuvRgnPxmobytzHr-gsOgfk0sGIc1LisvEF-20VV4EDD6BMndvCGnt2vT1cGQobg"},"success":true} What is correct te get session id for work with Synology.But when I use code to login via android app: String strurl = "http://QuickconnectID.quickconnect.to/webapi/entry.cgi?api=SYNO.API.Auth&version=6&method=login&account=<ACCOUNT>&passwd=<PASSWORD>&session=FileStation&format=sid" String s = fetchHttpList(strurl); private static URL createUrl(String stringUrl) { URL url = null; try { url = new URL(stringUrl); } catch (MalformedURLException e) { Log.e(LOG_TAG, "Problem building the URL ", e); } return url; } public static String fetchHttpList(String requestUrl) { URL url = createUrl(requestUrl); String jsonResponse = null; try { jsonResponse = makeHttpRequest(url,"GET"); } catch (IOException e) { Log.e(LOG_TAG, "Problem making the HTTP request.", e); } return jsonResponse; } private static String makeHttpRequest(URL url,String method) throws IOException { String jsonResponse = ""; if (url == null) { return jsonResponse; } HttpURLConnection urlConnection = null; InputStream inputStream = null; try { urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setReadTimeout(10000 /* milliseconds */); urlConnection.setConnectTimeout(15000 /* milliseconds */); urlConnection.setRequestMethod(method); //change in v16 urlConnection.connect(); if (urlConnection.getResponseCode() == 200) { inputStream = urlConnection.getInputStream(); jsonResponse = readFromStream(inputStream); } else { Log.e(LOG_TAG, "Error response code: " + urlConnection.getResponseCode()); Log.e(LOG_TAG, url.toString()); } } catch(ConnectException e){ throw e; } catch (IOException e) { Log.e(LOG_TAG, "Problem retrieving the JSON results.", e); } finally { if (urlConnection != null) { urlConnection.disconnect(); } if (inputStream != null) { inputStream.close(); } } return jsonResponse; } Result of s will be temporary PAGE WITH connecting: <!doctype html> <html> <head> <script async src="https://www.googletagmanager.com/gtag/js?id=UA-131382946-3">**strong text** etc... <br> Synology uses some redirection of web login pages. After enter link in browser http://somename.quickconnect.to final link after redirection will be: http://somename.direct.quickconnect.to:5001 Can you help me with this issue to get JSON like from PC Browser?
Send data(Client_id=1,Staff_id=2) from android application to tomcat server
i want to send data from android application to tomcat java server. Data is just one is client_id which is 1 and second is staff_id which is 2. after authenticate the client id and staff id from tomcat show me a toast of success....please help... Code is here public class MyAsyncTasks extends AsyncTask<String, String, String> { #Override protected void onPreExecute() { super.onPreExecute(); // display a progress dialog for good user experiance } #Override protected String doInBackground(String... params) { // implement API in background and store the response in current variable String current = ""; try { URL url; HttpURLConnection urlConnection = null; try { url = new URL("http://192.168.1.13:8080/digitaldisplay/s/m/data"); urlConnection = (HttpURLConnection) url .openConnection(); InputStream in = urlConnection.getInputStream(); InputStreamReader isw = new InputStreamReader(in); int data = isw.read(); while (data != -1) { current += (char) data; data = isw.read(); System.out.print(current); } // return the data to onPostExecute method return current; } catch (Exception e) { e.printStackTrace(); } finally { if (urlConnection != null) { urlConnection.disconnect(); } } } catch (Exception e) { e.printStackTrace(); return "Exception: " + e.getMessage(); } return current; } #Override protected void onPostExecute(String s) { Toast.makeText(Register.this, "success", Toast.LENGTH_SHORT).show(); Log.d("data", s.toString()); // dismiss the progress dialog after receiving data from API try { // JSON Parsing of data JSONArray jsonArray = new JSONArray(s); JSONObject oneObject = jsonArray.getJSONObject(0); // Pulling items from the array client = Integer.parseInt(oneObject.getString("client")); staff = Integer.parseInt(oneObject.getString("staff")); } catch (JSONException e) { e.printStackTrace(); } } }}
The logic in your code looks off to me. This is the pattern I usually follow when making a REST call from an activity using HttpURLConnection: try { String endpoint = "http://192.168.1.13:8080/digitaldisplay/s/m/data"; URL obj = new URL(endpoint); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); con.setRequestMethod("POST"); // but maybe you want GET here... con.setConnectTimeout(10000); con.setDoInput(true); con.setDoOutput(true); JSONObject inputJSON = new JSONObject(); inputJSON.put("Client_id", 1); inputJSON.put("Staff_id", 2); con.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); OutputStream os = con.getOutputStream(); BufferedWriter writer = new BufferedWriter( new OutputStreamWriter(os, "UTF-8")); writer.write(inputJSON.toString()); writer.flush(); writer.close(); os.close(); int responseCode = con.getResponseCode(); BufferedReader in = new BufferedReader( new InputStreamReader(con.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); System.out.println(response); } catch (SocketTimeoutException se) { // handle timeout exception responseCode = -1; } catch (Exception e) { // handle general exception responseCode = 0; } The only major change in adapting the above code for GET would be that you wouldn't write your input data to the connection. Instead, you would just append query parameters to the URL. I am possibly guessing that you need POST here, since your URL doesn't have any query parameters in it.
Trouble with "inputstream" when creating mobile app that downloads weather information (with AsyncTask)
I'm trying to make a mobile app that downloads info from the openweathermap.org apis. For example, if you feed that app this link: http://api.openweathermap.org/data/2.5/weather?q=Boston,us&appid=fed33a8f8fd54814d7cbe8515a5c25d7 you will get the information about the weather in Boston, MA. My code seems to work up to the point where I have to convert the input stream to a string variable. When I do that, I get garbage. Is there a particular way to do this seemingly simple task in a proper way? Here is my code so far... private class DownloadWebpageTask extends AsyncTask<String, Void, String> { #Override protected String doInBackground(String... urls) { // params comes from the execute() call: params[0] is the url. try { return downloadUrl(urls[0]); } catch (IOException e) { return null; } } // onPostExecute displays the results of the AsyncTask. #Override protected void onPostExecute(String result) { TextView test = (TextView) findViewById(R.id.test); if(result!=null) test.setText(result); else{ Log.i(DEBUG_TAG, "returned result is null");} } } private String downloadUrl(String myurl) throws IOException { InputStream is = null; try { URL url = new URL(myurl); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setReadTimeout(10000 /* milliseconds */); conn.setConnectTimeout(15000 /* milliseconds */); conn.setRequestMethod("GET"); conn.setDoInput(true); // Starts the query conn.connect(); int response = conn.getResponseCode(); Log.i(DEBUG_TAG, "The response is: " + response); is = conn.getInputStream(); String text = getStringFromInputStream(is); //JSONObject json = new JSONObject(text); //try (Scanner scanner = new Scanner(is, StandardCharsets.UTF_8.name())) { //text = scanner.useDelimiter("\\A").next(); //} //Bitmap bitmap = BitmapFactory.decodeStream(is); return text; }catch(Exception e) { Log.i(DEBUG_TAG, e.toString()); }finally { if (is != null) { is.close(); } } return null; } private static String getStringFromInputStream(InputStream is) { BufferedReader br = null; StringBuilder sb = new StringBuilder(); String line; try { br = new BufferedReader(new InputStreamReader(is)); while ((line = br.readLine()) != null) { sb.append(line); } } catch (IOException e) { e.printStackTrace(); } finally { if (br != null) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } return sb.toString(); }
Check this library . Is An asynchronous callback-based Http client for Android built on top of Apache’s HttpClient libraries.
HttpUrlConnection sometimes gives EOF exception
I am using HttpUrlConnection and using POST method to get some data from web server. Sometimes, I get the response and at times I get EOFexception These are the solutions are I have already tried : 1) System.setProperty("http.keepAlive", "false"); 2) if (Build.VERSION.SDK != null && Build.VERSION.SDK_INT > 13) { connection.setRequestProperty("Connection", "close"); } Below is my code from AsyncTask class; CODE : #Override protected JSONObject doInBackground(KeyValuePair... keyValuePairs) { JSONObject jsonResponse = new JSONObject(); HttpURLConnection connection = null; // check if is Internet is available before making a network call if (isInternetAvailable()) { try { jsonResponse = new JSONObject(); URL url = new URL(urlStr); connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Connection", "Keep-Alive"); connection.setUseCaches(false); connection.setDoOutput(true); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); connection.setRequestProperty("charset", "UTF-8"); if (Build.VERSION.SDK != null && Build.VERSION.SDK_INT > 13) { connection.setRequestProperty("Connection", "close"); } // setting post params StringBuilder builder = new StringBuilder(); for (int i = 0; i < keyValuePairs.length; i++) { builder.append(URLEncoder.encode(keyValuePairs[i].getKey(), "UTF-8") + "=" + URLEncoder.encode(keyValuePairs[i].getValue(), "UTF-8") + "&"); GeneralUtils.print("key : " + keyValuePairs[i].getKey() + ", value : " + keyValuePairs[i].getValue()); } String postData = builder.toString(); postData = postData.substring(0, postData.length() - 1); GeneralUtils.print("postData " + postData); byte[] postDataByteArr = postData.getBytes(); connection.setFixedLengthStreamingMode(postDataByteArr.length); connection.setConnectTimeout(20000); DataOutputStream dataOutputStream = new DataOutputStream(connection.getOutputStream()); dataOutputStream.writeBytes(postData); dataOutputStream.flush(); dataOutputStream.close(); GeneralUtils.print("respCode " + connection.getResponseCode()); // if connection was not successful if (connection.getResponseCode() != 200) { jsonResponse.put("status", "Failure"); jsonResponse.put("message", "Something went wrong. Please Try Again"); } else { BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line = null; StringBuilder sb = new StringBuilder(); while ((line = reader.readLine()) != null) { sb.append(line); } reader.close(); String response = sb.toString(); GeneralUtils.print("NetworkCall Server response " + response); jsonResponse = new JSONObject(response); } } catch (JSONException e) { GeneralUtils.print("NetworkCall.JSONEx 162 " + e); } catch (MalformedURLException e) { GeneralUtils.print("NetworkCall.MalformedURLEx " + e); } catch (IOException e) { try { jsonResponse.put("status", "No Internet Connection"); jsonResponse.put("message", "Please check your Internet connection and try again"); } catch (JSONException e1) { GeneralUtils.print("NetworkCall.JSONEx " + e); } } finally { connection.disconnect(); } } else { // if Internet is not available try { jsonResponse.put("status", "No Internet Connection"); jsonResponse.put("message", "Please check your Internet connection and try again"); } catch (JSONException e) { e.printStackTrace(); } } return jsonResponse; } Many many thanks in advance!
As of now I am following a workaround posted here which essentially dictates trying to connect N number of times to bypass the EOF exception issue. In my case, when I catch EOFException, I call the doInBackground again depending upon the reconnectCount; CODE : catch (IOException e) { try { if (reConnectCount <= 10) { reConnectCount++; jsonResponse = doInBackground(keyValuePairs); } else { jsonResponse.put("status", "No Internet Connection"); jsonResponse.put("message", "Please check your Internet connection and try again"); } } catch (JSONException e1) { GeneralUtils.print("NetworkCall.JSONEx " + e); } } Where jsonResponse essentially holds server response in JSON form. So, whenever doInBackground is successfully executed (i.e. does not get Caught and returns jsonResponse), we overwrite the calling doInBackground's jsonResponse object.
HTTP Request to Wikipedia gives no result
I try to fetch HTML per Code. When fetching from "http://www.google.com" for example it works perfect. When trying to fetch from "http://en.wikipedia.org/w/api.php" I do not get any results. Does someone have any idea ? Code: String sURL="http://en.wikipedia.org/w/api.php?action=query&generator=categorymembers&gcmtitle=Category:Countries&prop=info&gcmlimit=500&format=json"; String sText=readfromURL(sURL); public static String readfromURL(String sURL){ URL url = null; try { url = new URL(sURL); } catch (MalformedURLException e1) { e1.printStackTrace(); } URLConnection urlconnect = null; try { urlconnect = url.openConnection(); urlconnect.setRequestProperty("User-Agent","Mozilla/5.0 (Windows NT 5.1; rv:19.0) Gecko/20100101 Firefox/19.0"); } catch (IOException e) { e.printStackTrace(); } BufferedReader in = null; try { in = new BufferedReader(new InputStreamReader(urlconnect.getInputStream())); } catch (IOException e) { e.printStackTrace(); } String inputLine; String sEntireContent=""; try { while ((inputLine = in.readLine()) != null) { System.out.println(inputLine); sEntireContent=sEntireContent+inputLine; } } catch (IOException e) { e.printStackTrace(); } try { in.close(); } catch (IOException e) { e.printStackTrace(); } return sEntireContent; }
It looks like the request limit. Try to check the response code. From the documentation (https://www.mediawiki.org/wiki/API:Etiquette): If you make your requests in series rather than in parallel (i.e. wait for the one request to finish before sending a new request, such that you're never making more than one request at the same time), then you should definitely be fine. Be sure that you do not do few request at a time Update I did verification on my local your code - you are correct it does not work. Fix - you need to use https, so it would work: https://en.wikipedia.org/w/api.php?action=query&generator=categorymembers&gcmtitle=Category:Countries&prop=info&gcmlimit=500&format=json result: {"batchcomplete":"","query":{"pages":{"5165":{"pageid":5165,"ns":0,"title":"Country","contentmodel":"wikitext","pagelanguage":"en","touched":"2015-10-20T20:09:05Z","lastrevid":686706429,"length":12695},"5112305":{"pageid":5112305,"ns":14,"title":"Category:Countries by continent","contentmodel":"wikitext","pagelanguage":"en","touched":"2015-10-18T17:31:54Z","lastrevid":681415612,"length":133},"14353213":{"pageid":14353213,"ns":14,"title":"Category:Countries by form of government","contentmodel":"wikitext","pagelanguage":"en","touched":"2015-08-13T23:33:29Z","lastrevid":675984011,"length":261},"5112467":{"pageid":5112467,"ns":14,"title":"Category:Countries by international organization","contentmodel":"wikitext","pagelanguage":"en","touched":"2015-10-18T05:11:12Z","lastrevid":686245148,"length":123},"4696391":{"pageid":4696391,"ns":14,"title":"Category:Countries by language","contentmodel":"wikitext","pagelanguage":"en","touched":"2015-10-20T01:17:18Z","lastrevid":675966601,"length":333},"5112374":{"pageid":5112374,"ns":14,"title":"Category:Countries by status","contentmodel":"wikitext","pagelanguage":"en","touched":"2015-08-13T21:05:47Z","lastrevid":675966630,"length":30},"708617":{"pageid":708617,"ns":14,"title":"Category:Lists of countries","contentmodel":"wikitext","pagelanguage":"en","touched":"2015-10-18T05:08:45Z","lastrevid":681553760,"length":256},"46624537":{"pageid":46624537,"ns":14,"title":"Category:Caspian littoral states","contentmodel":"wikitext","pagelanguage":"en","touched":"2015-09-23T08:40:34Z","lastrevid":663549987,"length":50},"18066512":{"pageid":18066512,"ns":14,"title":"Category:City-states","contentmodel":"wikitext","pagelanguage":"en","touched":"2015-09-29T20:14:14Z","lastrevid":679367764,"length":145},"2019528":{"pageid":2019528,"ns":14,"title":"Category:Country classifications","contentmodel":"wikitext","pagelanguage":"en","touched":"2015-09-25T09:09:13Z","lastrevid":675966465,"length":182},"935240":{"pageid":935240,"ns":14,"title":"Category:Country codes","contentmodel":"wikitext","pagelanguage":"en","touched":"2015-10-17T06:05:53Z","lastrevid":546489724,"length":222},"36819536":{"pageid":36819536,"ns":14,"title":"Category:Countries in fiction","contentmodel":"wikitext","pagelanguage":"en","touched":"2015-10-03T06:09:16Z","lastrevid":674147667,"length":169},"699787":{"pageid":699787,"ns":14,"title":"Category:Fictional countries","contentmodel":"wikitext","pagelanguage":"en","touched":"2015-10-17T18:43:25Z","lastrevid":610289877,"length":356},"804303":{"pageid":804303,"ns":14,"title":"Category:Former countries","contentmodel":"wikitext","pagelanguage":"en","touched":"2015-09-21T09:58:52Z","lastrevid":668632882,"length":403},"7213567":{"pageid":7213567,"ns":14,"title":"Category:Island countries","contentmodel":"wikitext","pagelanguage":"en","touched":"2015-10-22T22:10:37Z","lastrevid":648502876,"length":157},"3046541":{"pageid":3046541,"ns":14,"title":"Category:Landlocked countries","contentmodel":"wikitext","pagelanguage":"en","touched":"2015-10-04T00:45:24Z","lastrevid":648502892,"length":54},"743058":{"pageid":743058,"ns":14,"title":"Category:Middle Eastern countries","contentmodel":"wikitext","pagelanguage":"en","touched":"2015-10-12T14:41:59Z","lastrevid":677900732,"length":495},"41711462":{"pageid":41711462,"ns":14,"title":"Category:Mongol states","contentmodel":"wikitext","pagelanguage":"en","touched":"2015-10-23T07:36:21Z","lastrevid":687093637,"length":121},"30645082":{"pageid":30645082,"ns":14,"title":"Category:Country names","contentmodel":"wikitext","pagelanguage":"en","touched":"2015-03-07T06:33:19Z","lastrevid":561256656,"length":94},"21218559":{"pageid":21218559,"ns":14,"title":"Category:Outlines of countries","contentmodel":"wikitext","pagelanguage":"en","touched":"2015-10-07T18:04:29Z","lastrevid":645312408,"length":248},"37943702":{"pageid":37943702,"ns":14,"title":"Category:Proposed countries","contentmodel":"wikitext","pagelanguage":"en","touched":"2015-10-20T02:30:25Z","lastrevid":668630396,"length":130},"15086044":{"pageid":15086044,"ns":14,"title":"Category:Turkic states","contentmodel":"wikitext","pagelanguage":"en","touched":"2015-10-20T06:23:35Z","lastrevid":677424552,"length":114},"32809189":{"pageid":32809189,"ns":14,"title":"Category:Works about countries","contentmodel":"wikitext","pagelanguage":"en","touched":"2015-10-17T08:45:32Z","lastrevid":620016516,"length":153},"27539189":{"pageid":27539189,"ns":14,"title":"Category:Wikipedia books on countries","contentmodel":"wikitext","pagelanguage":"en","touched":"2015-04-11T05:12:25Z","lastrevid":546775798,"length":203},"35317198":{"pageid":35317198,"ns":14,"title":"Category:Wikipedia categories named after countries","contentmodel":"wikitext","pagelanguage":"en","touched":"2015-10-17T18:35:14Z","lastrevid":641689352,"length":202}}}} {"batchcomplete":"","query":{"pages":{"5165":{"pageid":5165,"ns":0,"title":"Country","contentmodel":"wikitext","pagelanguage":"en","touched":"2015-10-20T20:09:05Z","lastrevid":686706429,"length":12695},"5112305":{"pageid":5112305,"ns":14,"title":"Category:Countries by continent","contentmodel":"wikitext","pagelanguage":"en","touched":"2015-10-18T17:31:54Z","lastrevid":681415612,"length":133},"14353213":{"pageid":14353213,"ns":14,"title":"Category:Countries by form of government","contentmodel":"wikitext","pagelanguage":"en","touched":"2015-08-13T23:33:29Z","lastrevid":675984011,"length":261},"5112467":{"pageid":5112467,"ns":14,"title":"Category:Countries by international organization","contentmodel":"wikitext","pagelanguage":"en","touched":"2015-10-18T05:11:12Z","lastrevid":686245148,"length":123},"4696391":{"pageid":4696391,"ns":14,"title":"Category:Countries by language","contentmodel":"wikitext","pagelanguage":"en","touched":"2015-10-20T01:17:18Z","lastrevid":675966601,"length":333},"5112374":{"pageid":5112374,"ns":14,"title":"Category:Countries by status","contentmodel":"wikitext","pagelanguage":"en","touched":"2015-08-13T21:05:47Z","lastrevid":675966630,"length":30},"708617":{"pageid":708617,"ns":14,"title":"Category:Lists of countries","contentmodel":"wikitext","pagelanguage":"en","touched":"2015-10-18T05:08:45Z","lastrevid":681553760,"length":256},"46624537":{"pageid":46624537,"ns":14,"title":"Category:Caspian littoral states","contentmodel":"wikitext","pagelanguage":"en","touched":"2015-09-23T08:40:34Z","lastrevid":663549987,"length":50},"18066512":{"pageid":18066512,"ns":14,"title":"Category:City-states","contentmodel":"wikitext","pagelanguage":"en","touched":"2015-09-29T20:14:14Z","lastrevid":679367764,"length":145},"2019528":{"pageid":2019528,"ns":14,"title":"Category:Country classifications","contentmodel":"wikitext","pagelanguage":"en","touched":"2015-09-25T09:09:13Z","lastrevid":675966465,"length":182},"935240":{"pageid":935240,"ns":14,"title":"Category:Country codes","contentmodel":"wikitext","pagelanguage":"en","touched":"2015-10-17T06:05:53Z","lastrevid":546489724,"length":222},"36819536":{"pageid":36819536,"ns":14,"title":"Category:Countries in fiction","contentmodel":"wikitext","pagelanguage":"en","touched":"2015-10-03T06:09:16Z","lastrevid":674147667,"length":169},"699787":{"pageid":699787,"ns":14,"title":"Category:Fictional countries","contentmodel":"wikitext","pagelanguage":"en","touched":"2015-10-17T18:43:25Z","lastrevid":610289877,"length":356},"804303":{"pageid":804303,"ns":14,"title":"Category:Former countries","contentmodel":"wikitext","pagelanguage":"en","touched":"2015-09-21T09:58:52Z","lastrevid":668632882,"length":403},"7213567":{"pageid":7213567,"ns":14,"title":"Category:Island countries","contentmodel":"wikitext","pagelanguage":"en","touched":"2015-10-22T22:10:37Z","lastrevid":648502876,"length":157},"3046541":{"pageid":3046541,"ns":14,"title":"Category:Landlocked countries","contentmodel":"wikitext","pagelanguage":"en","touched":"2015-10-04T00:45:24Z","lastrevid":648502892,"length":54},"743058":{"pageid":743058,"ns":14,"title":"Category:Middle Eastern countries","contentmodel":"wikitext","pagelanguage":"en","touched":"2015-10-12T14:41:59Z","lastrevid":677900732,"length":495},"41711462":{"pageid":41711462,"ns":14,"title":"Category:Mongol states","contentmodel":"wikitext","pagelanguage":"en","touched":"2015-10-23T07:36:21Z","lastrevid":687093637,"length":121},"30645082":{"pageid":30645082,"ns":14,"title":"Category:Country names","contentmodel":"wikitext","pagelanguage":"en","touched":"2015-03-07T06:33:19Z","lastrevid":561256656,"length":94},"21218559":{"pageid":21218559,"ns":14,"title":"Category:Outlines of countries","contentmodel":"wikitext","pagelanguage":"en","touched":"2015-10-07T18:04:29Z","lastrevid":645312408,"length":248},"37943702":{"pageid":37943702,"ns":14,"title":"Category:Proposed countries","contentmodel":"wikitext","pagelanguage":"en","touched":"2015-10-20T02:30:25Z","lastrevid":668630396,"length":130},"15086044":{"pageid":15086044,"ns":14,"title":"Category:Turkic states","contentmodel":"wikitext","pagelanguage":"en","touched":"2015-10-20T06:23:35Z","lastrevid":677424552,"length":114},"32809189":{"pageid":32809189,"ns":14,"title":"Category:Works about countries","contentmodel":"wikitext","pagelanguage":"en","touched":"2015-10-17T08:45:32Z","lastrevid":620016516,"length":153},"27539189":{"pageid":27539189,"ns":14,"title":"Category:Wikipedia books on countries","contentmodel":"wikitext","pagelanguage":"en","touched":"2015-04-11T05:12:25Z","lastrevid":546775798,"length":203},"35317198":{"pageid":35317198,"ns":14,"title":"Category:Wikipedia categories named after countries","contentmodel":"wikitext","pagelanguage":"en","touched":"2015-10-17T18:35:14Z","lastrevid":641689352,"length":202}}}}
The reason you didn't receive the response back is due to a HTTP Redirect 3XX. Wikipedia redirects your HTTP Request. Please try the below source code to fetch Response from Redirected URL. Please refer How to send HTTP request GET/POST in Java public static String readfromURLwithRedirect(String url) { String response = ""; try { URL obj = new URL(url); HttpURLConnection conn = (HttpURLConnection) obj.openConnection(); conn.setReadTimeout(5000); conn.addRequestProperty("Accept-Language", "en-US,en;q=0.8"); conn.addRequestProperty("User-Agent", "Mozilla"); conn.addRequestProperty("Referer", "google.com"); System.out.println("Request URL ... " + url); boolean redirect = false; // normally, 3xx is redirect int status = conn.getResponseCode(); if (status != HttpURLConnection.HTTP_OK) { if (status == HttpURLConnection.HTTP_MOVED_TEMP || status == HttpURLConnection.HTTP_MOVED_PERM || status == HttpURLConnection.HTTP_SEE_OTHER) { redirect = true; } } System.out.println("Response Code ... " + status); if (redirect) { // get redirect url from "location" header field String newUrl = conn.getHeaderField("Location"); // get the cookie if need, for login String cookies = conn.getHeaderField("Set-Cookie"); // open the new connnection again conn = (HttpURLConnection) new URL(newUrl).openConnection(); conn.setRequestProperty("Cookie", cookies); conn.addRequestProperty("Accept-Language", "en-US,en;q=0.8"); conn.addRequestProperty("User-Agent", "Mozilla"); conn.addRequestProperty("Referer", "google.com"); System.out.println("Redirect to URL : " + newUrl); } BufferedReader in = new BufferedReader( new InputStreamReader(conn.getInputStream())); String inputLine; StringBuffer responseBuffer = new StringBuffer(); while ((inputLine = in.readLine()) != null) { responseBuffer.append(inputLine); } in.close(); System.out.println("URL Content... \n" + responseBuffer.toString()); response = responseBuffer.toString(); System.out.println("Done"); } catch (Exception e) { e.printStackTrace(); } return response; }