java实现HTTP请求(二)

HttpClient3.1 发送了http请求  (不推荐

引入依赖

<dependency>
    <groupId>commons-httpclient</groupId>
    <artifactId>commons-httpclient</artifactId>
    <version>3.1</version>
    <scope>test</scope>
</dependency>

 代码实现

package com.rhine.blog.http;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;

public class HttpClient3 {

    public static String sendByGet(String url) {
        InputStream is = null;
        BufferedReader br = null;
        String result = null;
        HttpClient httpClient = new HttpClient();// 创建httpClient实例
        // 先获取连接管理器对象,再获取参数对象,再进行参数的赋值
        httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(15000);// 设置http连接主机服务超时时间:15000毫秒
        GetMethod getMethod = new GetMethod(url);// 创建一个Get方法实例对象
        getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 60000);// 设置get请求超时为60000毫秒
        // 设置请求重试机制,默认重试次数:3次,参数设置为true,重试机制可用,false相反
        getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, true));
        try {
            int statusCode = httpClient.executeMethod(getMethod);// 执行Get方法
            // 判断返回码
            if (statusCode != HttpStatus.SC_OK) {// 如果状态码返回的不是ok,说明失败了,打印错误信息
                System.err.println("Method faild: " + getMethod.getStatusLine());
            } else {
                is = getMethod.getResponseBodyAsStream();// 通过getMethod实例,获取远程的一个输入流
                br = new BufferedReader(new InputStreamReader(is, "UTF-8"));// 包装输入流
                StringBuffer sbf = new StringBuffer();
                String temp = null;// 读取封装的输入流
                while ((temp = br.readLine()) != null) {
                    sbf.append(temp).append("\r\n");
                }
                result = sbf.toString();
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭资源
            if (null != br) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != is) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            // 释放连接
            getMethod.releaseConnection();
        }
        return result;
    }


    public static String sendByPost(String url, Map<String, Object> paramMap) {
        InputStream is = null;// 获取输入流
        BufferedReader br = null;
        String result = null;
        HttpClient httpClient = new HttpClient();// 创建httpClient实例对象
        httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(15000);// 设置httpClient连接主机服务器超时时间:15000毫秒
        PostMethod postMethod = new PostMethod(url);// 创建post请求方法实例对象
        postMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 60000);// 设置post请求超时时间
        NameValuePair[] nvp = null;
        // 判断参数map集合paramMap是否为空
        if (null != paramMap && paramMap.size() > 0) {// 不为空
            nvp = new NameValuePair[paramMap.size()];// 创建键值参数对象数组,大小为参数的个数
            Set<Entry<String, Object>> entrySet = paramMap.entrySet();// 循环遍历参数集合map
            Iterator<Entry<String, Object>> iterator = entrySet.iterator();// 获取迭代器
            int index = 0;
            while (iterator.hasNext()) {
                Entry<String, Object> mapEntry = iterator.next();
                // 从mapEntry中获取key和value创建键值对象存放到数组中
                try {
                    nvp[index] = new NameValuePair(mapEntry.getKey(),new String(mapEntry.getValue().toString().getBytes("UTF-8"), "UTF-8"));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                index++;
            }
        }
        if (null != nvp && nvp.length > 0) {// 判断nvp数组是否为空
            postMethod.setRequestBody(nvp);// 将参数存放到requestBody对象中
        }
        try {
            int statusCode = httpClient.executeMethod(postMethod);
            if (statusCode != HttpStatus.SC_OK) {// 判断是否成功
                System.err.println("Method faild: " + postMethod.getStatusLine());
            }
            is = postMethod.getResponseBodyAsStream();// 获取远程返回的数据
            br = new BufferedReader(new InputStreamReader(is, "UTF-8"));// 封装输入流
            StringBuffer sbf = new StringBuffer();
            String temp = null;
            while ((temp = br.readLine()) != null) {
                sbf.append(temp).append("\r\n");
            }
            result = sbf.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != br) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != is) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            postMethod.releaseConnection();// 释放连接
        }
        return result;
    }
}
posted @ 2020-03-20 09:49  An-Optimistic-Person  阅读(490)  评论(0)    收藏  举报