diff options
author | rudolf <rudolf@d688527b-c9ab-4aba-bd8d-4036d912da1d> | 2005-01-17 11:06:50 +0000 |
---|---|---|
committer | rudolf <rudolf@d688527b-c9ab-4aba-bd8d-4036d912da1d> | 2005-01-17 11:06:50 +0000 |
commit | 1416bc8ea8df2ccf386c3b2a489c48c7d5761574 (patch) | |
tree | 2cba92ae41a053f9f0e7a2275ec0dbd8cd6c9b62 | |
parent | 6beeda59da023e77047915910caf5aab0a9502b2 (diff) | |
download | moa-id-spss-1416bc8ea8df2ccf386c3b2a489c48c7d5761574.tar.gz moa-id-spss-1416bc8ea8df2ccf386c3b2a489c48c7d5761574.tar.bz2 moa-id-spss-1416bc8ea8df2ccf386c3b2a489c48c7d5761574.zip |
Initial check in of Class HTTPUtils
git-svn-id: https://joinup.ec.europa.eu/svn/moa-idspss/trunk@213 d688527b-c9ab-4aba-bd8d-4036d912da1d
-rw-r--r-- | id.server/src/at/gv/egovernment/moa/id/util/HTTPUtils.java | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/id.server/src/at/gv/egovernment/moa/id/util/HTTPUtils.java b/id.server/src/at/gv/egovernment/moa/id/util/HTTPUtils.java new file mode 100644 index 000000000..270b0c968 --- /dev/null +++ b/id.server/src/at/gv/egovernment/moa/id/util/HTTPUtils.java @@ -0,0 +1,63 @@ +package at.gv.egovernment.moa.id.util; + +/** + * HTTP Utilities + * + * @author Rudolf Schamberger + * @version $Id$ + */ + +import java.io.BufferedInputStream; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.Reader; +import java.net.HttpURLConnection; +import java.net.URL; + +import org.apache.regexp.RE; + +/** + * + * @author Rudolf Schamberger + * + */ +public class HTTPUtils { + + /** + * Utility used to obtainin correct encoded HTTP content. + * Reads a given Content adressed by HTTP-URL into String. + * Content encoding is considered by using the Content-Type HTTP header charset value. + * @param URL HTTP URL to read from. + * @return String representation of content + * @throws IOException on data-reading problems + */ + public static String readHttpURL(String URL) + throws IOException { + + URL url = new URL(URL); + HttpURLConnection conn = (HttpURLConnection)url.openConnection(); + conn.setRequestMethod("GET"); + String contentType = conn.getContentType(); + RE regExp = new RE("(;.*charset=)(\"*)(.*[^\"])"); + boolean charsetSupplied = regExp.match(contentType); + String encoding = "ISO-8859-1"; //default HTTP encoding + if (charsetSupplied) { + encoding = regExp.getParen(3); + } + //TODO RS TEST if this works on all platforms + InputStream instream = new BufferedInputStream(conn.getInputStream()); + InputStreamReader isr = new InputStreamReader(instream, encoding); + Reader in = new BufferedReader(isr); + int ch; + StringBuffer buffer = new StringBuffer(); + while ((ch = in.read()) > -1) { + buffer.append((char)ch); + } + in.close(); + conn.disconnect(); + return buffer.toString(); + } + +} |