httpclient工具使用(org.apache.httpcomponents.httpclient)

httpclient工具使用(org.apache.httpcomponents.httpclient)

引入依赖

<dependency>
  		<groupId>org.apache.httpcomponents</groupId>
  		<artifactId>httpclient</artifactId>
  		<version>4.3.5</version>
  	</dependency>

  

 

 

get请求

public static void main(String[] args) throws Exception {
		
		//创建httpclient对象
		CloseableHttpClient httpClient = HttpClients.createDefault();
		//创建Http get请求
		HttpGet httpGet = new HttpGet("http://www.xxx.com/rest/content?categoryId=4&page=1&rows=20");
		//接收返回值
		CloseableHttpResponse response = null;
		
		try {
			//请求执行
			response = httpClient.execute(httpGet);
			if(response.getStatusLine().getStatusCode()==200){
				String content = EntityUtils.toString(response.getEntity(), "utf-8");
				System.out.println("--------->" + content);
			}
		}finally{
			if(response!=null){
				response.close();
			}
			httpClient.close();
		}

  

get带参数请求

public static void main(String[] args) throws Exception{
		
		//创建httpClient对象
		CloseableHttpClient httpClient = HttpClients.createDefault();
		//定义http get参数
		URI uri = new URIBuilder("http://www.xxxx.com/rest/content").setParameter("categoryId", "4")
				.setParameter("page", "1").setParameter("rows", "30").build();
		System.out.println(uri);
		//创建http get请求
		HttpGet httpGet = new HttpGet(uri);
		
		//接受请求返回的定义
		CloseableHttpResponse response = null;
		try {
			//执行get请求
			response = httpClient.execute(httpGet);
			if(response.getStatusLine().getStatusCode()==200){
				String content = EntityUtils.toString(response.getEntity(), "utf-8");
				System.out.println(content);
			}
		}finally{
			if(response!=null){
				response.close();
			}
			httpClient.close();
		}
	}

  

 

http post请求

 public static void main(String[] args) throws Exception {

        // 创建Httpclient对象
        CloseableHttpClient httpclient = HttpClients.createDefault();

        // 创建http POST请求
        HttpPost httpPost = new HttpPost("http://www.oschina.net/");
        // 伪装成浏览器
        httpPost.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36");

        CloseableHttpResponse response = null;
        try {
            // 执行请求
            response = httpclient.execute(httpPost);
            // 判断返回状态是否为200
            if (response.getStatusLine().getStatusCode() == 200) {
                String content = EntityUtils.toString(response.getEntity(), "UTF-8");
                System.out.println(content);
            }
        } finally {
            if (response != null) {
                response.close();
            }
            httpclient.close();
        }

    }

  

 

 

http post带参数请求

public static void main(String[] args) throws Exception{
		//创建httpclient
		CloseableHttpClient httpClient = HttpClients.createDefault();
		//创建http post
		HttpPost httpPost = new HttpPost("http://www.oschina.net/search");
		//模拟浏览器设置头
		httpPost.setHeader(
				"User-Agent",
				"Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36");
	
		//设置请求数据
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		params.add(new BasicNameValuePair("scope", "project"));
		params.add(new BasicNameValuePair("q", "java"));
		params.add(new BasicNameValuePair("fromerr", "7nXH76r7"));
		//构建表单
		UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(params);
		//将表达请求放入到httpost
		httpPost.setEntity(formEntity);
		
		//返回类型
		CloseableHttpResponse response = null;
		
		try {
			response = httpClient.execute(httpPost);
			String content = EntityUtils.toString(response.getEntity(), "utf-8");
			System.out.println(content);
		}finally{
			if(response!=null){
				response.close();
			}
			httpClient.close();
		}
		
	
	}

  

 

posted @ 2019-06-24 00:11  穆晟铭  阅读(10909)  评论(0编辑  收藏  举报