HttpUtil

import android.util.Log;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class HttpUtil {
    private static final String TAG = "HttpUtil";

    public static String get(String httpUrl){
        String result = null;
        Log.e(TAG, "httpUrl:"+httpUrl);
        BufferedReader br = null;
        try {
            URL url = new URL(httpUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            br = new BufferedReader(new InputStreamReader(connection.getInputStream(),"UTF-8"));
            int responseCode = connection.getResponseCode();
            Log.e(TAG, "responseCode: "+responseCode );
            if(responseCode == HttpURLConnection.HTTP_OK){
                StringBuffer sb = new StringBuffer();
                String line = null;
                while( (line = br.readLine()) != null ){
                    sb.append(line);
                }
                br.close();
                Log.e(TAG, "requestResult: " + sb.toString() );
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if(br != null)
                    br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }
    public static String post(String httpUrl , HashMap hashMap){
        String json = new JSONObject(hashMap).toString();
        Log.e(TAG, "httpUrl:"+httpUrl);
        Log.e(TAG, "request param:"+json);
        String result = null;
        BufferedWriter bw = null;
        BufferedReader br = null;
        try {
            URL url = new URL(httpUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);
            bw = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream()));
            bw.write(json);
            bw.flush();

            int responseCode = connection.getResponseCode();
            Log.e(TAG, "responseCode: "+responseCode );
            if(responseCode == HttpURLConnection.HTTP_OK){
                br = new BufferedReader(new InputStreamReader(connection.getInputStream(),"UTF-8"));
                StringBuffer sb = new StringBuffer();
                String line = null;
                while( (line = br.readLine()) != null ){
                    sb.append(line);
                }
                result = sb.toString();
                Log.e(TAG, "requestResult: " + result );
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if(bw != null)
                    bw.close();
                if(br != null)
                    br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    public static String post(String httpUrl , String json){
        Log.e(TAG, "httpUrl:"+httpUrl);
        Log.e(TAG, "request param:"+json);
        String result = null;
        BufferedWriter bw = null;
        BufferedReader br = null;
        try {
            URL url = new URL(httpUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);
            bw = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream()));
            bw.write(json);
            bw.flush();

            int responseCode = connection.getResponseCode();
            Log.e(TAG, "responseCode: "+responseCode );
            if(responseCode == HttpURLConnection.HTTP_OK){
                br = new BufferedReader(new InputStreamReader(connection.getInputStream(),"UTF-8"));
                StringBuffer sb = new StringBuffer();
                String line = null;
                while( (line = br.readLine()) != null ){
                    sb.append(line);
                }
                Log.e(TAG, "requestResult: " + sb.toString() );
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if(bw != null)
                    bw.close();
                if(br != null)
                    br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }

    public static String getHeadFieldsInfo(String httpUrl){
        String result = null;
        try {
            URL url = new URL(httpUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.connect();
            Map<String,List<String>> map = connection.getHeaderFields();
            Iterator<Map.Entry<String,List<String>>> iterator = map.entrySet().iterator();
            StringBuffer sb = new StringBuffer();
            sb.append("HeadFields\n");
            while (iterator.hasNext()){
                Map.Entry<String,List<String>> entry = iterator.next();
                sb.append("key:"+entry.getKey());
                for(String str : entry.getValue()){
                    sb.append("\t\tvalue:" + str);
                }
                sb.append("\n");
            }
            Log.e(TAG, sb.toString());
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值