通过Post方式调用webservice

来源:互联网 发布:矩阵分解推荐算法 编辑:程序博客网 时间:2024/05/16 04:12

需要用到的jar包:
commons-codec-1.4.jar
commons-httpclient-3.1.jar
commons-logging-1.0.4.jar
jsoup-1.6.2.jar

    /**     * 通过Post方式请求webserviec方法     * @param methodPost 接口地址+"/"+请求的方法名     * @param host      * @param params 请求参数     * @return     * @throws HttpException     * @throws IOException     */public String webServicePost(String postMethod, String host, Map<String, String> params)            throws HttpException, IOException {        InputStream is = null;        HttpClient client = new HttpClient();        PostMethod method = new PostMethod(postMethod);        method.setRequestHeader("Host", host);        method.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");        for (String key : params.keySet()) {            method.setParameter(key, params.get(key));        }        client.executeMethod(method);        is = method.getResponseBodyAsStream();        org.jsoup.nodes.Document document = Jsoup.parse(is, "utf-8", "");        if (is != null)            is.close();        return document.toString();    }