HttpSend 工具类Post Get请求

package com.yuuxin.wx.common;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @Author: WuChao
 * @ClassName:com.yuuxin.wx.common
 * @Description: 采用单例设计--调用时请使用instance函数调用方法
 * @date 2018/7/6
 */
public class HttpSendUtil {
    /*单例实现*/
    private HttpSendUtil() {
    }

    private static class HttpSendUtilInstance {
        private static final HttpSendUtil INSTANCE = new HttpSendUtil();
    }

    public static HttpSendUtil instance() {
        return HttpSendUtilInstance.INSTANCE;
    }

    /**
     * @Author:WuChao
     * @Description:使用HttpURLConnection 发送POST
     * @params: [url, params, encodType]
     * @Return: java.lang.String
     * @date:11:23 2018/7/6
     */
    public String sendPost(String sendUrl, String params, String encodType) {
        StringBuffer receive = new StringBuffer();

        HttpURLConnection URLConn = null;
        BufferedWriter bw = null;
        BufferedReader br = null;

        try {
            URL url = new URL(sendUrl);
            URLConn = (HttpURLConnection) url.openConnection();
            URLConn.setRequestMethod("POST");
            URLConn.setDoOutput(true);
            URLConn.setDoInput(true);
            URLConn.setUseCaches(false);
            URLConn.setAllowUserInteraction(true);
            HttpURLConnection.setFollowRedirects(true);
            URLConn.setInstanceFollowRedirects(true);

            URLConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
            URLConn.setRequestProperty("Content-Length", String.valueOf(params.getBytes().length));

            DataOutputStream dos = new DataOutputStream(URLConn.getOutputStream());
            dos.writeBytes(params);

            br = new BufferedReader(new InputStreamReader(URLConn.getInputStream(), encodType));

            String line;
            while ((line = br.readLine()) != null) {
                receive.append(line).append("\r\n");
            }

            br.close();

        } catch (java.io.IOException e) {
            receive.append("访问产生了异常-->").append(e.getMessage());
            e.printStackTrace();
        } finally {
            if (bw != null) {
                try {
                    bw.close();
                } catch (IOException ex) {
                    bw = null;
                    ex.printStackTrace();
                } finally {
                    if (URLConn != null) {
                        URLConn.disconnect();
                        URLConn = null;
                    }
                }
            }
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    br = null;
                    throw new RuntimeException(e);
                } finally {
                    if (URLConn != null) {
                        URLConn.disconnect();
                        URLConn = null;
                    }
                }
            }
        }

        return receive.toString();
    }


    /**
     * @Author:WuChao
     * @Description:使用HttpURLConnection 发送GET
     * @params: [sendUrl, encodType]
     * @Return: java.lang.String
     * @date:11:50 2018/7/6
     */
    public String sendGet(String sendUrl, String encodType) {
        StringBuffer receive = new StringBuffer();
        BufferedReader br = null;
        HttpURLConnection URLConn = null;
        try {
            URL url = new URL(sendUrl);
            URLConn = (HttpURLConnection) url.openConnection();

            URLConn.setDoInput(true);
            URLConn.setDoOutput(true);
            URLConn.connect();
            URLConn.getOutputStream().flush();
            br = new BufferedReader(new InputStreamReader(URLConn.getInputStream(), encodType));

            String line;
            while ((line = br.readLine()) != null) {
                receive.append(line).append("\r\n");
            }

        } catch (IOException e) {
            receive.append("访问产生了异常-->").append(e.getMessage());
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (java.io.IOException ex) {
                    br = null;
                    ex.printStackTrace();
                } finally {
                    if (URLConn != null) {
                        URLConn.disconnect();
                        URLConn = null;
                    }
                }
            }
        }

        return receive.toString();
    }
}

转载于:https://my.oschina.net/fusublog/blog/1841406

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值