List of usage examples for java.net HttpURLConnection setReadTimeout
public void setReadTimeout(int timeout)
From source file:eu.vital.TrustManager.connectors.dms.DMSManager.java
private String queryDMSTest(String dms_endpoint, String body) throws UnsupportedEncodingException, IOException, KeyManagementException, NoSuchAlgorithmException, KeyStoreException { HttpURLConnection connection = null; // Of course everything will go over HTTPS (here we trust anything, we do not check the certificate) SSLContext sc = null;// w w w. j a v a 2s . co m try { sc = SSLContext.getInstance("TLS"); } catch (NoSuchAlgorithmException e1) { } InputStream is; BufferedReader rd; char cbuf[] = new char[1000000]; int len; String urlParameters = body; // test cookie is the user performing the evalaution // The array of resources to evaluate policies on must be included byte[] postData = urlParameters.getBytes(StandardCharsets.UTF_8); int postDataLength = postData.length; URL url = new URL(dms_URL + "/" + dms_endpoint); try { connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setUseCaches(false); connection.setDoOutput(true); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); connection.setRequestProperty("charset", "utf-8"); connection.setRequestProperty("Content-Length", Integer.toString(postDataLength)); connection.setConnectTimeout(5000); connection.setReadTimeout(5000); connection.setRequestProperty("Cookie", cookie); // Include cookies (permissions evaluated for normal user, advanced user has the rights to evaluate) DataOutputStream wr = new DataOutputStream(connection.getOutputStream()); wr.write(postData); wr.close(); // Get Response int err = connection.getResponseCode(); if (err >= 200 && err < 300) { is = connection.getInputStream(); // rd = new BufferedReader(new InputStreamReader(is)); // //StringBuilder rd2 = new StringBuilder(); len = rd.read(cbuf); String resp2 = String.valueOf(cbuf).substring(0, len - 1); rd.close(); return resp2; // char[] buffer = new char[1024*1024]; // StringBuilder output = new StringBuilder(); // int readLength = 0; // while (readLength != -1) { // readLength = rd.read(buffer, 0, buffer.length); // if (readLength != -1) { // output.append(buffer, 0, readLength); // } // } // return output.toString(); } } catch (Exception e) { throw new java.net.ConnectException(); //log } finally { if (connection != null) { connection.disconnect(); } } return null; }
From source file:com.facebook.GraphRequest.java
final static void serializeToUrlConnection(GraphRequestBatch requests, HttpURLConnection connection) throws IOException, JSONException { Logger logger = new Logger(LoggingBehavior.REQUESTS, "Request"); int numRequests = requests.size(); boolean shouldUseGzip = isGzipCompressible(requests); HttpMethod connectionHttpMethod = (numRequests == 1) ? requests.get(0).httpMethod : HttpMethod.POST; connection.setRequestMethod(connectionHttpMethod.name()); setConnectionContentType(connection, shouldUseGzip); URL url = connection.getURL(); logger.append("Request:\n"); logger.appendKeyValue("Id", requests.getId()); logger.appendKeyValue("URL", url); logger.appendKeyValue("Method", connection.getRequestMethod()); logger.appendKeyValue("User-Agent", connection.getRequestProperty("User-Agent")); logger.appendKeyValue("Content-Type", connection.getRequestProperty("Content-Type")); connection.setConnectTimeout(requests.getTimeout()); connection.setReadTimeout(requests.getTimeout()); // If we have a single non-POST request, don't try to serialize anything or // HttpURLConnection will turn it into a POST. boolean isPost = (connectionHttpMethod == HttpMethod.POST); if (!isPost) { logger.log();/* w ww . j a va2 s . c o m*/ return; } connection.setDoOutput(true); OutputStream outputStream = null; try { outputStream = new BufferedOutputStream(connection.getOutputStream()); if (shouldUseGzip) { outputStream = new GZIPOutputStream(outputStream); } if (hasOnProgressCallbacks(requests)) { ProgressNoopOutputStream countingStream = null; countingStream = new ProgressNoopOutputStream(requests.getCallbackHandler()); processRequest(requests, null, numRequests, url, countingStream, shouldUseGzip); int max = countingStream.getMaxProgress(); Map<GraphRequest, RequestProgress> progressMap = countingStream.getProgressMap(); outputStream = new ProgressOutputStream(outputStream, requests, progressMap, max); } processRequest(requests, logger, numRequests, url, outputStream, shouldUseGzip); } finally { if (outputStream != null) { outputStream.close(); } } logger.log(); }
From source file:de.sjka.logstash.osgi.internal.LogstashSender.java
private void process(LogEntry logEntry) { if (logEntry.getLevel() <= getLogLevelConfig()) { if (!"true".equals(getConfig(LogstashConfig.ENABLED))) { return; }/* w w w . j av a 2s.co m*/ ; for (ILogstashFilter logstashFilter : logstashFilters) { if (!logstashFilter.apply(logEntry)) { return; } } String request = getConfig(LogstashConfig.URL); if (!request.endsWith("/")) { request += "/"; } HttpURLConnection conn = null; try { JSONObject values = serializeLogEntry(logEntry); String payload = values.toJSONString(); byte[] postData = payload.getBytes(StandardCharsets.UTF_8); String username = getConfig(LogstashConfig.USERNAME); String password = getConfig(LogstashConfig.PASSWORD); String authString = username + ":" + password; byte[] authEncBytes = Base64.encodeBase64(authString.getBytes()); String authStringEnc = new String(authEncBytes); URL url = new URL(request); conn = (HttpURLConnection) url.openConnection(); if (request.startsWith("https") && "true".equals(getConfig(LogstashConfig.SSL_NO_CHECK))) { if (sslSocketFactory != null) { ((HttpsURLConnection) conn).setSSLSocketFactory(sslSocketFactory); ((HttpsURLConnection) conn).setHostnameVerifier(new HostnameVerifier() { @Override public boolean verify(String hostname, SSLSession session) { return true; } }); } } conn.setDoOutput(true); conn.setInstanceFollowRedirects(false); conn.setRequestMethod("PUT"); conn.setRequestProperty("Content-Type", "application/json"); conn.setRequestProperty("charset", "utf-8"); conn.setReadTimeout(30 * SECONDS); conn.setConnectTimeout(30 * SECONDS); if (username != null && !"".equals(username)) { conn.setRequestProperty("Authorization", "Basic " + authStringEnc); } conn.setUseCaches(false); try (DataOutputStream wr = new DataOutputStream(conn.getOutputStream())) { wr.write(postData); wr.flush(); wr.close(); } if (conn.getResponseCode() != 200) { throw new IOException( "Got response " + conn.getResponseCode() + " - " + conn.getResponseMessage()); } } catch (IOException e) { throw new RuntimeException(e); } finally { if (conn != null) { conn.disconnect(); } } } }
From source file:com.startupbidder.dao.MockDataBuilder.java
private List<String> getStartuplyIds() { List<String> startuplyIds = new ArrayList<String>(); String startuplyPath = STARTUPLY_ROOT + "/Startups/"; try {//from ww w.j a va2 s . c om StartuplyCache startuplyCache = null; try { startuplyCache = getOfy().get(StartuplyCache.class, startuplyPath); } catch (NotFoundException e) { ; } if (startuplyCache == null) { URL url = new URL(startuplyPath); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(15000); connection.setReadTimeout(15000); connection.setDoOutput(true); connection.setRequestMethod("GET"); StringWriter stringWriter = new StringWriter(); IOUtils.copy(connection.getInputStream(), stringWriter, "UTF-8"); String startuplyPage = stringWriter.toString(); connection.disconnect(); if (!StringUtils.isEmpty(startuplyPage)) { startuplyCache = new StartuplyCache(startuplyPath, startuplyPage); //getOfy().put(startuplyCache); // too large for google } else { throw new Exception("Could not load startuply main page: " + startuplyPage); } } Pattern p = Pattern.compile("<a href=\"/Companies/([^.]*)[.]aspx\">([^\\<])*</a>"); Matcher m = p.matcher(startuplyCache.page); while (m.find()) { String startuplyId = m.group(1); startuplyIds.add(startuplyId); //String startuplyName = m.group(2); //log.info("StartuplyId: " + startuplyId + " name: " + startuplyName); } } catch (Exception e) { log.log(Level.WARNING, "Exception while importing Startuply startups", e); } return startuplyIds; }
From source file:org.pixmob.httpclient.HttpRequestBuilder.java
public HttpResponse execute() throws HttpClientException { HttpURLConnection conn = null; UncloseableInputStream payloadStream = null; try {// w w w. j a v a 2s .c o m if (parameters != null && !parameters.isEmpty()) { final StringBuilder buf = new StringBuilder(256); if (HTTP_GET.equals(method) || HTTP_HEAD.equals(method)) { buf.append('?'); } int paramIdx = 0; for (final Map.Entry<String, String> e : parameters.entrySet()) { if (paramIdx != 0) { buf.append("&"); } final String name = e.getKey(); final String value = e.getValue(); buf.append(URLEncoder.encode(name, CONTENT_CHARSET)).append("=") .append(URLEncoder.encode(value, CONTENT_CHARSET)); ++paramIdx; } if (!contentSet && (HTTP_POST.equals(method) || HTTP_DELETE.equals(method) || HTTP_PUT.equals(method))) { try { content = buf.toString().getBytes(CONTENT_CHARSET); } catch (UnsupportedEncodingException e) { // Unlikely to happen. throw new HttpClientException("Encoding error", e); } } else { uri += buf; } } conn = (HttpURLConnection) new URL(uri).openConnection(); conn.setConnectTimeout(hc.getConnectTimeout()); conn.setReadTimeout(hc.getReadTimeout()); conn.setAllowUserInteraction(false); conn.setInstanceFollowRedirects(false); conn.setRequestMethod(method); conn.setUseCaches(false); conn.setDoInput(true); if (headers != null && !headers.isEmpty()) { for (final Map.Entry<String, List<String>> e : headers.entrySet()) { final List<String> values = e.getValue(); if (values != null) { final String name = e.getKey(); for (final String value : values) { conn.addRequestProperty(name, value); } } } } if (cookies != null && !cookies.isEmpty() || hc.getInMemoryCookies() != null && !hc.getInMemoryCookies().isEmpty()) { final StringBuilder cookieHeaderValue = new StringBuilder(256); prepareCookieHeader(cookies, cookieHeaderValue); prepareCookieHeader(hc.getInMemoryCookies(), cookieHeaderValue); conn.setRequestProperty("Cookie", cookieHeaderValue.toString()); } final String userAgent = hc.getUserAgent(); if (userAgent != null) { conn.setRequestProperty("User-Agent", userAgent); } conn.setRequestProperty("Connection", "close"); conn.setRequestProperty("Location", uri); conn.setRequestProperty("Referrer", uri); conn.setRequestProperty("Accept-Encoding", "gzip,deflate"); conn.setRequestProperty("Accept-Charset", CONTENT_CHARSET); if (conn instanceof HttpsURLConnection) { setupSecureConnection(hc.getContext(), (HttpsURLConnection) conn); } for (final HttpRequestHandler connHandler : reqHandlers) { try { connHandler.onRequest(conn); } catch (HttpClientException e) { throw e; } catch (Exception e) { throw new HttpClientException("Failed to prepare request to " + uri, e); } } // It seems that when writing content starts the connection if (HTTP_POST.equals(method) || HTTP_DELETE.equals(method) || HTTP_PUT.equals(method)) { if (content == null) { noContent(); } conn.setDoOutput(true); if (!contentSet) { conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=" + CONTENT_CHARSET); } else if (contentType != null) { conn.setRequestProperty("Content-Type", contentType); } conn.setFixedLengthStreamingMode(content.length); final OutputStream out = conn.getOutputStream(); out.write(content); out.flush(); } conn.connect(); final int statusCode = conn.getResponseCode(); if (statusCode == -1) { throw new HttpClientException("Invalid response from " + uri); } if (!expectedStatusCodes.isEmpty() && !expectedStatusCodes.contains(statusCode)) { throw new HttpClientException( "Expected status code " + expectedStatusCodes + ", got " + statusCode); } else if (expectedStatusCodes.isEmpty() && statusCode / 100 != 2) { throw new HttpClientException("Expected status code 2xx, got " + statusCode); } final Map<String, List<String>> headerFields = conn.getHeaderFields(); final Map<String, String> inMemoryCookies = hc.getInMemoryCookies(); if (headerFields != null) { final List<String> newCookies = headerFields.get("Set-Cookie"); if (newCookies != null) { for (final String newCookie : newCookies) { final String rawCookie = newCookie.split(";", 2)[0]; final int i = rawCookie.indexOf('='); final String name = rawCookie.substring(0, i); final String value = rawCookie.substring(i + 1); inMemoryCookies.put(name, value); } } } if (isStatusCodeError(statusCode)) { // Got an error: cannot read input. payloadStream = new UncloseableInputStream(getErrorStream(conn)); } else { payloadStream = new UncloseableInputStream(getInputStream(conn)); } final HttpResponse resp = new HttpResponse(statusCode, payloadStream, headerFields == null ? NO_HEADERS : headerFields, inMemoryCookies); if (handler != null) { try { handler.onResponse(resp); } catch (HttpClientException e) { throw e; } catch (Exception e) { throw new HttpClientException("Error in response handler", e); } } else { final File temp = File.createTempFile("httpclient-req-", ".cache", hc.getContext().getCacheDir()); resp.preload(temp); temp.delete(); } return resp; } catch (SocketTimeoutException e) { if (handler != null) { try { handler.onTimeout(); return null; } catch (HttpClientException e2) { throw e2; } catch (Exception e2) { throw new HttpClientException("Error in response handler", e2); } } else { throw new HttpClientException("Response timeout from " + uri, e); } } catch (IOException e) { throw new HttpClientException("Connection failed to " + uri, e); } finally { if (conn != null) { if (payloadStream != null) { // Fully read Http response: // http://docs.oracle.com/javase/6/docs/technotes/guides/net/http-keepalive.html try { while (payloadStream.read(buffer) != -1) { ; } } catch (IOException ignore) { } payloadStream.forceClose(); } conn.disconnect(); } } }
From source file:com.canappi.connector.yp.yhere.CouponView.java
public ArrayList<Element> getCouponsByZip(HashMap<String, String> requestParameters) { ArrayList<Element> data = new ArrayList<Element>(); System.setProperty("http.keepAlive", "false"); System.setProperty("javax.net.debug", "all"); // _FakeX509TrustManager.allowAllSSL(); //Protocol::: HTTP GET StringBuffer query = new StringBuffer(); HttpURLConnection connection = null; try {/* ww w. j a va 2 s .co m*/ URL url; if (requestParameters != null) { String key; query.append( "http://api2.yp.com/listings/v1/coupons?format=xml&key=5d0b448ba491c2dff5a36040a125df0a"); query.append("&"); key = "searchloc"; String searchlocValue = requestParameters.get(key); String searchlocDefaultValue = retrieveFromUserDefaultsFor(key); if (searchlocValue.length() > 0) { query.append("" + key + "=" + requestParameters.get(key)); } else { //try to find the value in the user defaults if (searchlocDefaultValue != null) { query.append("" + key + "=" + retrieveFromUserDefaultsFor(key)); } } url = new URL(query.toString()); } else { url = new URL( "http://api2.yp.com/listings/v1/coupons?format=xml&key=5d0b448ba491c2dff5a36040a125df0a"); } connection = (HttpURLConnection) url.openConnection(); connection.setConnectTimeout(5000); connection.setReadTimeout(5000); connection.setUseCaches(false); connection.setRequestMethod("GET"); connection.setDoOutput(true); connection.setDoInput(true); connection.connect(); int rc = connection.getResponseCode(); Log.i("Response code", String.valueOf(rc)); InputStream is; if (rc <= 400) { is = connection.getInputStream(); } else { /* error from server */ is = connection.getErrorStream(); } //XML ResultSet try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); InputSource isrc = new InputSource(); isrc.setByteStream(is); Document doc = db.parse(isrc); NodeList nodes = doc.getElementsByTagName("searchListings"); if (nodes.getLength() > 0) { Element list = (Element) nodes.item(0); NodeList l = list.getChildNodes(); for (int i = 0; i < l.getLength(); i++) { Node n = l.item(i); if (n.getNodeType() == Node.ELEMENT_NODE) { Element row = (Element) l.item(i); data.add(row); } } } } catch (Exception e) { e.printStackTrace(); } } catch (Exception e) { e.printStackTrace(); } finally { connection.disconnect(); } return data; }
From source file:com.jms.notify.utils.httpclient.SimpleHttpUtils.java
/** * * @param httpParam// w ww .jav a 2 s . c o m * @return */ public static SimpleHttpResult httpRequest(SimpleHttpParam httpParam) { String url = httpParam.getUrl(); Map<String, Object> parameters = httpParam.getParameters(); String sMethod = httpParam.getMethod(); String charSet = httpParam.getCharSet(); boolean sslVerify = httpParam.isSslVerify(); int maxResultSize = httpParam.getMaxResultSize(); Map<String, Object> headers = httpParam.getHeaders(); int readTimeout = httpParam.getReadTimeout(); int connectTimeout = httpParam.getConnectTimeout(); boolean ignoreContentIfUnsuccess = httpParam.isIgnoreContentIfUnsuccess(); boolean hostnameVerify = httpParam.isHostnameVerify(); TrustKeyStore trustKeyStore = httpParam.getTrustKeyStore(); ClientKeyStore clientKeyStore = httpParam.getClientKeyStore(); if (url == null || url.trim().length() == 0) { throw new IllegalArgumentException("invalid url : " + url); } if (maxResultSize <= 0) { throw new IllegalArgumentException("maxResultSize must be positive : " + maxResultSize); } Charset.forName(charSet); HttpURLConnection urlConn = null; URL destURL = null; String baseUrl = url.trim(); if (!baseUrl.toLowerCase().startsWith(HTTPS_PREFIX) && !baseUrl.toLowerCase().startsWith(HTTP_PREFIX)) { baseUrl = HTTP_PREFIX + baseUrl; } String method = null; if (sMethod != null) { method = sMethod.toUpperCase(); } if (method == null || !(method.equals(HTTP_METHOD_POST) || method.equals(HTTP_METHOD_GET))) { throw new IllegalArgumentException("invalid http method : " + method); } int index = baseUrl.indexOf("?"); if (index > 0) { baseUrl = urlEncode(baseUrl, charSet); } else if (index == 0) { throw new IllegalArgumentException("invalid url : " + url); } String queryString = mapToQueryString(parameters, charSet); String targetUrl = ""; if (method.equals(HTTP_METHOD_POST)) { targetUrl = baseUrl; } else { if (index > 0) { targetUrl = baseUrl + "&" + queryString; } else { targetUrl = baseUrl + "?" + queryString; } } try { destURL = new URL(targetUrl); urlConn = (HttpURLConnection) destURL.openConnection(); setSSLSocketFactory(urlConn, sslVerify, hostnameVerify, trustKeyStore, clientKeyStore); boolean hasContentType = false; boolean hasUserAgent = false; for (String key : headers.keySet()) { if ("Content-Type".equalsIgnoreCase(key)) { hasContentType = true; } if ("user-agent".equalsIgnoreCase(key)) { hasUserAgent = true; } } if (!hasContentType) { headers.put("Content-Type", "application/x-www-form-urlencoded; charset=" + charSet); } if (!hasUserAgent) { headers.put("user-agent", "PlatSystem"); } if (headers != null && !headers.isEmpty()) { for (Entry<String, Object> entry : headers.entrySet()) { String key = entry.getKey(); Object value = entry.getValue(); List<String> values = makeStringList(value); for (String v : values) { urlConn.addRequestProperty(key, v); } } } urlConn.setDoOutput(true); urlConn.setDoInput(true); urlConn.setAllowUserInteraction(false); urlConn.setUseCaches(false); urlConn.setRequestMethod(method); urlConn.setConnectTimeout(connectTimeout); urlConn.setReadTimeout(readTimeout); if (method.equals(HTTP_METHOD_POST)) { String postData = queryString.length() == 0 ? httpParam.getPostData() : queryString; if (postData != null && postData.trim().length() > 0) { OutputStream os = urlConn.getOutputStream(); OutputStreamWriter osw = new OutputStreamWriter(os, charSet); osw.write(postData); osw.flush(); osw.close(); } } int responseCode = urlConn.getResponseCode(); Map<String, List<String>> responseHeaders = urlConn.getHeaderFields(); String contentType = urlConn.getContentType(); SimpleHttpResult result = new SimpleHttpResult(responseCode); result.setHeaders(responseHeaders); result.setContentType(contentType); if (responseCode != 200 && ignoreContentIfUnsuccess) { return result; } InputStream is = urlConn.getInputStream(); byte[] temp = new byte[1024]; ByteArrayOutputStream baos = new ByteArrayOutputStream(); int readBytes = is.read(temp); while (readBytes > 0) { baos.write(temp, 0, readBytes); readBytes = is.read(temp); } String resultString = new String(baos.toByteArray(), charSet); //new String(buffer.array(), charSet); baos.close(); result.setContent(resultString); return result; } catch (Exception e) { logger.warn("connection error : " + e.getMessage()); return new SimpleHttpResult(e); } finally { if (urlConn != null) { urlConn.disconnect(); } } }
From source file:com.photon.phresco.framework.rest.api.QualityService.java
public static boolean checkIfURLExists(String targetUrl) { HttpURLConnection httpUrlConn = null; try {/* w w w . j av a 2 s. c o m*/ httpUrlConn = (HttpURLConnection) new java.net.URL(targetUrl).openConnection(); httpUrlConn.setRequestMethod(HEAD_REVISION); httpUrlConn.setConnectTimeout(30000); httpUrlConn.setReadTimeout(30000); return (httpUrlConn.getResponseCode() == HttpURLConnection.HTTP_OK); } catch (Exception e) { return false; } finally { httpUrlConn.disconnect(); } }
From source file:com.mobicage.rogerthat.Api.java
@SuppressWarnings("unchecked") private Object wireRequest(final JSONObject request, final int attempt) throws RogerthatAPIException { final URL url; try {/*from w w w . j a va2 s .com*/ url = new URL(apiLocation); } catch (MalformedURLException e) { // Will never come here throw new RogerthatAPIException(e); } HttpURLConnection connection; try { if (proxyHost != null && proxyPort != 0) { // Adapt proxy settings Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)); connection = (HttpURLConnection) url.openConnection(proxy); } else { connection = (HttpURLConnection) url.openConnection(); } } catch (IOException e) { throw new RogerthatAPIException(e); } try { connection.setDoOutput(true); try { connection.setRequestMethod("POST"); } catch (ProtocolException e) { // Will never come here throw new RogerthatAPIException(e); } connection.setRequestProperty("X-Nuntiuz-API-Key", apiKey); connection.setRequestProperty("Content-type", "application/json-rpc; charset=utf-8"); connection.setReadTimeout(30000); Writer writer; try { writer = new OutputStreamWriter(connection.getOutputStream(), "UTF-8"); try { String jsonString = request.toJSONString(); if (logging) log.info("Sending request to Rogerthat:\n" + jsonString); writer.write(jsonString); } finally { writer.close(); } switch (connection.getResponseCode()) { case HttpURLConnection.HTTP_OK: InputStream is = connection.getInputStream(); try { JSONObject result = (JSONObject) JSONValue .parse(new BufferedReader(new InputStreamReader(is, "UTF-8"))); if (logging) log.info("Result received from Rogerthat:\n" + result.toJSONString()); JSONObject error = (JSONObject) result.get("error"); if (error != null) throw new RogerthatAPIException(Integer.parseInt(error.get("code").toString()), (String) error.get("message"), error); else return result.get("result"); } finally { is.close(); } case HttpURLConnection.HTTP_UNAUTHORIZED: throw new RogerthatAPIException(1000, "Could not authenticate against Rogerth.at Messenger API! Check your api key.", null); case HttpURLConnection.HTTP_NOT_FOUND: throw new RogerthatAPIException(1000, "Rogerth.at Messenger API method not found!", null); default: if (attempt < 5) return wireRequest(request, attempt + 1); else throw new RogerthatAPIException(1000, "Could not send call to Rogerth.at Messenger!", null); } } catch (IOException e) { // Will never come here throw new RogerthatAPIException(e); } } finally { connection.disconnect(); } }