HttpUrlConnection 进行post提交方式

package com.gxt.utils;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
/**
 *
 *@ 罗文浩
 * GetSingHttpClient 获取HttpClient 2015-1-10 下午8:41:37
 *
 * @version 1.0.0
 *
 */
public class SingHttpConnection extends HttpURLConnectionFactory {
    Logger log = Logger.getLogger("SingHttpConnection");

    public SingHttpConnection() {

    }

    public static SingHttpConnection getSingHttpConnection() {
        return SingletonHolderHttpConnection.INSTANCE;
    }

    private static class SingletonHolderHttpConnection {
        private static  SingHttpConnection INSTANCE = new SingHttpConnection();

        public static String getHttpConnection(String url,int timeount) {
            HttpURLConnection connection = null;
            String line = null;
            String result = null;
            BufferedReader reader = null;
            long startTime=System.currentTimeMillis();
            try {
                URL postUrl = new URL(url);
                connection = (HttpURLConnection) postUrl.openConnection();
                connection.setConnectTimeout(timeount);
                //connection.setReadTimeout(timeount);
                connection.setDoOutput(true);
                connection.setDoInput(true);
                connection.setRequestMethod("POST");
                connection.setUseCaches(true);
                connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
                connection.connect();
                reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                while ((line = reader.readLine()) != null) {
                    result = line;
                }
                long endTime=System.currentTimeMillis();
                System.out.println("系统链接时间="+(endTime-startTime));
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    reader.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                connection.disconnect();
            }
            return result;
        }
        public static String getHttpConnectionJson(String requestUrl,int timeount,String datajson) {
            BufferedReader bufferedReader = null;
            StringBuffer responseResult = new StringBuffer();
            HttpURLConnection httpURLConnection = null;
            try {
                URL realUrl = new URL(requestUrl);
                // 打开和URL之间的连接
                httpURLConnection = (HttpURLConnection) realUrl.openConnection();
                // 设置通用的请求属性
                httpURLConnection.setRequestProperty("accept", "*/*");
                httpURLConnection.setRequestProperty("connection", "Keep-Alive");
                httpURLConnection.setRequestProperty("Content-Length", String.valueOf(datajson.length()));
                // 发送POST请求必须设置如下两行
                httpURLConnection.setDoOutput(true);
                httpURLConnection.setDoInput(true);
                // 获取URLConnection对象对应的输出流
                OutputStream outStream = httpURLConnection.getOutputStream();
                // 发送请求参数
                byte[] data = datajson.getBytes("utf-8");
                outStream.write(data);
                // flush输出流的缓冲
                outStream.flush();
                // 根据ResponseCode判断连接是否成功
                int responseCode = httpURLConnection.getResponseCode();
                if (responseCode != 200) {
                    System.out.println("post请求失败");
                } else {
                    System.out.println("post请求成功");
                }
                // 定义BufferedReader输入流来读取URL的ResponseData
                bufferedReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
                String line;
                while ((line = bufferedReader.readLine()) != null) {
                    responseResult.append(line);
                }
            } catch (Exception e) {
                System.out.println("send post request error!" + e);
            } finally {
                httpURLConnection.disconnect();
                try {
                    if (bufferedReader != null) {
                        bufferedReader.close();
                    }
                } catch (IOException ex) {
                    ex.printStackTrace();
                }

            }
            return responseResult.toString();
    }

}

    /* (non Javadoc)
     * @Title : getDefaultHttpConnection
     * @Description: TODO
     * @param url
     * @param timeount
     * @return
     * @see com.gxt.utils.HttpURLConnectionFactory#getDefaultHttpConnection(java.lang.String, int)
     */
    @Override
    public String getDefaultHttpConnection(String url, int timeount) {
        return SingletonHolderHttpConnection.getHttpConnection(url,timeount);
    }

    /* (non Javadoc)
     * @Title: getDefaultHttpConnectionJson
     * @Description: TODO
     * @param url
     * @param timeout
     * @param jsonstr
     * @return
     * @see com.gxt.utils.HttpURLConnectionFactory#getDefaultHttpConnectionJson(java.lang.String, int, java.lang.String)
     */
    @Override
    public String getDefaultHttpConnectionJson(String url, int timeout, String jsonstr) {
        // TODO Auto-generated method stub
        return SingletonHolderHttpConnection.getHttpConnectionJson(url, timeout, jsonstr);
    }
}

转载于:https://my.oschina.net/lwhmdj0823/blog/651173

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值