java.net.HttpURLConnection httpURLConnection = (java.net.HttpURLConnection) url.openConnection();
httpURLConnection.setDoInput(true);
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
httpURLConnection.connect();
InputStream inputStream = httpURLConnection.getInputStream();
byte[] byteArray = new byte[1024];
int len = 0;
int ascii = inputStream.read();
byteArray[len] = (byte) ascii;
while (ascii > -1) {
ascii = inputStream.read();
len ++;
byteArray[len] = (byte) ascii;
}
result = new String(byteArray, 0, len);
java从输入流读取ASCII
最新推荐文章于 2024-09-12 21:04:39 发布
这段代码演示了如何使用Java的HttpURLConnection类执行一个GET请求,并读取响应内容。它设置了请求方法,内容类型,并连接到指定URL。然后从输入流中读取响应数据并转换为字符串。
589

被折叠的 条评论
为什么被折叠?



