httpclient如何发送get请求

2018-06-18 02:09:38来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

/**
* Created by Administrator on 2018/4/11.
*/
public class HttpClientTest {


public static void main(String[] args) {
try {

String charset = "UTF-8";
//创建CloseableHttpClient
CloseableHttpClient httpClient = HttpClients.custom().build();
String url = "http://www.baidu.com";

//如果有带参数,直接在url后面加?号拼接 例如 http://www.baidu.com?a=n&b=n

//请求方式
HttpGet get = new HttpGet(url);
//接收请求返回对象
HttpEntity entity =httpClient.execute(get).getEntity();
//使用EntityUtils 把httpEntity请求返回流读取成字符串
String result = EntityUtils.toString(entity,charset);
System.out.println(result);

EntityUtils.consume(entity);
httpClient.close();

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

}

}

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Java练习(模拟扫雷游戏)

下一篇:LinkedHashMap就这么简单【源码剖析】