java傳輸soap

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();
    }	

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章