zoukankan      html  css  js  c++  java
  • httpclient

    添加架包支持:

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

      

    第一步是获取http客户端的实列,编写代码

    package com.java.httpclient;
    
    import java.io.IOException;
    
    import org.apache.http.HttpEntity;
    import org.apache.http.ParseException;
    import org.apache.http.client.ClientProtocolException;
    import org.apache.http.client.methods.CloseableHttpResponse;
    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;
    
    public class Helloword {
    	public static void main(String[] args) {
    		CloseableHttpClient  httpclient=HttpClients.createDefault(); //创建浏览器实列
    		HttpGet httpget=new HttpGet("http://www.java1234.com"); //创建get实列
    		CloseableHttpResponse response=null;
    		try {
    			//执行get请求
    			response=httpclient.execute(httpget);
    		} catch (ClientProtocolException e) { //http协议异常
    			e.printStackTrace();
    		} catch (IOException e) { //io异常
    			e.printStackTrace();
    		}
    		
    		//返回实体
    	     HttpEntity entity=response.getEntity();
    	     try {
    			System.out.println("输出实体:"+EntityUtils.toString(entity,"utf-8"));
    		} catch (ParseException e) { //解析异常
    			e.printStackTrace();
    			
    		} catch (IOException e) {  //io 异常
    
    			e.printStackTrace();
    		}
    		try {
    			response.close();
    		} catch (IOException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    		
    	}
    }
    

      

  • 相关阅读:
    java_泛型
    java工具类Collections
    Map集合的遍历
    集合元素顺序的实现
    apk比较版本大小
    存储过程与SQL语句怎么选择
    线程协作-生产者/消费者问题
    线程中断、线程让步、线程睡眠、线程合并
    如何将字符串去重复demo工具
    JavaScript 中的函数介绍
  • 原文地址:https://www.cnblogs.com/nidegui/p/10886528.html
Copyright © 2011-2022 走看看