HTTP GET/POST PARAM

Java实现HTTP请求

import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.net.*;

public class HTTPUtil {

    public static String HTTPGet(String url, String param) {
        InputStream inputStream = null;
        BufferedReader bufferedReader = null;
        HttpURLConnection connection = null;
        StringBuffer stringBuffer = null;
        try {
            String urlNameString = url + (StringUtils.isBlank(param) ? "" : ("?" + param));
            //创建连接
            URL realUrl = new URL(urlNameString);
            //            URL url = new URL("http://192.168.31.179:19303/entity-area/queryAreaList");

            //通过远程url连接对象打开一个连接,强转成httpURLConnection类
            connection = (HttpURLConnection) realUrl.openConnection();

            // 设置通用的请求属性
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");

            // 设置连接方式:get
            connection.setRequestMethod("GET");
            // 设置连接主机服务器的超时时间:15000毫秒
            connection.setConnectTimeout(15000);
            // 设置读取远程返回的数据时间:60000毫秒
            connection.setReadTimeout(60000);
            // 发送请求
            connection.connect();
            //获取http响应code值
            if (connection.getResponseCode() == 200) {
                // 通过connection连接,获取输入流
                inputStream = connection.getInputStream();
                // 封装输入流inputStream,并指定字符集
                bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
                stringBuffer = new StringBuffer();
                String temp = null;
                while ((temp = bufferedReader.readLine()) != null) {
                    stringBuffer.append(temp);
                    stringBuffer.append("\r\n");
                }
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ProtocolException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != inputStream) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    System.err.println(e.getCause().getMessage());
                }
            }
            if (null != inputStream) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    System.err.println(e.getCause().getMessage());
                }
            }
            connection.disconnect();// 关闭远程连接
        }

        return null == stringBuffer ? "" : stringBuffer.toString();
    }


    public static String HTTPPost(String url, String param) {
        HttpURLConnection connection = null;
        StringBuffer stringBuffer = null;
        InputStream inputStream = null;
        InputStreamReader inputStreamReader = null;
        BufferedReader bufferedReader = null;

        try {
            URL realUrl = new URL(url);
            connection = (HttpURLConnection) realUrl.openConnection();
            connection.setRequestMethod("POST");
            // 设置通用的请求属性
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
//       URL连接可以用于输入和/或输出。如果想使用URL连接作为输入,设置DoInput *标志为true,如果不是假的。默认为true
            connection.setDoOutput(true);
            connection.setDoInput(true);
// 获取URLConnection对象对应的输出流
            PrintWriter out = new PrintWriter(connection.getOutputStream());
            // 发送请求参数
            out.print(param);
            // flush输出流的缓冲
            out.flush();
//发送请求
            connection.connect();
            inputStream = connection.getInputStream();
            inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
            bufferedReader = new BufferedReader(inputStreamReader);
            stringBuffer = new StringBuffer();
            String temp = null;
            while ((temp = bufferedReader.readLine()) != null) {
                stringBuffer.append(temp);
                stringBuffer.append("\r\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != inputStream) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != inputStreamReader) {
                try {
                    inputStreamReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != bufferedReader) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != connection) {
                //关闭远程连接
                connection.disconnect();
            }
        }
        return stringBuffer.toString();
    }


  /*  public static void main(String[] args) {
// String param = "&param=param";
//        String s = HTTPUtil.HTTPGet("http://IP:端口/地址", param);


//        String s = HTTPUtil.HTTPPost("http://IP:端口/地址", param);

        System.out.println(s);
    }
*/

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值