/** * @author UFreedom * @since 2014 2014-10-4 下午2:18:59 * @see www.52yummy.com * <br> * sunfreedom@sina.cn * <br> * Copyright (C) 2014 UFreedom. All Rights Reserved. */ package com.codecomb.utils; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import javax.net.ssl.HttpsURLConnection; public class NetUtil { public static byte[] getUrlBytes(String urlSpec) throws IOException { URL url = new URL(urlSpec); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); try { ByteArrayOutputStream out = new ByteArrayOutputStream(); InputStream in = connection.getInputStream(); if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) { return null; } int bytesRead = 0; byte[] buffer = new byte[1024]; while ((bytesRead = in.read(buffer)) > 0) { out.write(buffer, 0, bytesRead); } out.close(); return out.toByteArray(); } finally { connection.disconnect(); } } public static String getUrl(String urlSpec) throws IOException { return new String(getUrlBytes(urlSpec)); } }