java传输soap

来源:互联网 发布:电脑自动改mac地址 编辑:程序博客网 时间:2024/05/17 23:39
public String postSoap(String soap, String requestName) throws IOException {        // Post请求的url,与get不同的是不需要带参数        URL postUrl = new URL(POST_URL);        // 打开连接        HttpURLConnection connection = (HttpURLConnection) postUrl                .openConnection();        logger.info(requestName + " request: " + soap);        connection.setDoOutput(true);        connection.setDoInput(true);        connection.setRequestMethod("POST");        connection.setUseCaches(false);        connection.setInstanceFollowRedirects(true);        connection.setRequestProperty("Content-Type", "text/xml; charset=utf-8");        connection.setRequestProperty("SOAPAction", "http://ispp.com.cn/ispp_npi/SYNNPIAPI");               StringBuilder result = new StringBuilder();        BufferedReader reader = null;        try {        connection.connect();            DataOutputStream out = new DataOutputStream(connection                    .getOutputStream());            out.write(soap.getBytes("UTF-8"));             out.flush();            out.close(); // flush and close            reader = new BufferedReader(new InputStreamReader(                    connection.getInputStream()));            String line;            while ((line = reader.readLine()) != null) {            result.append(line);            }} catch (Exception e) {logger.error(requestName + " error: ", e);}finally{ reader.close();     connection.disconnect();}                return result.toString();    }

0 0
原创粉丝点击