HttpUrlConnection工具类

Posted kibana

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HttpUrlConnection工具类相关的知识,希望对你有一定的参考价值。

package com.ligotop.core.utils;

import com.ligotop.core.exceptions.BusinessException;

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

public class HttpMethodUtil {

    public static String doPost(String url,String params){
        HttpURLConnection conn = null;
        BufferedReader reader = null;
        PrintWriter writer = null;
        String content = null;
        StringBuffer sbf = new StringBuffer();
        try{
            URL u = new URL(url);
            conn = (HttpURLConnection)u.openConnection();
            conn.setDoInput(true);
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            conn.setReadTimeout(50000);
            conn.setConnectTimeout(60000);
            conn.setRequestProperty("accept","*/*");
            conn.setRequestProperty("connection","Keep-Alive");
            conn.setRequestProperty("content-Type","application/json");

            writer = new PrintWriter(new OutputStreamWriter(conn.getOutputStream(),"UTF-8"));
            writer.print(params);
            writer.flush();

            reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            while((content = reader.readLine())!=null){
                sbf.append(content);
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            if(writer!=null){
                writer.close();
            }
            if(reader!=null){
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            conn.disconnect();
        }
        return sbf.toString();

    }

    public static String doGet(String url){
        StringBuffer sbf = new StringBuffer();
        HttpURLConnection conn = null;
        BufferedReader br = null;
        String content = null;
        try{
            URL u = new URL(url);
            conn = (HttpURLConnection)u.openConnection();
            conn.setReadTimeout(50000);
            conn.setConnectTimeout(60000);
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            if(conn.getResponseCode()==200){
                br = new BufferedReader(new InputStreamReader(conn.getInputStream(),"utf-8"));
                while((content=br.readLine())!=null){
                    sbf.append(content);
                }
            }
        }catch(Exception e){
            e.printStackTrace();
            throw new BusinessException("请求数据失败");
        }finally{
            if(br!=null){
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            conn.disconnect();
        }
        return sbf.toString();
    }

}

以上是关于HttpUrlConnection工具类的主要内容,如果未能解决你的问题,请参考以下文章

HttpUrlConnection工具类

HttpURLConnection 当作请求调用接口不带返回参数的工具类

elasticsearch代码片段,及工具类SearchEsUtil.java

HTTP客户端代码片段

Android之通过HttpURLConnection.getResponseCode状态码抛出异常的问题以及解决方法

solr分布式索引实战分片配置读取:工具类configUtil.java,读取配置代码片段,配置实例