Get the text content (Java) route through htpp

readFileByUrl static String public (String urlStr) { 
String RES = null;
the try {
the URL = new new URL the URL (urlStr);
the HttpURLConnection Conn = (the HttpURLConnection) url.openConnection ();
// set over time is 3 seconds
conn.setConnectTimeout (3 * 1000);
// prevented from crawling shield 403 returns an error
conn.setRequestProperty ( "the User-- Agent", "the Mozilla / 4.0 (compatible; MSIE 5.0; the Windows NT; DigExt)");
// get the input stream
inputStream inputStream conn.getInputStream = ();
RES = readInputStream (inputStream);
} the catch (Exception E) {
System.out.println ( "Get text address failed by url");
}
return RES;
}


/ **
* from the input stream Gets a string in
*
* @param inputStream
* @return
* @throws IOException
*/
public static String readInputStream(InputStream inputStream) throws IOException {
byte[] buffer = new byte[1024];
int len = 0;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
while ((len = inputStream.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
bos.close();
return new String(bos.toByteArray(), "utf-8");
}

Guess you like

Origin www.cnblogs.com/NowShowTimeChenKang/p/11358665.html