<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    athrunwang

    紀元
    數據加載中……
    [導入][轉載]java中用URLConnection 類post方式提交表單
    /**
     * <pre>
     * Title:         HttpRequestProxy.java
     * Project:     HP-Common
     * Type:        com.hengpeng.common.web.HttpRequestProxy
     * Author:        benl
     * Create:         2007-7-3 上午03:07:07
     * Copyright:     Copyright (c) 2007
     * Company:
     * <pre>
     */
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import java.net.URLEncoder;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Map;
    import java.util.Map.Entry;

    import org.apache.log4j.Logger;

    /**
     * <pre>
     * HTTP請求代理類
     * </pre>
     *
     * @author benl
     * @version 1.0, 2007-7-3
     */
    public class HttpRequestProxy
    {
        /**
         * 連接超時
         */
        private static int connectTimeOut = 5000;

        /**
         * 讀取數據超時
         */
        private static int readTimeOut = 10000;

        /**
         * 請求編碼
         */
        private static String requestEncoding = "GBK";

        private static Logger logger = Logger.getLogger(HttpRequestProxy.class);

        /**
         * <pre>
         * 發送帶參數的GET的HTTP請求
         * </pre>
         *
         * @param reqUrl HTTP請求URL
         * @param parameters 參數映射表
         * @return HTTP響應的字符串
         */
        public static String doGet(String reqUrl, Map parameters,
                String recvEncoding)
        {
            HttpURLConnection url_con = null;
            String responseContent = null;
            try
            {
                StringBuffer params = new StringBuffer();
                for (Iterator iter = parameters.entrySet().iterator(); iter
                        .hasNext();)
                {
                    Entry element = (Entry) iter.next();
                    params.append(element.getKey().toString());
                    params.append("=");
                    params.append(URLEncoder.encode(element.getValue().toString(),
                            HttpRequestProxy.requestEncoding));
                    params.append("&");
                }

                if (params.length() > 0)
                {
                    params = params.deleteCharAt(params.length() - 1);
                }

                URL url = new URL(reqUrl);
                url_con = (HttpURLConnection) url.openConnection();
                url_con.setRequestMethod("GET");
                System.setProperty("sun.net.client.defaultConnectTimeout", String
                        .valueOf(HttpRequestProxy.connectTimeOut));// (單位:毫秒)jdk1.4換成這個,連接超時
                System.setProperty("sun.net.client.defaultReadTimeout", String
                        .valueOf(HttpRequestProxy.readTimeOut)); // (單位:毫秒)jdk1.4換成這個,讀操作超時
                // url_con.setConnectTimeout(5000);//(單位:毫秒)jdk
                // 1.5換成這個,連接超時
                // url_con.setReadTimeout(5000);//(單位:毫秒)jdk 1.5換成這個,讀操作超時
                url_con.setDoOutput(true);
                byte[] b = params.toString().getBytes();
                url_con.getOutputStream().write(b, 0, b.length);
                url_con.getOutputStream().flush();
                url_con.getOutputStream().close();

                InputStream in = url_con.getInputStream();
                BufferedReader rd = new BufferedReader(new InputStreamReader(in,
                        recvEncoding));
                String tempLine = rd.readLine();
                StringBuffer temp = new StringBuffer();
                String crlf=System.getProperty("line.separator");
                while (tempLine != null)
                {
                    temp.append(tempLine);
                    temp.append(crlf);
                    tempLine = rd.readLine();
                }
                responseContent = temp.toString();
                rd.close();
                in.close();
            }
            catch (IOException e)
            {
                logger.error("網絡故障", e);
            }
            finally
            {
                if (url_con != null)
                {
                    url_con.disconnect();
                }
            }

            return responseContent;
        }

        /**
         * <pre>
         * 發送不帶參數的GET的HTTP請求
         * </pre>
         *
         * @param reqUrl HTTP請求URL
         * @return HTTP響應的字符串
         */
        public static String doGet(String reqUrl, String recvEncoding)
        {
            HttpURLConnection url_con = null;
            String responseContent = null;
            try
            {
                StringBuffer params = new StringBuffer();
                String queryUrl = reqUrl;
                int paramIndex = reqUrl.indexOf("?");

                if (paramIndex > 0)
                {
                    queryUrl = reqUrl.substring(0, paramIndex);
                    String parameters = reqUrl.substring(paramIndex + 1, reqUrl
                            .length());
                    String[] paramArray = parameters.split("&");
                    for (int i = 0; i < paramArray.length; i++)
                    {
                        String string = paramArray[i];
                        int index = string.indexOf("=");
                        if (index > 0)
                        {
                            String parameter = string.substring(0, index);
                            String value = string.substring(index + 1, string
                                    .length());
                            params.append(parameter);
                            params.append("=");
                            params.append(URLEncoder.encode(value,
                                    HttpRequestProxy.requestEncoding));
                            params.append("&");
                        }
                    }

                    params = params.deleteCharAt(params.length() - 1);
                }

                URL url = new URL(queryUrl);
                url_con = (HttpURLConnection) url.openConnection();
                url_con.setRequestMethod("GET");
                System.setProperty("sun.net.client.defaultConnectTimeout", String
                        .valueOf(HttpRequestProxy.connectTimeOut));// (單位:毫秒)jdk1.4換成這個,連接超時
                System.setProperty("sun.net.client.defaultReadTimeout", String
                        .valueOf(HttpRequestProxy.readTimeOut)); // (單位:毫秒)jdk1.4換成這個,讀操作超時
                // url_con.setConnectTimeout(5000);//(單位:毫秒)jdk
                // 1.5換成這個,連接超時
                // url_con.setReadTimeout(5000);//(單位:毫秒)jdk 1.5換成這個,讀操作超時
                url_con.setDoOutput(true);
                byte[] b = params.toString().getBytes();
                url_con.getOutputStream().write(b, 0, b.length);
                url_con.getOutputStream().flush();
                url_con.getOutputStream().close();
                InputStream in = url_con.getInputStream();
                BufferedReader rd = new BufferedReader(new InputStreamReader(in,
                        recvEncoding));
                String tempLine = rd.readLine();
                StringBuffer temp = new StringBuffer();
                String crlf=System.getProperty("line.separator");
                while (tempLine != null)
                {
                    temp.append(tempLine);
                    temp.append(crlf);
                    tempLine = rd.readLine();
                }
                responseContent = temp.toString();
                rd.close();
                in.close();
            }
            catch (IOException e)
            {
                logger.error("網絡故障", e);
            }
            finally
            {
                if (url_con != null)
                {
                    url_con.disconnect();
                }
            }

            return responseContent;
        }

        /**
         * <pre>
         * 發送帶參數的POST的HTTP請求
         * </pre>
         *
         * @param reqUrl HTTP請求URL
         * @param parameters 參數映射表
         * @return HTTP響應的字符串
         */
        public static String doPost(String reqUrl, Map parameters,
                String recvEncoding)
        {
            HttpURLConnection url_con = null;
            String responseContent = null;
            try
            {
                StringBuffer params = new StringBuffer();
                for (Iterator iter = parameters.entrySet().iterator(); iter
                        .hasNext();)
                {
                    Entry element = (Entry) iter.next();
                    params.append(element.getKey().toString());
                    params.append("=");
                    params.append(URLEncoder.encode(element.getValue().toString(),
                            HttpRequestProxy.requestEncoding));
                    params.append("&");
                }

                if (params.length() > 0)
                {
                    params = params.deleteCharAt(params.length() - 1);
                }

                URL url = new URL(reqUrl);
                url_con = (HttpURLConnection) url.openConnection();
                url_con.setRequestMethod("POST");
                System.setProperty("sun.net.client.defaultConnectTimeout", String
                        .valueOf(HttpRequestProxy.connectTimeOut));// (單位:毫秒)jdk1.4換成這個,連接超時
                System.setProperty("sun.net.client.defaultReadTimeout", String
                        .valueOf(HttpRequestProxy.readTimeOut)); // (單位:毫秒)jdk1.4換成這個,讀操作超時
                // url_con.setConnectTimeout(5000);//(單位:毫秒)jdk
                // 1.5換成這個,連接超時
                // url_con.setReadTimeout(5000);//(單位:毫秒)jdk 1.5換成這個,讀操作超時
                url_con.setDoOutput(true);
                byte[] b = params.toString().getBytes();
                url_con.getOutputStream().write(b, 0, b.length);
                url_con.getOutputStream().flush();
                url_con.getOutputStream().close();

                InputStream in = url_con.getInputStream();
                BufferedReader rd = new BufferedReader(new InputStreamReader(in,
                        recvEncoding));
                String tempLine = rd.readLine();
                StringBuffer tempStr = new StringBuffer();
                String crlf=System.getProperty("line.separator");
                while (tempLine != null)
                {
                    tempStr.append(tempLine);
                    tempStr.append(crlf);
                    tempLine = rd.readLine();
                }
                responseContent = tempStr.toString();
                rd.close();
                in.close();
            }
            catch (IOException e)
            {
                logger.error("網絡故障", e);
            }
            finally
            {
                if (url_con != null)
                {
                    url_con.disconnect();
                }
            }
            return responseContent;
        }

        /**
         * @return 連接超時(毫秒)
         * @see com.hengpeng.common.web.HttpRequestProxy#connectTimeOut
         */
        public static int getConnectTimeOut()
        {
            return HttpRequestProxy.connectTimeOut;
        }

        /**
         * @return 讀取數據超時(毫秒)
         * @see com.hengpeng.common.web.HttpRequestProxy#readTimeOut
         */
        public static int getReadTimeOut()
        {
            return HttpRequestProxy.readTimeOut;
        }

        /**
         * @return 請求編碼
         * @see com.hengpeng.common.web.HttpRequestProxy#requestEncoding
         */
        public static String getRequestEncoding()
        {
            return requestEncoding;
        }

        /**
         * @param connectTimeOut 連接超時(毫秒)
         * @see com.hengpeng.common.web.HttpRequestProxy#connectTimeOut
         */
        public static void setConnectTimeOut(int connectTimeOut)
        {
            HttpRequestProxy.connectTimeOut = connectTimeOut;
        }

        /**
         * @param readTimeOut 讀取數據超時(毫秒)
         * @see com.hengpeng.common.web.HttpRequestProxy#readTimeOut
         */
        public static void setReadTimeOut(int readTimeOut)
        {
            HttpRequestProxy.readTimeOut = readTimeOut;
        }

        /**
         * @param requestEncoding 請求編碼
         * @see com.hengpeng.common.web.HttpRequestProxy#requestEncoding
         */
        public static void setRequestEncoding(String requestEncoding)
        {
            HttpRequestProxy.requestEncoding = requestEncoding;
        }
       
        public static void main(String[] args)
        {
            Map map = new HashMap();
            map.put("actionType", "1");
    //        map.put("issueId", "33");
            String temp = HttpRequestProxy.doPost("http://192.168.0.99/AgentPortal/autoHandler", map, "GBK");
            System.out.println("返回的消息是:"+temp);
           
        }
    }

    文章來源:http://blog.163.com/ccbobo_cat/blog/static/320994622009616102329953

    posted on 2011-09-28 13:05 AthrunWang 閱讀(11182) 評論(0)  編輯  收藏


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 亚洲精品无码你懂的网站| 亚洲国产成人私人影院| 国产线视频精品免费观看视频| 久久综合九九亚洲一区| 成人无遮挡裸免费视频在线观看| 无遮挡呻吟娇喘视频免费播放| 亚洲成人精品久久| 国产又黄又爽又猛的免费视频播放 | 亚洲AV无码一区二区二三区软件 | 黄色片免费在线观看| 亚洲午夜在线播放| 亚洲一区二区三区AV无码| 91在线视频免费播放| 国产高潮久久免费观看| 亚洲综合校园春色| 亚洲国产美女精品久久久久∴| 成年女人毛片免费播放人| 在线观看免费黄网站| 亚洲国产欧美日韩精品一区二区三区 | 特黄aa级毛片免费视频播放| 久久精品国产亚洲77777| 亚洲精品无码永久在线观看| 精品熟女少妇AV免费观看| 精品免费视在线观看| 黄色免费网址大全| 亚洲中文字幕无码中文字| 久久久久亚洲av无码专区蜜芽| 四虎永久在线精品免费影视| 青娱分类视频精品免费2| 免费av一区二区三区| 色多多A级毛片免费看| 亚洲字幕AV一区二区三区四区| 久久久久亚洲精品美女| 国产亚洲午夜高清国产拍精品| 免费观看亚洲人成网站| 无码日韩人妻av一区免费| 亚洲免费精彩视频在线观看| 一级全免费视频播放| 午夜亚洲乱码伦小说区69堂| 亚洲一区二区三区写真| 亚洲精品欧洲精品|