HttpURLConnection的使用

public class HttpUtil {

    /**
     connection.setRequestMethod("POST");   //设置POST方式连接
     connection.setConnectTimeout(2000);    //请求超时时间(毫秒)
     connection.setUseCaches(false);  //POST不允许缓存(测试时用true也没异常)
     connection.setDoInput(true);   //需要输入  默认为true
     connection.setDoOutput(true);   //需要输出 默认为false

     connection.setRequestProperty("Charset", "UTF-8");     //字符编码
     connection.setRequestProperty("Content-length", "" + json.getBytes().length);
     connection.setRequestProperty("Content-Type", "application/octet-stream");
     connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
     // 设定传送的内容类型是可序列化的java对象
     // (如果不设此项,在传送序列化对象时,当WEB服务默认的不是这种类型时可能抛java.io.EOFException)
     httpUrlConnection.setRequestProperty("Content-type", "application/x-java-serialized-object");
     connection.setRequestProperty("Connection", "Keep-Alive");// 维持长连接
     */
    public static String post(String httpUrl , String json){
        Logger.e("httpUrl:"+httpUrl);
        Logger.e("request param:"+json);
        String result = null;
        BufferedWriter bw = null;
        BufferedReader br = null;
        try {
            URL url = new URL(httpUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setConnectTimeout(2000);
            connection.setDoOutput(true);

            bw = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream()));
            bw.write(json);
            bw.flush();

            int responseCode = connection.getResponseCode();
            Logger.e("responseCode:"+responseCode);
            if(responseCode == HttpURLConnection.HTTP_OK){
                br = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                StringBuffer sb = new StringBuffer();
                String line = null;
                while( (line = br.readLine()) != null ){
                    sb.append(line);
                }
                Logger.e("response result:"+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;
    }

    /**
     HeadFields
     key:null		value:HTTP/1.1 200 OK
     key:Access-control-allow-origin		value:*
     key:Content-length		value:1223
     key:Content-type		value:text/plain; charset=utf-8
     key:Date		value:Fri, 17 Mar 2017 07:13:26 GMT
     key:X-Android-Received-Millis		value:1489734805955
     key:X-Android-Response-Source		value:NETWORK 200
     key:X-Android-Sent-Millis		value:1489734805932
     */
    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");
            }
            Logger.e(sb.toString());
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值