HttpClient GET请求
步骤
- 创建HttpClient对象
- 创建请求方法的实例
- 调用HttpClient对象的execute发送请求,该方法返回一个HttpResponse
- 通过Response获取此响应的消息实体
返回字符串
import com.cyh.impl.DeraultChapterSpider;
import com.cyh.interfaces.IChapterSpider;
import com.cyh.pojo.Chapter;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
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;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.junit.Test;import java.io.IOException;
import java.util.List;public class NovelTest {
@Test
public void test2(){
try {
String url = "https://www.bxwx9.org/b/58/58522/index.html";
HttpClient client = HttpClients.createDefault();
HttpGet get = new HttpGet(url);
HttpResponse response = client.execute(get);
HttpEntity entity = response.getEntity();
System.out.println(entity);
System.out.println(EntityUtils.toString(entity,"utf-8"));
} catch (IOException e) {
e.printStackTrace();
}
}
}
详细用法
http://blog.csdn.net/zhuwukai/article/details/78644484
通过Jsoup的方法也可以发起get请求, 得到网页源码很简单
Jsoup.connect("http://example.com/").get();
还没有评论,来说两句吧...