使用HttpClient进行Get方式通信

下载apache包

http://hc.apache.org/downloads.cgi

比较eclipse自带api,简单,易上手

实例:

package zw1;

import java.io.IOException;

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.HttpClients;
import org.apache.http.util.EntityUtils;

public class texte1 {

public static void main(String[] args) {
new Get().start();//运行类
}
static class Get extends Thread{

HttpClient client = HttpClients.createDefault();

public void run() {
HttpGet get = new HttpGet("http://www.baidu.com");
try {

HttpResponse response = client.execute(get);
HttpEntity entity = response.getEntity();
String string = EntityUtils.toString(entity, "utf-8");

System.out.println(string);

} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

上一篇:[开发笔记]-火狐的event和jquery1.9.1.min的问题


下一篇:Spring源码追踪1——doGetBean(为什么org.springframework.data.redis.core.RedisTemplate的实例可以注入为ListOperations)