HttpClient GET请求

曾经终败给现在 2022-05-30 10:46 305阅读 0赞

步骤

  1. 创建HttpClient对象
  2. 创建请求方法的实例
  3. 调用HttpClient对象的execute发送请求,该方法返回一个HttpResponse
  4. 通过Response获取此响应的消息实体
  5. 返回字符串

    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 {

    1. @Test
    2. public void test2(){
    3. try {
    4. String url = "https://www.bxwx9.org/b/58/58522/index.html";
    5. HttpClient client = HttpClients.createDefault();
    6. HttpGet get = new HttpGet(url);
    7. HttpResponse response = client.execute(get);
    8. HttpEntity entity = response.getEntity();
    9. System.out.println(entity);
    10. System.out.println(EntityUtils.toString(entity,"utf-8"));
    11. } catch (IOException e) {
    12. e.printStackTrace();
    13. }
    14. }

    }

详细用法

http://blog.csdn.net/zhuwukai/article/details/78644484

通过Jsoup的方法也可以发起get请求, 得到网页源码很简单

  1. Jsoup.connect("http://example.com/").get();

发表评论

表情:
评论列表 (有 0 条评论,305人围观)

还没有评论,来说两句吧...

相关阅读