java实现httpclient2
2024-08-26 19:07:29
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader; import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient; public class TTT
{ /**
* @param args
* @throws IOException
* @throws ClientProtocolException
*/
public static void main(String[] args) throws ClientProtocolException, IOException
{
// 创建HttpClient实例
HttpClient httpclient = new DefaultHttpClient();
// 创建Get方法实例
HttpGet httpgets = new HttpGet("http://127.0.0.1/testhttp.php?username=yonghuming");
HttpResponse response = httpclient.execute(httpgets);
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instreams = entity.getContent();
String str = convertStreamToString(instreams);
System.out.println("Do something");
System.out.println(str);
// Do not need the rest
httpgets.abort();
}
} public static String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder(); String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
} }
最新文章
- IOS 页面之间的跳转
- c语言编程之双向循环链表
- Careercup - Google面试题 - 6407924087783424
- 原 Debian设置开机自动启动与关闭
- 实时错误 '91' :对象变量或with块变量未设置
- 在Fedora 23 Server和Workstation上安装LAMP(Linux, Apache, MariaDB和PHP)
- 四轴飞行器1.3 MPU6050(大端)和M4的FPU开启方法
- iOS 推送全解析,你不可不知的所有 Tips!
- js 四舍五入函数 toFixed(),里面的参数 就是保留小数的位数。
- 详解Java中的clone方法
- permission denied for window type 2003
- offsetLeft 解析
- 【原创】大叔经验分享(8)创建hive表时用内部表还是外部表
- dpkg: 处理软件包 qjackctl (--configure)时出错解决方法
- Linux-程序包管理
- 解题:SDOI 2017 硬币游戏
- Linux内存管理学习2 —— head.S中的段页表的建立
- 2018房地产沉思录 z
- scala ";←"; ";<;-";
- 高阶篇:4.2.3)DFMEA现有设计:预防控制与探测控制