List of usage examples for javax.net.ssl HttpsURLConnection setHostnameVerifier
public void setHostnameVerifier(HostnameVerifier v)
HostnameVerifier
for this instance. 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;/*from w w w. j a va2s . co 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 ""; } }