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();
    		}
    		
    	}
    }
    

      

  • 相关阅读:
    JD20 紧急疏散
    排序-归并
    排序:快排
    剑指offer-二叉树镜像
    美的笔试--分糖果
    centos限定端口访问
    javascript中使用ajax异步验证数据库后方法return true时总是返回undefined
    javascript自定义startWith()和endWith()方法
    jsp <input type="file"> 清空value值的一种方式
    Could not initialize class sun.awt.X11GraphicsEnvironment
  • 原文地址:https://www.cnblogs.com/nidegui/p/10886528.html
Copyright © 2011-2022 走看看