POST 发送HTTP请求入参为:String url, Map propsMap

/**
     * 发送HTTP请求
     *
     * @param url
     * @param propsMap
     *            发送的参数
     */
    public static String httpSend(String url, Map<String, Object> propsMap) {
        String responseMsg = "";

        HttpClient httpClient = new HttpClient();
        PostMethod postMethod = new PostMethod(url);// POST请求
        // 参数设置
        Set<String> keySet = propsMap.keySet();
        NameValuePair[] postData = new NameValuePair[keySet.size()];
        int index = 0;
        for (String key : keySet) {
            postData[index++] = new NameValuePair(key, propsMap.get(key).toString());
        }
        postMethod.addParameters(postData);
        try {
            httpClient.executeMethod(postMethod);// 发送请求
            // 读取内容
            byte[] responseBody = postMethod.getResponseBody();
            // 处理返回的内容
            responseMsg = new String(responseBody);

        } catch (HttpException e) {
            e.printStackTrace();
            Logger.getLogger(PaymentAction.class).error(e.getMessage());
        } catch (IOException e) {
            e.printStackTrace();
            Logger.getLogger(PaymentAction.class).error(e.getMessage());
        } finally {
            postMethod.releaseConnection();// 关闭连接
        }
        return responseMsg;
    }
    
上一篇:Python之路(第一篇):Python简介和基础


下一篇:【Unity3d游戏开发】unity3D OnTriggerEnter和OnCollisionEnter的一点个人心得(转载)