📄 .#soapinvoke.java.1.4
字号:
package no.auc.one.portableplayer.communication.soap;
import java.io.*;
import javax.microedition.io.*;
/**
* 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) {
setSoapAction(action);
return invoke();
}
/**
* Invoke a SOAP action based on the current settings of this class.
*
* @return Result of this request.
*/
public String invoke() {
HttpConnection connection;
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();
contentData = null;
InputStream is = connection.openInputStream();
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();
connection.close();
response = new String(bos.toByteArray());
} catch (IOException ioe) {
ioe.printStackTrace();
}
return response;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -