Cisco Cisco Configuration Engine 3.5.3 Guida Dello Sviluppatore

Pagina di 348
3-9
Cisco Configuration Engine Software Development Kit API Reference and Programmer Guide 3.5.3
OL-17661-04
Chapter 3      Configuration Service
Template File Manager
JAVA
Below is a simple programmatic example of posting XML to the servlet using the multipart/form-data 
MIME content type. This simple java class PostUtil.java utilizes an HTTPClient class library from 
http://www.innovation.ch/java/HTTPClient The jar file HTTPClient.jar (which can be obtained from the 
above URL) should be in the classpath.
In this client example, you would be posting to your host system with hostname beethoven a 
<cns-request> XML document that you had saved in a file import.xml to the following 
URL:http://beethoven/cns/CommandProcessor
import HTTPClient.*;
import java.io.*;
import java.net.*;
public class PostUtil
{
    public static void main(String[] args)
    {
        String _sHost       = "beethoven";
        String _sPort       = "80";
        String _sPath       = "/cns/CommandProcessor";
        String _sParamName  = "command";
        String _sFileName   = "import.xml";
        try
        {
            String _sResponse = PostUtil.postFile(_sHost,
                                                  _sPort,
                                                  _sPath,
                                                  _sParamName,
                                                  _sFileName);
            System.out.println(_sResponse);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    /**
     *  This method uploads a file to a server via a HTTP post().
     *  The file content will be included as the value of a name/value pair
     *  encoded using the 'multipart/form-data' MIME content-type
     *  (and therefore the encoding) and submitted to a process on the server
     *  that will receive and parse the form data.
     *
     *  @param path - path to servlet/cgi that will process the post
     *  @param paramName - name in the name/value pair submitted in form
     *  @param uploadFileName - name of file whose content will be the value
     */
    public static String postFile(String host,
                                  String port,
                                  String path,
                                  String paramName,
                                  String uploadFileName)
    throws IOException, ModuleException
    {
        // arg validation code left out for brevity
        StringBuffer _sBuff = new StringBuffer();
        URL url = new URL("http://" + host + ":" + port);