⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 soapinvoke.java

📁 国外的j2me播放器软件
💻 JAVA
字号:
package no.auc.one.portableplayer.communication.soap;

import java.io.*;
import javax.microedition.io.*;

import no.auc.one.portableplayer.communication.HttpServiceUnavailableException;

/**
 * Invoke a SOAP action.
 *
 * Send a body describing the SOAP action to a server, and will then return 
 * the answer. Two usages of this class is supported:
 * <ul>
 *  <li>Single shot invoke: For invoking a single action.</li>
 *  <li>Multiple invokes: For invoking several actions sequential.</li>
 * </ul>
 */
public class SoapInvoke {
    private String actionUrl = null;
    private String soapAction = null;
    private String requestMethod = HttpConnection.POST;
    private String body = null;

    /**
     * Default constructor, initializes to default values.
     */ 
    public SoapInvoke() {
    }

    /**
     * Supports multiple invoke usage.
     *
     * @param url URL to send the action requests to.
     */
    public SoapInvoke(String url) {
        setActionUrl(url);
    }

    /**
     * Supports Single-shot invoke.
     *
     * @param action SOAP action to invoke.
     *
     * @param url URL to send the action requests to.
     *
     * @see #actionUrl
     * @see #soapAction
     */
    public SoapInvoke(String action, String url) {
        setSoapAction(action);
        setActionUrl(url);
    }

    /**
     * Get request method.
     *
     * @return The current request method used by this class. Default is POST.
     * @see #requestMethod
     */
    public String getRequestMethod() {
        return requestMethod;
    }

    /**
     * Set request method.
     *
     * @param req Requested request method to use. Should be a valid HTTP 
     *            request.
     * @see #requestMethod
     */
    public void setRequestMethod(String req) {
        requestMethod = req;
    }

    /**
     * Get the body of the next action to invoke.
     *
     * @return Body of the next action to invoke.
     */
    public String getBody() {
        return body;
    }

    /**
     * Set the body of the next action to invoke.
     *
     * @param b New body.
     */
    public void setBody(String b) {
        body = b;
    }

    /**
     * Set action URL.
     *
     * @param url New action URL to use for future invokes
     */
    public void setActionUrl(String url) {
        actionUrl = url;
    }

    /**
     * Get current action URL.
     *
     * @return Current action URL.
     */
    public String getActionUrl() {
        return actionUrl;
    }

    /**
     * Set SOAP action.
     *
     * @param action SOAP action to be requested.
     */
    public void setSoapAction(String action) {
        soapAction = action;
    }

    /**
     * Get SOAP action.
     *
     * @return Current SOAP action.
     */
    public String getSoapAction() { 
        return soapAction;
    }

    /**
     * Invoke a specific SOAP action.
     *
     * @param action Action to be invoked at the server.
     *
     * @return Result of this request.
     */
    public String invoke(String action) throws IOException {
        setSoapAction(action);
        return invoke();
    }

    /**
     * Invoke a SOAP action based on the current settings of this class.
     *
     * @return Result of this request.
     */
    public String invoke() throws IOException {
        HttpConnection connection = null;
        String response = null;
        
        try {
            byte[] contentData = body.getBytes();
        
            connection =
                (HttpConnection) Connector.open(
                    getActionUrl(),
                    Connector.READ_WRITE,
                    true);

            connection.setRequestProperty(
                "SOAPAction", getSoapAction());
            connection.setRequestProperty(
                "Content-Type", "text/xml ; charset=\"utf-8\"");
            connection.setRequestProperty(
                "Content-Length", "" + contentData.length);
            connection.setRequestProperty("User-Agent", "ONE-SOAP/2.0");
            connection.setRequestMethod(getRequestMethod());

            OutputStream os = connection.openOutputStream();
            os.write(contentData, 0, contentData.length);
            os.close();
            
            int statusCode = connection.getResponseCode();
            if (statusCode == 200) {
                contentData = null;
    
                InputStream is = null;
                int connectionAttempts = 3;
                do {
                    try {
                        is = connection.openInputStream();
                    } catch (IOException ioe) {
                        connectionAttempts--;
                    }
                } while (is == null && connectionAttempts > 0);
                
                if (is != null) {
                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
                    byte[] buf = new byte[256];
        
                    while (true) {
                        int rd = is.read(buf, 0, 256);
                        if (rd == -1)
                            break;
                        bos.write(buf, 0, rd);
                    }
                    is.close();            
        
                    response = new String(bos.toByteArray());
                }            
            } else if (statusCode == 503) {
                throw new HttpServiceUnavailableException(
                        connection.getResponseMessage(), 
                        connection.getHeaderFieldInt("Retry-After", -1));
            }/* XXX Enable after debugging completed! else {
                throw new IOException(connection.getResponseMessage());
            }*/
        } catch (IOException ioe) {
            System.out.println("IOException occured while invoking SOAP");
            ioe.printStackTrace();
            throw ioe;
        } finally {
            if (connection != null) {
                connection.close();
            }
        }

	    return response;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -