Created
October 6, 2017 10:29
-
-
Save ag2s20150909/4df30f9f00bf0b4545ee364c6364dae2 to your computer and use it in GitHub Desktop.
a simple httptool
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.BufferedInputStream; | |
import java.io.ByteArrayOutputStream; | |
import java.io.InputStream; | |
import java.net.HttpURLConnection; | |
import java.net.URL; | |
public class HttpTool { | |
public static InputStream getInputStream(String url) { | |
try { | |
URL murl = new URL(url); | |
HttpURLConnection urlConnection = (HttpURLConnection) murl.openConnection(); | |
InputStream in = new BufferedInputStream(urlConnection.getInputStream()); | |
return in; | |
} catch (Exception e) { | |
return null; | |
} | |
} | |
public static String getString(String url) { | |
try { | |
byte[] data = getBytes(url); | |
return new String(data); | |
} catch (Exception e) { | |
return e.getMessage(); | |
} | |
} | |
public static byte[] getBytes(String url) { | |
try { | |
InputStream in = getInputStream(url); | |
byte[] data = mRead(in); | |
return data; | |
} catch (Exception e) { | |
return null; | |
} | |
} | |
public static byte[] mRead(InputStream inStream) throws Exception { | |
ByteArrayOutputStream outStream = new ByteArrayOutputStream(); | |
byte[] buffer = new byte[1024]; | |
int len = 0; | |
while ((len = inStream.read(buffer)) != -1) { | |
outStream.write(buffer, 0, len); | |
} | |
inStream.close(); | |
return outStream.toByteArray(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment