List of usage examples for java.net HttpURLConnection getResponseMessage
public String getResponseMessage() throws IOException
From source file:carnero.cgeo.cgBase.java
public void postTweet(cgeoapplication app, cgSettings settings, String status, Double latitude, Double longitude) {//from w w w . ja v a 2 s. c o m if (app == null) { return; } if (settings == null || settings.tokenPublic == null || settings.tokenPublic.length() == 0 || settings.tokenSecret == null || settings.tokenSecret.length() == 0) { return; } try { HashMap<String, String> parameters = new HashMap<String, String>(); parameters.put("status", status); if (latitude != null && longitude != null) { parameters.put("lat", String.format("%.6f", latitude)); parameters.put("long", String.format("%.6f", longitude)); parameters.put("display_coordinates", "true"); } final String paramsDone = cgOAuth.signOAuth("api.twitter.com", "/1/statuses/update.json", "POST", false, parameters, settings.tokenPublic, settings.tokenSecret); HttpURLConnection connection = null; try { final StringBuffer buffer = new StringBuffer(); final URL u = new URL("http://api.twitter.com/1/statuses/update.json"); final URLConnection uc = u.openConnection(); uc.setRequestProperty("Host", "api.twitter.com"); connection = (HttpURLConnection) uc; connection.setReadTimeout(30000); connection.setRequestMethod("POST"); HttpURLConnection.setFollowRedirects(true); connection.setDoInput(true); connection.setDoOutput(true); final OutputStream out = connection.getOutputStream(); final OutputStreamWriter wr = new OutputStreamWriter(out); wr.write(paramsDone); wr.flush(); wr.close(); Log.i(cgSettings.tag, "Twitter.com: " + connection.getResponseCode() + " " + connection.getResponseMessage()); InputStream ins; final String encoding = connection.getContentEncoding(); if (encoding != null && encoding.equalsIgnoreCase("gzip")) { ins = new GZIPInputStream(connection.getInputStream()); } else if (encoding != null && encoding.equalsIgnoreCase("deflate")) { ins = new InflaterInputStream(connection.getInputStream(), new Inflater(true)); } else { ins = connection.getInputStream(); } final InputStreamReader inr = new InputStreamReader(ins); final BufferedReader br = new BufferedReader(inr); readIntoBuffer(br, buffer); br.close(); ins.close(); inr.close(); connection.disconnect(); } catch (IOException e) { Log.e(cgSettings.tag, "cgBase.postTweet.IO: " + connection.getResponseCode() + ": " + connection.getResponseMessage() + " ~ " + e.toString()); final InputStream ins = connection.getErrorStream(); final StringBuffer buffer = new StringBuffer(); final InputStreamReader inr = new InputStreamReader(ins); final BufferedReader br = new BufferedReader(inr); readIntoBuffer(br, buffer); br.close(); ins.close(); inr.close(); } catch (Exception e) { Log.e(cgSettings.tag, "cgBase.postTweet.inner: " + e.toString()); } connection.disconnect(); } catch (Exception e) { Log.e(cgSettings.tag, "cgBase.postTweet: " + e.toString()); } }
From source file:au.com.infiniterecursion.vidiom.utils.PublishingUtils.java
private String uploadMetaData(final Activity activity, final Handler handler, String filePath, String title, String description, boolean retry, long sdrecord_id) throws IOException { String uploadUrl = INITIAL_UPLOAD_URL; HttpURLConnection urlConnection = getGDataUrlConnection(uploadUrl); urlConnection.setRequestMethod("POST"); urlConnection.setDoOutput(true);/*from w w w. j av a 2 s . c om*/ urlConnection.setRequestProperty("Content-Type", "application/atom+xml"); // urlConnection.setRequestProperty("Content-Length", newValue); urlConnection.setRequestProperty("Slug", filePath); String atomData = null; String category = DEFAULT_VIDEO_CATEGORY; this.tags = DEFAULT_VIDEO_TAGS; String template = readFile(activity, R.raw.gdata).toString(); // Workarounds for corner cases. Youtube doesnt like empty titles if (title == null || title.length() == 0) { title = "Untitled"; } if (description == null || description.length() == 0) { description = "No description"; } // Check user preference to see if YouTube videos should be private by // default. SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity.getBaseContext()); Boolean ytPrivate = prefs.getBoolean("defaultYouTubePrivatePreference", true); String private_or_not = "<yt:private />"; if (!ytPrivate) { private_or_not = ""; } atomData = String.format(template, title, description, category, this.tags, private_or_not); OutputStreamWriter outStreamWriter = null; int responseCode = -1; try { outStreamWriter = new OutputStreamWriter(urlConnection.getOutputStream()); outStreamWriter.write(atomData); outStreamWriter.close(); /* * urlConnection.connect(); InputStream is = * urlConnection.getInputStream(); BufferedReader in = new * BufferedReader(new InputStreamReader(is)); String inputLine; * * while ((inputLine = in.readLine()) != null) { * Log.d(TAG,inputLine); } in.close(); */ responseCode = urlConnection.getResponseCode(); // ERROR LOGGING InputStream is = urlConnection.getErrorStream(); if (is != null) { Log.e(TAG, " Error stream from Youtube available!"); BufferedReader in = new BufferedReader(new InputStreamReader(is)); String inputLine; while ((inputLine = in.readLine()) != null) { Log.d(TAG, inputLine); } in.close(); Map<String, List<String>> hfs = urlConnection.getHeaderFields(); for (Entry<String, List<String>> hf : hfs.entrySet()) { Log.d(TAG, " entry : " + hf.getKey()); List<String> vals = hf.getValue(); for (String s : vals) { Log.d(TAG, "vals:" + s); } } } } catch (IOException e) { // // Catch IO Exceptions here, like UnknownHostException, so we can // detect network failures, and send a notification // Log.d(TAG, " Error occured in uploadMetaData! "); e.printStackTrace(); responseCode = -1; outStreamWriter = null; mainapp.removeSDFileRecordIDfromUploadingTrack(sdrecord_id, TYPE_YT); // Use the handler to execute a Runnable on the // main thread in order to have access to the // UI elements. handler.postDelayed(new Runnable() { public void run() { // Update UI // Indicate back to calling activity the result! // update uploadInProgress state also. ((VidiomActivity) activity).finishedUploading(false); ((VidiomActivity) activity) .createNotification(res.getString(R.string.upload_to_youtube_host_failed_)); } }, 0); // forward it on! throw e; } if (responseCode < 200 || responseCode >= 300) { // The response code is 40X if ((responseCode + "").startsWith("4") && retry) { Log.d(TAG, "retrying to fetch auth token for " + youTubeName); this.clientLoginToken = authorizer.getFreshAuthToken(youTubeName, clientLoginToken); // Try again with fresh token return uploadMetaData(activity, handler, filePath, title, description, false, sdrecord_id); } else { mainapp.removeSDFileRecordIDfromUploadingTrack(sdrecord_id, TYPE_YT); // Probably not authorised! // Need to setup a Youtube account. // Use the handler to execute a Runnable on the // main thread in order to have access to the // UI elements. handler.postDelayed(new Runnable() { public void run() { // Update UI // Indicate back to calling activity the result! // update uploadInProgress state also. ((VidiomActivity) activity).finishedUploading(false); ((VidiomActivity) activity) .createNotification(res.getString(R.string.upload_to_youtube_host_failed_)); } }, 0); throw new IOException(String.format("response code='%s' (code %d)" + " for %s", urlConnection.getResponseMessage(), responseCode, urlConnection.getURL())); } } return urlConnection.getHeaderField("Location"); }
From source file:cgeo.geocaching.cgBase.java
public static String requestJSON(String scheme, String host, String path, String method, String params) { int httpCode = -1; //String httpLocation = null; if (method == null) { method = "GET"; } else {/* w ww . j a v a2s .co m*/ method = method.toUpperCase(); } boolean methodPost = false; if (method.equalsIgnoreCase("POST")) { methodPost = true; } URLConnection uc = null; HttpURLConnection connection = null; Integer timeout = 30000; final StringBuffer buffer = new StringBuffer(); for (int i = 0; i < 3; i++) { if (i > 0) { Log.w(cgSettings.tag, "Failed to download data, retrying. Attempt #" + (i + 1)); } buffer.delete(0, buffer.length()); timeout = 30000 + (i * 15000); try { try { URL u = null; if (methodPost) { u = new URL(scheme + host + path); } else { u = new URL(scheme + host + path + "?" + params); } if (u.getProtocol().toLowerCase().equals("https")) { trustAllHosts(); HttpsURLConnection https = (HttpsURLConnection) u.openConnection(); https.setHostnameVerifier(doNotVerify); uc = https; } else { uc = (HttpURLConnection) u.openConnection(); } uc.setRequestProperty("Host", host); uc.setRequestProperty("Accept", "application/json, text/javascript, */*; q=0.01"); if (methodPost) { uc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); uc.setRequestProperty("Content-Length", Integer.toString(params.length())); uc.setRequestProperty("X-HTTP-Method-Override", "GET"); } else { uc.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); } uc.setRequestProperty("X-Requested-With", "XMLHttpRequest"); connection = (HttpURLConnection) uc; connection.setReadTimeout(timeout); connection.setRequestMethod(method); HttpURLConnection.setFollowRedirects(false); // TODO: Fix these (FilCab) connection.setDoInput(true); if (methodPost) { connection.setDoOutput(true); final OutputStream out = connection.getOutputStream(); final OutputStreamWriter wr = new OutputStreamWriter(out); wr.write(params); wr.flush(); wr.close(); } else { connection.setDoOutput(false); } InputStream ins = getInputstreamFromConnection(connection); final InputStreamReader inr = new InputStreamReader(ins); final BufferedReader br = new BufferedReader(inr, 1024); readIntoBuffer(br, buffer); httpCode = connection.getResponseCode(); final String paramsLog = params.replaceAll(passMatch, "password=***"); Log.i(cgSettings.tag + " | JSON", "[POST " + (int) (params.length() / 1024) + "k | " + httpCode + " | " + (int) (buffer.length() / 1024) + "k] Downloaded " + "http://" + host + path + "?" + paramsLog); connection.disconnect(); br.close(); ins.close(); inr.close(); } catch (IOException e) { httpCode = connection.getResponseCode(); Log.e(cgSettings.tag, "cgeoBase.requestJSON.IOException: " + httpCode + ": " + connection.getResponseMessage() + " ~ " + e.toString()); } } catch (Exception e) { Log.e(cgSettings.tag, "cgeoBase.requestJSON: " + e.toString()); } if (StringUtils.isNotBlank(buffer)) { break; } if (httpCode == 403) { // we're not allowed to download content, so let's move break; } } String page = null; //This is reported as beeing deadCode (httpLocation is always null) //2011-08-09 - 302 is redirect so something should probably be done /* * if (httpCode == 302 && httpLocation != null) { * final Uri newLocation = Uri.parse(httpLocation); * if (newLocation.isRelative()) { * page = requestJSONgc(host, path, params); * } else { * page = requestJSONgc(newLocation.getHost(), newLocation.getPath(), params); * } * } else { */ replaceWhitespace(buffer); page = buffer.toString(); //} if (page != null) { return page; } else { return ""; } }
From source file:com.diablominer.DiabloMiner.NetworkState.JSONRPCNetworkState.java
JsonNode doJSONRPCCall(boolean longPoll, ObjectNode message) throws IOException { HttpURLConnection connection = null; try {//from w w w .ja va 2s .c o m URL url; if (longPoll) url = longPollUrl; else url = queryUrl; Proxy proxy = diabloMiner.getProxy(); if (proxy == null) connection = (HttpURLConnection) url.openConnection(); else connection = (HttpURLConnection) url.openConnection(proxy); if (longPoll) { connection.setConnectTimeout(10 * 60 * 1000); connection.setReadTimeout(10 * 60 * 1000); } else { connection.setConnectTimeout(15 * 1000); connection.setReadTimeout(15 * 1000); } connection.setRequestProperty("Authorization", userPass); connection.setRequestProperty("Accept", "application/json"); connection.setRequestProperty("Accept-Encoding", "gzip,deflate"); connection.setRequestProperty("Content-Type", "application/json"); connection.setRequestProperty("Cache-Control", "no-cache"); connection.setRequestProperty("User-Agent", "DiabloMiner"); connection.setRequestProperty("X-Mining-Extensions", "longpoll rollntime switchto"); connection.setDoOutput(true); OutputStream requestStream = connection.getOutputStream(); Writer request = new OutputStreamWriter(requestStream); request.write(message.toString()); request.close(); requestStream.close(); ObjectNode responseMessage = null; InputStream responseStream = null; try { String xLongPolling = connection.getHeaderField("X-Long-Polling"); if (xLongPolling != null && !"".equals(xLongPolling) && longPollAsync == null) { if (xLongPolling.startsWith("http")) longPollUrl = new URL(xLongPolling); else if (xLongPolling.startsWith("/")) longPollUrl = new URL(queryUrl.getProtocol(), queryUrl.getHost(), queryUrl.getPort(), xLongPolling); else longPollUrl = new URL(queryUrl.getProtocol(), queryUrl.getHost(), queryUrl.getPort(), (url.getFile() + "/" + xLongPolling).replace("//", "/")); longPollAsync = new LongPollAsync(); Thread thread = new Thread(longPollAsync, "DiabloMiner JSONRPC LongPollAsync for " + url.getHost()); thread.start(); diabloMiner.addThread(thread); workLifetime = 60000; diabloMiner.debug(queryUrl.getHost() + ": Enabling long poll support"); } String xRollNTime = connection.getHeaderField("X-Roll-NTime"); if (xRollNTime != null && !"".equals(xRollNTime)) { if (!"n".equalsIgnoreCase(xRollNTime) && rollNTime == false) { rollNTime = true; if (xRollNTime.startsWith("expire=")) { try { workLifetime = Integer.parseInt(xRollNTime.substring(7)) * 1000; } catch (NumberFormatException ex) { } } else { workLifetime = 60000; } diabloMiner.debug(queryUrl.getHost() + ": Enabling roll ntime support, expire after " + (workLifetime / 1000) + " seconds"); } else if ("n".equalsIgnoreCase(xRollNTime) && rollNTime == true) { rollNTime = false; if (longPoll) workLifetime = 60000; else workLifetime = diabloMiner.getWorkLifetime(); diabloMiner.debug(queryUrl.getHost() + ": Disabling roll ntime support"); } } String xSwitchTo = connection.getHeaderField("X-Switch-To"); if (xSwitchTo != null && !"".equals(xSwitchTo)) { String oldHost = queryUrl.getHost(); JsonNode newHost = mapper.readTree(xSwitchTo); queryUrl = new URL(queryUrl.getProtocol(), newHost.get("host").asText(), newHost.get("port").getIntValue(), queryUrl.getPath()); if (longPollUrl != null) longPollUrl = new URL(longPollUrl.getProtocol(), newHost.get("host").asText(), newHost.get("port").getIntValue(), longPollUrl.getPath()); diabloMiner.info(oldHost + ": Switched to " + queryUrl.getHost()); } String xRejectReason = connection.getHeaderField("X-Reject-Reason"); if (xRejectReason != null && !"".equals(xRejectReason)) { rejectReason = xRejectReason; } String xIsP2Pool = connection.getHeaderField("X-Is-P2Pool"); if (xIsP2Pool != null && !"".equals(xIsP2Pool)) { if (!noDelay) diabloMiner.info("P2Pool no delay mode enabled"); noDelay = true; } if (connection.getContentEncoding() != null) { if (connection.getContentEncoding().equalsIgnoreCase("gzip")) responseStream = new GZIPInputStream(connection.getInputStream()); else if (connection.getContentEncoding().equalsIgnoreCase("deflate")) responseStream = new InflaterInputStream(connection.getInputStream()); } else { responseStream = connection.getInputStream(); } if (responseStream == null) throw new IOException("Drop to error handler"); Object output = mapper.readTree(responseStream); if (NullNode.class.equals(output.getClass())) { throw new IOException("Bitcoin returned unparsable JSON"); } else { try { responseMessage = (ObjectNode) output; } catch (ClassCastException e) { throw new IOException("Bitcoin returned unparsable JSON"); } } responseStream.close(); } catch (JsonProcessingException e) { throw new IOException("Bitcoin returned unparsable JSON"); } catch (IOException e) { InputStream errorStream = null; IOException e2 = null; if (connection.getErrorStream() == null) throw new IOException("Bitcoin disconnected during response: " + connection.getResponseCode() + " " + connection.getResponseMessage()); if (connection.getContentEncoding() != null) { if (connection.getContentEncoding().equalsIgnoreCase("gzip")) errorStream = new GZIPInputStream(connection.getErrorStream()); else if (connection.getContentEncoding().equalsIgnoreCase("deflate")) errorStream = new InflaterInputStream(connection.getErrorStream()); } else { errorStream = connection.getErrorStream(); } if (errorStream == null) throw new IOException("Bitcoin disconnected during response: " + connection.getResponseCode() + " " + connection.getResponseMessage()); byte[] errorbuf = new byte[8192]; if (errorStream.read(errorbuf) < 1) throw new IOException("Bitcoin returned an error, but with no message"); String error = new String(errorbuf).trim(); if (error.startsWith("{")) { try { Object output = mapper.readTree(error); if (NullNode.class.equals(output.getClass())) throw new IOException("Bitcoin returned an error message: " + error); else try { responseMessage = (ObjectNode) output; } catch (ClassCastException f) { throw new IOException("Bitcoin returned unparsable JSON"); } if (responseMessage.get("error") != null) { if (responseMessage.get("error").get("message") != null && responseMessage.get("error").get("message").asText() != null) { error = responseMessage.get("error").get("message").asText().trim(); e2 = new IOException("Bitcoin returned error message: " + error); } else if (responseMessage.get("error").asText() != null) { error = responseMessage.get("error").asText().trim(); if (!"null".equals(error) && !"".equals(error)) e2 = new IOException("Bitcoin returned an error message: " + error); } } } catch (JsonProcessingException f) { e2 = new IOException("Bitcoin returned unparsable JSON"); } } else { e2 = new IOException("Bitcoin returned an error message: " + error); } errorStream.close(); if (responseStream != null) responseStream.close(); if (e2 == null) e2 = new IOException("Bitcoin returned an error, but with no message"); throw e2; } if (responseMessage.get("error") != null) { if (responseMessage.get("error").get("message") != null && responseMessage.get("error").get("message").asText() != null) { String error = responseMessage.get("error").get("message").asText().trim(); throw new IOException("Bitcoin returned error message: " + error); } else if (responseMessage.get("error").asText() != null) { String error = responseMessage.get("error").asText().trim(); if (!"null".equals(error) && !"".equals(error)) throw new IOException("Bitcoin returned error message: " + error); } } JsonNode result; try { result = responseMessage.get("result"); } catch (Exception e) { throw new IOException("Bitcoin returned unparsable JSON"); } if (result == null) throw new IOException("Bitcoin did not return a result or an error"); return result; } catch (IOException e) { if (connection != null) connection.disconnect(); throw e; } }
From source file:cgeo.geocaching.cgBase.java
public cgResponse request(boolean secure, String host, String path, String method, String params, int requestId, Boolean xContentType) {//from www .j a v a 2 s . c o m URL u = null; int httpCode = -1; String httpMessage = null; String httpLocation = null; if (requestId == 0) { requestId = (int) (Math.random() * 1000); } if (method == null || (method.equalsIgnoreCase("GET") == false && method.equalsIgnoreCase("POST") == false)) { method = "POST"; } else { method = method.toUpperCase(); } // https String scheme = "http://"; if (secure) { scheme = "https://"; } String cookiesDone = CookieJar.getCookiesAsString(prefs); URLConnection uc = null; HttpURLConnection connection = null; Integer timeout = 30000; StringBuffer buffer = null; for (int i = 0; i < 5; i++) { if (i > 0) { Log.w(cgSettings.tag, "Failed to download data, retrying. Attempt #" + (i + 1)); } buffer = new StringBuffer(); timeout = 30000 + (i * 10000); try { if (method.equals("GET")) { // GET u = new URL(scheme + host + path + "?" + params); uc = u.openConnection(); uc.setRequestProperty("Host", host); uc.setRequestProperty("Cookie", cookiesDone); if (xContentType) { uc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); } if (settings.asBrowser == 1) { uc.setRequestProperty("Accept", "application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"); // uc.setRequestProperty("Accept-Encoding", "gzip"); // not supported via cellular network uc.setRequestProperty("Accept-Charset", "utf-8, iso-8859-1, utf-16, *;q=0.7"); uc.setRequestProperty("Accept-Language", "en-US"); uc.setRequestProperty("User-Agent", idBrowser); uc.setRequestProperty("Connection", "keep-alive"); uc.setRequestProperty("Keep-Alive", "300"); } connection = (HttpURLConnection) uc; connection.setReadTimeout(timeout); connection.setRequestMethod(method); HttpURLConnection.setFollowRedirects(false); connection.setDoInput(true); connection.setDoOutput(false); } else { // POST u = new URL(scheme + host + path); uc = u.openConnection(); uc.setRequestProperty("Host", host); uc.setRequestProperty("Cookie", cookiesDone); if (xContentType) { uc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); } if (settings.asBrowser == 1) { uc.setRequestProperty("Accept", "application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"); // uc.setRequestProperty("Accept-Encoding", "gzip"); // not supported via cellular network uc.setRequestProperty("Accept-Charset", "utf-8, iso-8859-1, utf-16, *;q=0.7"); uc.setRequestProperty("Accept-Language", "en-US"); uc.setRequestProperty("User-Agent", idBrowser); uc.setRequestProperty("Connection", "keep-alive"); uc.setRequestProperty("Keep-Alive", "300"); } connection = (HttpURLConnection) uc; connection.setReadTimeout(timeout); connection.setRequestMethod(method); HttpURLConnection.setFollowRedirects(false); connection.setDoInput(true); connection.setDoOutput(true); final OutputStream out = connection.getOutputStream(); final OutputStreamWriter wr = new OutputStreamWriter(out); wr.write(params); wr.flush(); wr.close(); } CookieJar.setCookies(prefs, uc); InputStream ins = getInputstreamFromConnection(connection); final InputStreamReader inr = new InputStreamReader(ins); final BufferedReader br = new BufferedReader(inr, 16 * 1024); readIntoBuffer(br, buffer); httpCode = connection.getResponseCode(); httpMessage = connection.getResponseMessage(); httpLocation = uc.getHeaderField("Location"); final String paramsLog = params.replaceAll(passMatch, "password=***"); Log.i(cgSettings.tag + "|" + requestId, "[" + method + " " + (int) (params.length() / 1024) + "k | " + httpCode + " | " + (int) (buffer.length() / 1024) + "k] Downloaded " + scheme + host + path + "?" + paramsLog); connection.disconnect(); br.close(); ins.close(); inr.close(); } catch (IOException e) { Log.e(cgSettings.tag, "cgeoBase.request.IOException: " + e.toString()); } catch (Exception e) { Log.e(cgSettings.tag, "cgeoBase.request: " + e.toString()); } if (buffer.length() > 0) { break; } } cgResponse response = new cgResponse(); try { if (httpCode == 302 && httpLocation != null) { final Uri newLocation = Uri.parse(httpLocation); if (newLocation.isRelative()) { response = request(secure, host, path, "GET", new HashMap<String, String>(), requestId, false, false, false); } else { boolean secureRedir = false; if (newLocation.getScheme().equals("https")) { secureRedir = true; } response = request(secureRedir, newLocation.getHost(), newLocation.getPath(), "GET", new HashMap<String, String>(), requestId, false, false, false); } } else { if (StringUtils.isNotEmpty(buffer)) { replaceWhitespace(buffer); String data = buffer.toString(); buffer = null; if (data != null) { response.setData(data); } else { response.setData(""); } response.setStatusCode(httpCode); response.setStatusMessage(httpMessage); response.setUrl(u.toString()); } } } catch (Exception e) { Log.e(cgSettings.tag, "cgeoBase.page: " + e.toString()); } return response; }
From source file:carnero.cgeo.original.libs.Base.java
public String requestJSON(String scheme, String host, String path, String method, String params) { int httpCode = -1; String httpLocation = null;//from w ww . j a va 2 s . c o m if (method == null) { method = "GET"; } else { method = method.toUpperCase(); } boolean methodPost = false; if (method.equalsIgnoreCase("POST")) { methodPost = true; } URLConnection uc = null; HttpURLConnection connection = null; Integer timeout = 30000; final StringBuffer buffer = new StringBuffer(); for (int i = 0; i < 3; i++) { if (i > 0) { Log.w(Settings.tag, "Failed to download data, retrying. Attempt #" + (i + 1)); } buffer.delete(0, buffer.length()); timeout = 30000 + (i * 15000); try { try { URL u = null; if (methodPost) { u = new URL(scheme + host + path); } else { u = new URL(scheme + host + path + "?" + params); } if (u.getProtocol().toLowerCase().equals("https")) { trustAllHosts(); HttpsURLConnection https = (HttpsURLConnection) u.openConnection(); https.setHostnameVerifier(doNotVerify); uc = https; } else { uc = (HttpURLConnection) u.openConnection(); } uc.setRequestProperty("Host", host); uc.setRequestProperty("Accept", "application/json, text/javascript, */*; q=0.01"); if (methodPost) { uc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); uc.setRequestProperty("Content-Length", Integer.toString(params.length())); uc.setRequestProperty("X-HTTP-Method-Override", "GET"); } else { uc.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); } uc.setRequestProperty("X-Requested-With", "XMLHttpRequest"); connection = (HttpURLConnection) uc; connection.setReadTimeout(timeout); connection.setRequestMethod(method); HttpURLConnection.setFollowRedirects(false); // TODO: Fix these (FilCab) connection.setDoInput(true); if (methodPost) { connection.setDoOutput(true); final OutputStream out = connection.getOutputStream(); final OutputStreamWriter wr = new OutputStreamWriter(out); wr.write(params); wr.flush(); wr.close(); } else { connection.setDoOutput(false); } final String encoding = connection.getContentEncoding(); InputStream ins; if (encoding != null && encoding.equalsIgnoreCase("gzip")) { ins = new GZIPInputStream(connection.getInputStream()); } else if (encoding != null && encoding.equalsIgnoreCase("deflate")) { ins = new InflaterInputStream(connection.getInputStream(), new Inflater(true)); } else { ins = connection.getInputStream(); } final InputStreamReader inr = new InputStreamReader(ins); final BufferedReader br = new BufferedReader(inr); readIntoBuffer(br, buffer); httpCode = connection.getResponseCode(); final String paramsLog = params.replaceAll(passMatch, "password=***"); Log.i(Settings.tag + " | JSON", "[POST " + (int) (params.length() / 1024) + "k | " + httpCode + " | " + (int) (buffer.length() / 1024) + "k] Downloaded " + "http://" + host + path + "?" + paramsLog); connection.disconnect(); br.close(); ins.close(); inr.close(); } catch (IOException e) { httpCode = connection.getResponseCode(); Log.e(Settings.tag, "cgeoBase.requestJSON.IOException: " + httpCode + ": " + connection.getResponseMessage() + " ~ " + e.toString()); } } catch (Exception e) { Log.e(Settings.tag, "cgeoBase.requestJSON: " + e.toString()); } if (buffer != null && buffer.length() > 0) { break; } if (httpCode == 403) { // we're not allowed to download content, so let's move break; } } String page = null; if (httpCode == 302 && httpLocation != null) { final Uri newLocation = Uri.parse(httpLocation); if (newLocation.isRelative() == true) { page = requestJSONgc(host, path, params); } else { page = requestJSONgc(newLocation.getHost(), newLocation.getPath(), params); } } else { page = replaceWhitespace(buffer); } if (page != null) { return page; } else { return ""; } }
From source file:carnero.cgeo.cgBase.java
public String requestJSON(String scheme, String host, String path, String method, String params) { int httpCode = -1; String httpLocation = null;/* w w w . j ava2 s . c o m*/ if (method == null) { method = "GET"; } else { method = method.toUpperCase(); } boolean methodPost = false; if (method.equalsIgnoreCase("POST")) { methodPost = true; } URLConnection uc = null; HttpURLConnection connection = null; Integer timeout = 30000; final StringBuffer buffer = new StringBuffer(); for (int i = 0; i < 3; i++) { if (i > 0) { Log.w(cgSettings.tag, "Failed to download data, retrying. Attempt #" + (i + 1)); } buffer.delete(0, buffer.length()); timeout = 30000 + (i * 15000); try { try { URL u = null; if (methodPost) { u = new URL(scheme + host + path); } else { u = new URL(scheme + host + path + "?" + params); } if (u.getProtocol().toLowerCase().equals("https")) { trustAllHosts(); HttpsURLConnection https = (HttpsURLConnection) u.openConnection(); https.setHostnameVerifier(doNotVerify); uc = https; } else { uc = (HttpURLConnection) u.openConnection(); } uc.setRequestProperty("Host", host); uc.setRequestProperty("Accept", "application/json, text/javascript, */*; q=0.01"); if (methodPost) { uc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); uc.setRequestProperty("Content-Length", Integer.toString(params.length())); uc.setRequestProperty("X-HTTP-Method-Override", "GET"); } else { uc.setRequestProperty("Content-Type", "application/json; charset=UTF-8"); } uc.setRequestProperty("X-Requested-With", "XMLHttpRequest"); connection = (HttpURLConnection) uc; connection.setReadTimeout(timeout); connection.setRequestMethod(method); HttpURLConnection.setFollowRedirects(false); // TODO: Fix these (FilCab) connection.setDoInput(true); if (methodPost) { connection.setDoOutput(true); final OutputStream out = connection.getOutputStream(); final OutputStreamWriter wr = new OutputStreamWriter(out); wr.write(params); wr.flush(); wr.close(); } else { connection.setDoOutput(false); } final String encoding = connection.getContentEncoding(); InputStream ins; if (encoding != null && encoding.equalsIgnoreCase("gzip")) { ins = new GZIPInputStream(connection.getInputStream()); } else if (encoding != null && encoding.equalsIgnoreCase("deflate")) { ins = new InflaterInputStream(connection.getInputStream(), new Inflater(true)); } else { ins = connection.getInputStream(); } final InputStreamReader inr = new InputStreamReader(ins); final BufferedReader br = new BufferedReader(inr); readIntoBuffer(br, buffer); httpCode = connection.getResponseCode(); final String paramsLog = params.replaceAll(passMatch, "password=***"); Log.i(cgSettings.tag + " | JSON", "[POST " + (int) (params.length() / 1024) + "k | " + httpCode + " | " + (int) (buffer.length() / 1024) + "k] Downloaded " + "http://" + host + path + "?" + paramsLog); connection.disconnect(); br.close(); ins.close(); inr.close(); } catch (IOException e) { httpCode = connection.getResponseCode(); Log.e(cgSettings.tag, "cgeoBase.requestJSON.IOException: " + httpCode + ": " + connection.getResponseMessage() + " ~ " + e.toString()); } } catch (Exception e) { Log.e(cgSettings.tag, "cgeoBase.requestJSON: " + e.toString()); } if (buffer != null && buffer.length() > 0) { break; } if (httpCode == 403) { // we're not allowed to download content, so let's move break; } } String page = null; if (httpCode == 302 && httpLocation != null) { final Uri newLocation = Uri.parse(httpLocation); if (newLocation.isRelative() == true) { page = requestJSONgc(host, path, params); } else { page = requestJSONgc(newLocation.getHost(), newLocation.getPath(), params); } } else { page = replaceWhitespace(buffer); } if (page != null) { return page; } else { return ""; } }
From source file:carnero.cgeo.original.libs.Base.java
public Response request(boolean secure, String host, String path, String method, String params, int requestId, Boolean xContentType) {//from w w w . java2 s.co m URL u = null; int httpCode = -1; String httpMessage = null; String httpLocation = null; if (requestId == 0) { requestId = (int) (Math.random() * 1000); } if (method == null || (method.equalsIgnoreCase("GET") == false && method.equalsIgnoreCase("POST") == false)) { method = "POST"; } else { method = method.toUpperCase(); } // https String scheme = "http://"; if (secure) { scheme = "https://"; } // prepare cookies String cookiesDone = null; if (cookies == null || cookies.isEmpty() == true) { if (cookies == null) { cookies = new HashMap<String, String>(); } final Map<String, ?> prefsAll = prefs.getAll(); final Set<String> prefsKeys = prefsAll.keySet(); for (String key : prefsKeys) { if (key.matches("cookie_.+") == true) { final String cookieKey = key.substring(7); final String cookieValue = (String) prefsAll.get(key); cookies.put(cookieKey, cookieValue); } } } if (cookies != null && !cookies.isEmpty() && cookies.keySet().size() > 0) { final Object[] keys = cookies.keySet().toArray(); final ArrayList<String> cookiesEncoded = new ArrayList<String>(); for (int i = 0; i < keys.length; i++) { String value = cookies.get(keys[i].toString()); cookiesEncoded.add(keys[i] + "=" + value); } if (cookiesEncoded.size() > 0) { cookiesDone = implode("; ", cookiesEncoded.toArray()); } } if (cookiesDone == null) { Map<String, ?> prefsValues = prefs.getAll(); if (prefsValues != null && prefsValues.size() > 0 && prefsValues.keySet().size() > 0) { final Object[] keys = prefsValues.keySet().toArray(); final ArrayList<String> cookiesEncoded = new ArrayList<String>(); final int length = keys.length; for (int i = 0; i < length; i++) { if (keys[i].toString().length() > 7 && keys[i].toString().substring(0, 7).equals("cookie_") == true) { cookiesEncoded .add(keys[i].toString().substring(7) + "=" + prefsValues.get(keys[i].toString())); } } if (cookiesEncoded.size() > 0) { cookiesDone = implode("; ", cookiesEncoded.toArray()); } } } if (cookiesDone == null) { cookiesDone = ""; } URLConnection uc = null; HttpURLConnection connection = null; Integer timeout = 30000; StringBuffer buffer = null; for (int i = 0; i < 5; i++) { if (i > 0) { Log.w(Settings.tag, "Failed to download data, retrying. Attempt #" + (i + 1)); } buffer = new StringBuffer(); timeout = 30000 + (i * 10000); try { if (method.equals("GET")) { // GET u = new URL(scheme + host + path + "?" + params); uc = u.openConnection(); uc.setRequestProperty("Host", host); uc.setRequestProperty("Cookie", cookiesDone); if (xContentType == true) { uc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); } if (settings.asBrowser == 1) { uc.setRequestProperty("Accept", "application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"); // uc.setRequestProperty("Accept-Encoding", "gzip"); // not supported via cellular network uc.setRequestProperty("Accept-Charset", "utf-8, iso-8859-1, utf-16, *;q=0.7"); uc.setRequestProperty("Accept-Language", "en-US"); uc.setRequestProperty("User-Agent", idBrowser); uc.setRequestProperty("Connection", "keep-alive"); uc.setRequestProperty("Keep-Alive", "300"); } connection = (HttpURLConnection) uc; connection.setReadTimeout(timeout); connection.setRequestMethod(method); HttpURLConnection.setFollowRedirects(false); connection.setDoInput(true); connection.setDoOutput(false); } else { // POST u = new URL(scheme + host + path); uc = u.openConnection(); uc.setRequestProperty("Host", host); uc.setRequestProperty("Cookie", cookiesDone); if (xContentType == true) { uc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); } if (settings.asBrowser == 1) { uc.setRequestProperty("Accept", "application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"); // uc.setRequestProperty("Accept-Encoding", "gzip"); // not supported via cellular network uc.setRequestProperty("Accept-Charset", "utf-8, iso-8859-1, utf-16, *;q=0.7"); uc.setRequestProperty("Accept-Language", "en-US"); uc.setRequestProperty("User-Agent", idBrowser); uc.setRequestProperty("Connection", "keep-alive"); uc.setRequestProperty("Keep-Alive", "300"); } connection = (HttpURLConnection) uc; connection.setReadTimeout(timeout); connection.setRequestMethod(method); HttpURLConnection.setFollowRedirects(false); connection.setDoInput(true); connection.setDoOutput(true); final OutputStream out = connection.getOutputStream(); final OutputStreamWriter wr = new OutputStreamWriter(out); wr.write(params); wr.flush(); wr.close(); } String headerName = null; final SharedPreferences.Editor prefsEditor = prefs.edit(); for (int j = 1; (headerName = uc.getHeaderFieldKey(j)) != null; j++) { if (headerName != null && headerName.equalsIgnoreCase("Set-Cookie")) { int index; String cookie = uc.getHeaderField(j); index = cookie.indexOf(";"); if (index > -1) { cookie = cookie.substring(0, cookie.indexOf(";")); } index = cookie.indexOf("="); if (index > -1 && cookie.length() > (index + 1)) { String name = cookie.substring(0, cookie.indexOf("=")); String value = cookie.substring(cookie.indexOf("=") + 1, cookie.length()); cookies.put(name, value); prefsEditor.putString("cookie_" + name, value); } } } prefsEditor.commit(); final String encoding = connection.getContentEncoding(); InputStream ins; if (encoding != null && encoding.equalsIgnoreCase("gzip")) { ins = new GZIPInputStream(connection.getInputStream()); } else if (encoding != null && encoding.equalsIgnoreCase("deflate")) { ins = new InflaterInputStream(connection.getInputStream(), new Inflater(true)); } else { ins = connection.getInputStream(); } final InputStreamReader inr = new InputStreamReader(ins); final BufferedReader br = new BufferedReader(inr); readIntoBuffer(br, buffer); httpCode = connection.getResponseCode(); httpMessage = connection.getResponseMessage(); httpLocation = uc.getHeaderField("Location"); final String paramsLog = params.replaceAll(passMatch, "password=***"); if (buffer != null && connection != null) { Log.i(Settings.tag + "|" + requestId, "[" + method + " " + (int) (params.length() / 1024) + "k | " + httpCode + " | " + (int) (buffer.length() / 1024) + "k] Downloaded " + scheme + host + path + "?" + paramsLog); } else { Log.i(Settings.tag + "|" + requestId, "[" + method + " | " + httpCode + "] Failed to download " + scheme + host + path + "?" + paramsLog); } connection.disconnect(); br.close(); ins.close(); inr.close(); } catch (IOException e) { Log.e(Settings.tag, "cgeoBase.request.IOException: " + e.toString()); } catch (Exception e) { Log.e(Settings.tag, "cgeoBase.request: " + e.toString()); } if (buffer != null && buffer.length() > 0) { break; } } Response response = new Response(); String data = null; try { if (httpCode == 302 && httpLocation != null) { final Uri newLocation = Uri.parse(httpLocation); if (newLocation.isRelative() == true) { response = request(secure, host, path, "GET", new HashMap<String, String>(), requestId, false, false, false); } else { boolean secureRedir = false; if (newLocation.getScheme().equals("https")) { secureRedir = true; } response = request(secureRedir, newLocation.getHost(), newLocation.getPath(), "GET", new HashMap<String, String>(), requestId, false, false, false); } } else { if (buffer != null && buffer.length() > 0) { data = replaceWhitespace(buffer); buffer = null; if (data != null) { response.setData(data); } else { response.setData(""); } response.setStatusCode(httpCode); response.setStatusMessage(httpMessage); response.setUrl(u.toString()); } } } catch (Exception e) { Log.e(Settings.tag, "cgeoBase.page: " + e.toString()); } return response; }
From source file:carnero.cgeo.cgBase.java
public cgResponse request(boolean secure, String host, String path, String method, String params, int requestId, Boolean xContentType) {// w ww.ja va 2s .com URL u = null; int httpCode = -1; String httpMessage = null; String httpLocation = null; if (requestId == 0) { requestId = (int) (Math.random() * 1000); } if (method == null || (method.equalsIgnoreCase("GET") == false && method.equalsIgnoreCase("POST") == false)) { method = "POST"; } else { method = method.toUpperCase(); } // https String scheme = "http://"; if (secure) { scheme = "https://"; } // prepare cookies String cookiesDone = null; if (cookies == null || cookies.isEmpty() == true) { if (cookies == null) { cookies = new HashMap<String, String>(); } final Map<String, ?> prefsAll = prefs.getAll(); final Set<String> prefsKeys = prefsAll.keySet(); for (String key : prefsKeys) { if (key.matches("cookie_.+") == true) { final String cookieKey = key.substring(7); final String cookieValue = (String) prefsAll.get(key); cookies.put(cookieKey, cookieValue); } } } if (cookies != null && !cookies.isEmpty() && cookies.keySet().size() > 0) { final Object[] keys = cookies.keySet().toArray(); final ArrayList<String> cookiesEncoded = new ArrayList<String>(); for (int i = 0; i < keys.length; i++) { String value = cookies.get(keys[i].toString()); cookiesEncoded.add(keys[i] + "=" + value); } if (cookiesEncoded.size() > 0) { cookiesDone = implode("; ", cookiesEncoded.toArray()); } } if (cookiesDone == null) { Map<String, ?> prefsValues = prefs.getAll(); if (prefsValues != null && prefsValues.size() > 0 && prefsValues.keySet().size() > 0) { final Object[] keys = prefsValues.keySet().toArray(); final ArrayList<String> cookiesEncoded = new ArrayList<String>(); final int length = keys.length; for (int i = 0; i < length; i++) { if (keys[i].toString().length() > 7 && keys[i].toString().substring(0, 7).equals("cookie_") == true) { cookiesEncoded .add(keys[i].toString().substring(7) + "=" + prefsValues.get(keys[i].toString())); } } if (cookiesEncoded.size() > 0) { cookiesDone = implode("; ", cookiesEncoded.toArray()); } } } if (cookiesDone == null) { cookiesDone = ""; } URLConnection uc = null; HttpURLConnection connection = null; Integer timeout = 30000; StringBuffer buffer = null; for (int i = 0; i < 5; i++) { if (i > 0) { Log.w(cgSettings.tag, "Failed to download data, retrying. Attempt #" + (i + 1)); } buffer = new StringBuffer(); timeout = 30000 + (i * 10000); try { if (method.equals("GET")) { // GET u = new URL(scheme + host + path + "?" + params); uc = u.openConnection(); uc.setRequestProperty("Host", host); uc.setRequestProperty("Cookie", cookiesDone); if (xContentType == true) { uc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); } if (settings.asBrowser == 1) { uc.setRequestProperty("Accept", "application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"); // uc.setRequestProperty("Accept-Encoding", "gzip"); // not supported via cellular network uc.setRequestProperty("Accept-Charset", "utf-8, iso-8859-1, utf-16, *;q=0.7"); uc.setRequestProperty("Accept-Language", "en-US"); uc.setRequestProperty("User-Agent", idBrowser); uc.setRequestProperty("Connection", "keep-alive"); uc.setRequestProperty("Keep-Alive", "300"); } connection = (HttpURLConnection) uc; connection.setReadTimeout(timeout); connection.setRequestMethod(method); HttpURLConnection.setFollowRedirects(false); connection.setDoInput(true); connection.setDoOutput(false); } else { // POST u = new URL(scheme + host + path); uc = u.openConnection(); uc.setRequestProperty("Host", host); uc.setRequestProperty("Cookie", cookiesDone); if (xContentType == true) { uc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); } if (settings.asBrowser == 1) { uc.setRequestProperty("Accept", "application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5"); // uc.setRequestProperty("Accept-Encoding", "gzip"); // not supported via cellular network uc.setRequestProperty("Accept-Charset", "utf-8, iso-8859-1, utf-16, *;q=0.7"); uc.setRequestProperty("Accept-Language", "en-US"); uc.setRequestProperty("User-Agent", idBrowser); uc.setRequestProperty("Connection", "keep-alive"); uc.setRequestProperty("Keep-Alive", "300"); } connection = (HttpURLConnection) uc; connection.setReadTimeout(timeout); connection.setRequestMethod(method); HttpURLConnection.setFollowRedirects(false); connection.setDoInput(true); connection.setDoOutput(true); final OutputStream out = connection.getOutputStream(); final OutputStreamWriter wr = new OutputStreamWriter(out); wr.write(params); wr.flush(); wr.close(); } String headerName = null; final SharedPreferences.Editor prefsEditor = prefs.edit(); for (int j = 1; (headerName = uc.getHeaderFieldKey(j)) != null; j++) { if (headerName != null && headerName.equalsIgnoreCase("Set-Cookie")) { int index; String cookie = uc.getHeaderField(j); index = cookie.indexOf(";"); if (index > -1) { cookie = cookie.substring(0, cookie.indexOf(";")); } index = cookie.indexOf("="); if (index > -1 && cookie.length() > (index + 1)) { String name = cookie.substring(0, cookie.indexOf("=")); String value = cookie.substring(cookie.indexOf("=") + 1, cookie.length()); cookies.put(name, value); prefsEditor.putString("cookie_" + name, value); } } } prefsEditor.commit(); final String encoding = connection.getContentEncoding(); InputStream ins; if (encoding != null && encoding.equalsIgnoreCase("gzip")) { ins = new GZIPInputStream(connection.getInputStream()); } else if (encoding != null && encoding.equalsIgnoreCase("deflate")) { ins = new InflaterInputStream(connection.getInputStream(), new Inflater(true)); } else { ins = connection.getInputStream(); } final InputStreamReader inr = new InputStreamReader(ins); final BufferedReader br = new BufferedReader(inr); readIntoBuffer(br, buffer); httpCode = connection.getResponseCode(); httpMessage = connection.getResponseMessage(); httpLocation = uc.getHeaderField("Location"); final String paramsLog = params.replaceAll(passMatch, "password=***"); if (buffer != null && connection != null) { Log.i(cgSettings.tag + "|" + requestId, "[" + method + " " + (int) (params.length() / 1024) + "k | " + httpCode + " | " + (int) (buffer.length() / 1024) + "k] Downloaded " + scheme + host + path + "?" + paramsLog); } else { Log.i(cgSettings.tag + "|" + requestId, "[" + method + " | " + httpCode + "] Failed to download " + scheme + host + path + "?" + paramsLog); } connection.disconnect(); br.close(); ins.close(); inr.close(); } catch (IOException e) { Log.e(cgSettings.tag, "cgeoBase.request.IOException: " + e.toString()); } catch (Exception e) { Log.e(cgSettings.tag, "cgeoBase.request: " + e.toString()); } if (buffer != null && buffer.length() > 0) { break; } } cgResponse response = new cgResponse(); String data = null; try { if (httpCode == 302 && httpLocation != null) { final Uri newLocation = Uri.parse(httpLocation); if (newLocation.isRelative() == true) { response = request(secure, host, path, "GET", new HashMap<String, String>(), requestId, false, false, false); } else { boolean secureRedir = false; if (newLocation.getScheme().equals("https")) { secureRedir = true; } response = request(secureRedir, newLocation.getHost(), newLocation.getPath(), "GET", new HashMap<String, String>(), requestId, false, false, false); } } else { if (buffer != null && buffer.length() > 0) { data = replaceWhitespace(buffer); buffer = null; if (data != null) { response.setData(data); } else { response.setData(""); } response.setStatusCode(httpCode); response.setStatusMessage(httpMessage); response.setUrl(u.toString()); } } } catch (Exception e) { Log.e(cgSettings.tag, "cgeoBase.page: " + e.toString()); } return response; }