zoukankan      html  css  js  c++  java
  • 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();
    		}
    		
    	
    	}
    

      

  • 相关阅读:
    绝对干货:供个人开发者赚钱免费使用的一些好的API接口
    科普技术贴:个人开发者的那些赚钱方式
    北漂程序员的笑与泪
    非著名程序员公众号
    北漂程序员的笑与泪
    【有人@我】Android中高亮变色显示文本中的关键字
    新时代的coder如何成为专业程序员
    自定义圆形控件RoundImageView并认识一下attr.xml
    偷天换日:网络劫持,网页js被伪装替换。
    jeesite 去掉 /a
  • 原文地址:https://www.cnblogs.com/achengmu/p/11075007.html
Copyright © 2011-2022 走看看