--- dao/DaoAdresseVedligehold/src/main/java/dk/daoas/adressevedligehold/util/HttpUtil.java 2016/01/30 14:17:27 2881 +++ dao/DaoAdresseVedligehold/src/main/java/dk/daoas/adressevedligehold/util/HttpUtil.java 2016/01/30 14:31:48 2882 @@ -1,156 +1,155 @@ -package dk.daoas.adressevedligehold.util; - -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.io.UnsupportedEncodingException; -import java.net.HttpURLConnection; -import java.net.URL; -import java.net.URLConnection; -import java.net.URLDecoder; -import java.net.URLEncoder; -import java.util.HashMap; -import java.util.Map; - - -public class HttpUtil { - - static public Map decodeUri(String uri) { - if (uri.indexOf('?') != -1) { - String parts[] = uri.split("\\?"); - return decodeParams( parts[1] ); - } else { - return new HashMap(); - } - - - - } - - static public Map decodeParams(String str) { - Map res = new HashMap(); - - - String parts[] = str.split("&"); - - for (String part : parts) { - String pair[] = part.split("="); - String key = pair[0]; - - String val = null; - if (pair.length == 2) { - - - try { - val = URLDecoder.decode(pair[1], "ISO-8859-1"); - } catch (UnsupportedEncodingException e) { - val = pair[1]; // if decode fails try with the raw string - } - - } - - res.put(key, val); - } - - return res; - } - - static public String getLastPart(String url) { - String parts[] = url.split("/"); - String lastPart = parts[ parts.length - 1]; - - return lastPart; - } - - static public String encodeParams(Map params) { - StringBuilder sb = new StringBuilder(); - - boolean isFirst = true; - for (String key : params.keySet()) { - if (isFirst) { - isFirst = false; - } else { - sb.append("&"); - } - - String val = params.get(key); - sb.append(key); - sb.append("="); - sb.append( encode(val) ); - - - } - - - return sb.toString(); - - } - - static String encode(String url) { - if (url == null) - return ""; - - try { - return URLEncoder.encode(url, "UTF-8"); - } catch (Exception e) { - return url; - } - } - - public static String getContentString(String uri, int timeout) throws IOException { - return getContentString(uri, timeout, "UTF-8"); - } - - public static String getContentString(String uri, int timeout, String encoding) throws IOException { - byte[] buf = getContent(uri, timeout); - return new String(buf, encoding); - } - - public static byte[] getContent(String uri, int timeout) throws IOException { - URL url = new URL(uri); - URLConnection connection = url.openConnection(); - - connection.setConnectTimeout(timeout); - connection.setReadTimeout(timeout * 10); - InputStream is = connection.getInputStream(); - - return readInputStream(is); - } - - public static byte[] postContent(String uri, String data, int timeout) throws IOException { - URL url = new URL(uri); - HttpURLConnection connection = (HttpURLConnection) url.openConnection(); - connection.setDoOutput(true); - connection.setRequestMethod("POST"); - connection.setConnectTimeout(timeout); - connection.setReadTimeout(timeout * 10); - - OutputStream os = connection.getOutputStream(); - os.write( data.getBytes() ); - - InputStream is = connection.getInputStream(); - - return readInputStream(is); - - } - - static byte[] readInputStream(InputStream is) throws IOException{ - byte buffer[] = new byte[1024]; - ByteArrayOutputStream baos = new ByteArrayOutputStream(32768); //start buffer size - instead of default 32bytes - try { - int count; - while ( (count = is.read(buffer)) != -1) { - baos.write(buffer, 0, count); - } - } finally { - try { - is.close(); - baos.close(); - } catch (Exception e) {} //never mind if close throws an exception - } - - return baos.toByteArray(); - } - -} +package dk.daoas.adressevedligehold.util + +// kopieret fra dk.thoerup.genericjavautils; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.UnsupportedEncodingException; +import java.net.HttpURLConnection; +import java.net.URL; +import java.net.URLConnection; +import java.net.URLDecoder; +import java.net.URLEncoder; +import java.nio.charset.Charset; +import java.util.HashMap; +import java.util.Map; + + +public class HttpUtil { + + static public Map decodeUri(String uri) { + if (uri.indexOf('?') != -1) { + String parts[] = uri.split("\\?"); + return decodeParams( parts[1] ); + } else { + return new HashMap(); + } + + + + } + + static public Map decodeParams(String str) { + Map res = new HashMap(); + + + String parts[] = str.split("&"); + + for (String part : parts) { + String pair[] = part.split("="); + String key = pair[0]; + + String val = null; + if (pair.length == 2) { + + + try { + val = URLDecoder.decode(pair[1], "ISO-8859-1"); + } catch (UnsupportedEncodingException e) { + val = pair[1]; // if decode fails try with the raw string + } + + } + + res.put(key, val); + } + + return res; + } + + static public String getLastPart(String url) { + String parts[] = url.split("/"); + String lastPart = parts[ parts.length - 1]; + + return lastPart; + } + + static public String encodeParams(Map params) { + StringBuilder sb = new StringBuilder(); + + boolean isFirst = true; + for (Map.Entry entry : params.entrySet()) { + if (isFirst) { + isFirst = false; + } else { + sb.append("&"); + } + + + sb.append( entry.getKey() ); + sb.append("="); + sb.append( encode( entry.getValue() ) ); + + + } + + + return sb.toString(); + + } + + static String encode(String url) { + if (url == null) + return ""; + + try { + return URLEncoder.encode(url, "UTF-8"); + } catch (Exception e) { + return url; + } + } + + public static String getContentString(String uri, int timeout) throws IOException { + return getContentString(uri, timeout, "UTF-8"); + } + + public static String getContentString(String uri, int timeout, String encoding) throws IOException { + byte[] buf = getContent(uri, timeout); + return new String(buf, encoding); + } + + public static byte[] getContent(String uri, int timeout) throws IOException { + URL url = new URL(uri); + URLConnection connection = url.openConnection(); + + connection.setConnectTimeout(timeout); + connection.setReadTimeout(timeout); + InputStream is = connection.getInputStream(); + + return readInputStream(is); + } + + public static byte[] postContent(String uri, String data, int timeout) throws IOException { + URL url = new URL(uri); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setDoOutput(true); + connection.setRequestMethod("POST"); + connection.setConnectTimeout(timeout); + + OutputStream os = connection.getOutputStream(); + os.write( data.getBytes(Charset.forName("UTF-8")) ); + + InputStream is = connection.getInputStream(); + + return readInputStream(is); + + } + + static byte[] readInputStream(InputStream is) throws IOException{ + byte buffer[] = new byte[1024]; + //try /; //start buffer size - instead of default 32bytes + try (ByteArrayOutputStream baos = new ByteArrayOutputStream(32768)) { + int count; + + while ( (count = is.read(buffer)) != -1) { + baos.write(buffer, 0, count); + } + is.close(); + return baos.toByteArray(); + } + } + +} +