HTTP请求代理

工具类:

package com.coreCompanySupplier.receive.connector;

import org.apache.commons.lang.StringUtils;

import com.coreCompanySupplier.receive.connector.dto.GeneralHttpSend;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.Proxy;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

/**
 * http发送
 * <p/>
 */
public class HttpSendUtil {







    /**
     * http发送
     *
     * @param general 请求参数对象
     * @return 响应报文
     * @throws IOException 
     * @throws Exception 异常
     */
     public static String send(GeneralHttpSend general) throws HttpProxyException, IOException {

            HttpURLConnection conn = null;
            InputStream inputStream = null;
            OutputStream dataOutputStream = null;
            ByteArrayOutputStream bos = null;
            try {
                URL url = new URL(general.getUrl());
                String proxyHost = general.getProxyHost();
                if (StringUtils.isNotEmpty(proxyHost)) {
                    InetSocketAddress sa = new InetSocketAddress(proxyHost, Integer.valueOf(general.getProxyPort()));
                    Proxy proxy = new Proxy(Proxy.Type.HTTP, sa);
                    conn = (HttpURLConnection) url.openConnection(proxy);
                } else {
                    conn = (HttpURLConnection) url.openConnection();
                }
                String reqCharSet = general.getReqCharSet();
                //初始化http链接
                initConn(conn, general);
                dataOutputStream = new DataOutputStream(conn.getOutputStream());
                dataOutputStream.write(general.getRequestData().getBytes(reqCharSet));
                dataOutputStream.flush();
                if (HttpURLConnection.HTTP_OK != conn.getResponseCode()) {
                    throw new HttpProxyException("Request fail, error message : " + conn.getResponseMessage() + ", http status : " + conn.getResponseCode());
                } else {
                    bos = new ByteArrayOutputStream();
                    inputStream = conn.getInputStream();
                    Transfer.copy(inputStream, bos, -1);
                    bos.flush();
                }
            }finally {
                try {
                    Transfer.disconnect(inputStream, dataOutputStream, conn);
                } finally {
                    if (bos != null) bos.close();
                }
            }
            return new String(bos.toByteArray(), general.getRespCharSet());
        }

    /**
     * 初始化http连接对象
     *
     * @param conn    http连接对象
     * @param general 请求参数对象
     * @throws ProtocolException 
     * @throws Exception 异常
     */
    private static void initConn(HttpURLConnection conn, GeneralHttpSend general) throws ProtocolException {
        conn.setRequestMethod(general.getSubmitType());
        conn.setUseCaches(general.isUseCaches());
        conn.setDoInput(general.isDoInput());
        conn.setDoOutput(general.isDoOutput());
        Map<String, String> propertyMap = general.getRequestPropertyMap();
        for (Map.Entry<String, String> entry : propertyMap.entrySet()) {
            conn.setRequestProperty(entry.getKey(), entry.getValue());
        }
        //连接超时时间
        conn.setConnectTimeout(Integer.parseInt(general.getConnectTimeOut()));
        //读超时时间
        conn.setReadTimeout(Integer.parseInt(general.getReadTimeOut()));
    }
}

GeneralHttpSend :

/**
 * 通用http发送器dto
 * <p/>
 * Created by 13071600 on 2017/5/26.
 */
public class GeneralHttpSend extends GeneralHttp {
    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = 1L;
}

GeneralHttp:

public abstract class GeneralHttp extends GeneralSend {
    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = 1L;

    /**
     * 请求url
     */
    private String url;

    /**
     * http提交方式,暂时只支持post提交
     */
    private String submitType = "POST";

    /**
     * 请求是否使用缓存,默认不使用,如有需要,自行设值
     */
    private boolean useCaches = false;

    /**
     * 设置是否从httpUrlConnection读入,默认true,如有需要,自行设值
     */
    private boolean doInput = true;

    /**
     * 设置是否向httpUrlConnection输出,默认true,如有需要,自行设值
     */
    private boolean doOutput = true;

    /**
     * 设定传输属性
     */
    private Map<String, String> requestPropertyMap;

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getSubmitType() {
        return submitType;
    }

    public void setSubmitType(String submitType) {
        this.submitType = submitType;
    }

    public boolean isUseCaches() {
        return useCaches;
    }

    public void setUseCaches(boolean useCaches) {
        this.useCaches = useCaches;
    }

    public boolean isDoInput() {
        return doInput;
    }

    public void setDoInput(boolean doInput) {
        this.doInput = doInput;
    }

    public boolean isDoOutput() {
        return doOutput;
    }

    public void setDoOutput(boolean doOutput) {
        this.doOutput = doOutput;
    }

    public Map<String, String> getRequestPropertyMap() {
        return requestPropertyMap;
    }

    public void setRequestPropertyMap(Map<String, String> requestPropertyMap) {
        this.requestPropertyMap = requestPropertyMap;
    }

    @Override
    public String toString() {
        return "GeneralHttp{" +
                "url='" + url + '\'' +
                ", submitType='" + submitType + '\'' +
                ", useCaches=" + useCaches +
                ", doInput=" + doInput +
                ", doOutput=" + doOutput +
                ", requestPropertyMap=" + requestPropertyMap +
                '}';
    }
}

GeneralSend:

public abstract class GeneralSend implements Serializable {
    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = 1L;

    /**
     * 请求报文
     */
    private String requestData;

    /**
     * 连接超时,默认2s,请自行设置时间
     */
//    private String connectTimeOut = "2000";
    private String connectTimeOut = "5000";

    /**
     * 读超时,默认5s,请自行设置时间
     */
//    private String readTimeOut = "5000";
    private String readTimeOut = "10000";

    /**
     * 请求报文处理编码,默认UTF-8,如有特殊,自行设置
     */
    private String reqCharSet = "UTF-8";

    /**
     * 响应报文处理编码,默认UTF-8,如有特殊,自行设置
     */
    private String respCharSet = "UTF-8";

    /**
     * 代理地址
     */
    private String proxyHost;

    /**
     * 代理端口
     */
    private String proxyPort;

    public String getRequestData() {
        return requestData;
    }

    public void setRequestData(String requestData) {
        this.requestData = requestData;
    }

    public String getConnectTimeOut() {
        return connectTimeOut;
    }

    public void setConnectTimeOut(String connectTimeOut) {
        this.connectTimeOut = connectTimeOut;
    }

    public String getReadTimeOut() {
        return readTimeOut;
    }

    public void setReadTimeOut(String readTimeOut) {
        this.readTimeOut = readTimeOut;
    }

    public String getReqCharSet() {
        return reqCharSet;
    }

    public void setReqCharSet(String reqCharSet) {
        this.reqCharSet = reqCharSet;
    }

    public String getRespCharSet() {
        return respCharSet;
    }

    public void setRespCharSet(String respCharSet) {
        this.respCharSet = respCharSet;
    }

    public String getProxyHost() {
        return proxyHost;
    }

    public void setProxyHost(String proxyHost) {
        this.proxyHost = proxyHost;
    }

    public String getProxyPort() {
        return proxyPort;
    }

    public void setProxyPort(String proxyPort) {
        this.proxyPort = proxyPort;
    }

    @Override
    public String toString() {
        return "GeneralSend{" +
                "requestData='" + requestData + '\'' +
                ", connectTimeOut='" + connectTimeOut + '\'' +
                ", readTimeOut='" + readTimeOut + '\'' +
                ", reqCharSet='" + reqCharSet + '\'' +
                ", respCharSet='" + respCharSet + '\'' +
                ", proxyHost='" + proxyHost + '\'' +
                ", proxyPort='" + proxyPort + '\'' +
                '}';
    }
}

Transfer:

package com.coreCompanySupplier.receive.connector;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.Socket;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

/**
 * 数据传输工具类
 *
 * @author Guozheng
 * @version 1.0.0 12-08-28 11:34
 */
public final class Transfer {
    /**
     * 缓冲流缓存大小
     */
    public static final int DEFAULT_BUFFER_SIZE = 1024 * 64;

    /**
     * 每次读写操作的字节数
     */
    public static final int DEFAULT_BLOCK_SIZE = 1024 * 8;

    /**
     * 从输入流读出数据并写入输出流。
     *
     * @param is     输入流
     * @param os     输出流
     * @param length 复制的字节长度
     * @throws java.io.IOException 传输中可能出现的异常
     */
    public static void copy(InputStream is, OutputStream os, long length) throws IOException {
        if (length == 0) return;
        // 指定长度是否有效
        final boolean LENGTH_VALID = length > 0;
        // 定义缓存
        byte[] bytes = new byte[LENGTH_VALID && length < DEFAULT_BLOCK_SIZE ? (int) length : DEFAULT_BLOCK_SIZE];
        // 已读取字节总数
        long sum = 0;
        // 当次读取字节数
        int cnt;
        // 复制
        while ((cnt = read(is, bytes)) > 0) {
            os.write(bytes, 0, cnt);
            // 累计
            sum += cnt;
            // 指定有效长度
            if (LENGTH_VALID) {
                long unread = length - sum;     // 未读字节数
                if (unread == 0) {   // 刚好读完
                    break;
                } else if (unread < DEFAULT_BLOCK_SIZE) {
                    bytes = new byte[(int) unread];
                }
            }
        }
        // 校验已读数据长度和指定长度是否一致
        if (LENGTH_VALID)
            if (sum != length) throw new IOException("incorrect length: expected " + length + ", actual " + sum);
    }

    /**
     * 从输入流中读出数据并全部写到输出流中
     *
     * @param is 输入流
     * @param os 输出流
     * @throws IOException 传输中出现的IO异常
     */
    public static void copy(InputStream is, OutputStream os) throws IOException {
        copy(is, os, -1);
    }

    /**
     * 从输入流中读取文件
     *
     * @param is     输入流
     * @param target 目标文件
     * @param length 文件长度
     * @throws java.io.IOException 复制过程中出现的异常
     */
    public static void copy(InputStream is, File target, long length) throws IOException {
        // 创建目标文件所在目录
        File parent = target.getParentFile();
        if (!parent.mkdirs() && !parent.exists())
            throw new IOException("can not make directory: " + parent.getAbsolutePath());
        // 复制
        OutputStream os = null;
        try {
            os = new BufferedOutputStream(new FileOutputStream(target), DEFAULT_BUFFER_SIZE);
            copy(is, os, length);
            os.flush();
        } finally {
            if (os != null) os.close();
        }
    }

    /**
     * 读取文件到输出流
     *
     * @param source 源文件
     * @param os     输出流
     * @throws java.io.IOException 复制过程中出现的异常
     */
    public static void copy(File source, OutputStream os) throws IOException {
        InputStream is = null;
        try {
            is = new BufferedInputStream(new FileInputStream(source), DEFAULT_BUFFER_SIZE);
            copy(is, os, source.length());
        } finally {
            if (is != null) is.close();
        }
    }

    /**
     * 复制文件
     *
     * @param source 源文件
     * @param target 目标文件
     * @throws java.io.IOException 复制过程中出现的异常
     */
    public static void copy(File source, File target) throws IOException {
        InputStream is = null;
        try {
            is = new BufferedInputStream(new FileInputStream(source), DEFAULT_BUFFER_SIZE);
            copy(is, target, source.length());
        } finally {
            if (is != null) is.close();
        }
    }

    /**
     * 复制文件
     *
     * @param bytes  源数据
     * @param target 目标文件
     * @throws java.io.IOException 复制过程中出现的异常
     */
    public static void copy(byte[] bytes, File target) throws IOException {
        InputStream is = null;
        try {
            is = new ByteArrayInputStream(bytes);
            copy(is, target, bytes.length);
        } finally {
            if (is != null) is.close();
        }
    }

    /**
     * 从输入流中读取指定长度的字节数。
     *
     * @param is    输入流
     * @param bytes 读取字节容器
     * @return 已读取字节数,可能小于bytes.length
     * @throws java.io.IOException 读取中出现的异常
     */
    public static int read(InputStream is, byte[] bytes) throws IOException {
        // 从流中已经读出的字节数
        int sum = 0;
        // 在数据未满前一直读取,避免原始方式由于数据量大导致漏读数据
        while (sum < bytes.length) {
            // 本次读取的数据
            int cnt = is.read(bytes, sum, bytes.length - sum);
            // 如果流已经读到尽头,即使数组未读满也终止
            if (cnt == -1) break;
            // 已读数据计数累计
            sum += cnt;
        }
        return sum;
    }

    /**
     * 将字节数组按指定的长度左对齐、不足补\u0000写入输出流
     *
     * @param bytes  待输出字节
     * @param os     输出流
     * @param length 指定本次输出的字节长度
     * @throws java.io.IOException 输出中出现的异常
     */
    public static void write(byte[] bytes, OutputStream os, int length) throws IOException {
        // 定长数组,存放数据
        byte[] box = new byte[length];
        System.arraycopy(bytes, 0, box, 0, bytes.length);
        os.write(box);
    }

    /**
     * 压缩
     *
     * @param bytes 待压缩字节数组
     * @return 压缩后的字节数组
     * @throws IOException 异常
     */
    public static byte[] compress(byte[] bytes) throws IOException {
        // 特殊处理null情况
        if (bytes == null) return null;
        // 压缩
        GZIPOutputStream gos = null;
        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            gos = new GZIPOutputStream(bos);
            gos.write(bytes);
            gos.finish();
            gos.flush();
            gos.close();
            return bos.toByteArray();
        } finally {
            if (gos != null) gos.close();
        }
    }

    /**
     * 解压缩
     *
     * @param bytes 待解压缩字节数组
     * @return 解压缩后的字节数组
     * @throws IOException 异常
     */
    public static byte[] decompress(byte[] bytes) throws IOException {
        // 特殊处理null情况
        if (bytes == null) return null;
        // 解压
        GZIPInputStream gis = null;
        ByteArrayOutputStream bos = null;
        try {
            ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
            gis = new GZIPInputStream(bis);
            bos = new ByteArrayOutputStream();
            copy(gis, bos);
            bos.flush();
            bos.close();
            return bos.toByteArray();
        } finally {
            close(gis, bos);
        }
    }

    /**
     * 关闭输入输出流
     *
     * @param is 输入流
     * @param os 输出流
     * @throws IOException 异常
     */
    public static void close(InputStream is, OutputStream os) throws IOException {
        try {
            if (is != null) is.close();
        } finally {
            if (os != null) os.close();
        }
    }

    /**
     * 关闭socket对象及相关流对象
     *
     * @param is     输入流
     * @param os     输出流
     * @param socket 对象
     * @throws IOException 异常
     */
    public static void close(InputStream is, OutputStream os, Socket socket) throws IOException {
        try {
            close(is, os);
        } finally {
            if (socket != null) socket.close();
        }
    }

    public static void disconnect(InputStream is, OutputStream os, HttpURLConnection connection) throws IOException {
        try {
            close(is, os);
        } finally {
            if (connection != null) connection.disconnect();
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值