Post请求时参数为xml的方法

联调接口时,有一个接口入参是xml接口。发现系统没有针对这个类型的工具类,不得不自己手撸一个,在此记录,有需要请拿走。

public static String sendXmlPost(String url, String xml,String appId,String appKey) {
    // 设置默认请求头
    StringBuffer response = new StringBuffer();
    org.apache.commons.httpclient.HttpClient client = new HttpClient();
    PostMethod method = new PostMethod(url);
    BufferedReader reader = null;
    try {
        method.setRequestHeader("X-APP-ID",appId);
        method.setRequestHeader("X-APP-KEY",appKey);
        RequestEntity requestEntity = new StringRequestEntity(xml, "application/xml", "UTF-8");// application/json,text/xml,text/plain
        method.setRequestEntity(requestEntity);
        client.executeMethod(method);
        reader = new BufferedReader(new InputStreamReader(method.getResponseBodyAsStream(), "UTF-8"));
        String line;
        while ((line = reader.readLine()) != null) {
            if (false) {
                response.append(line).append(System.getProperty("line.separator"));
            } else {
                response.append(line);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }finally {
        try {
            if (reader != null) {
                reader.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        method.releaseConnection();
    }
    return response.toString();


}
上一篇:全屏滚动效果H5FullscreenPage.js


下一篇:Reader及其子类