NavigationUser login |
if_rig, appendix G annoying Java snippet ServletAdapterTest//Not pretty, but it works. public class ifbo { public static void main(String[] args) { String url; url = "http://server:port/apifw/APIFWAdapterServlet"; environment = ""; /* Read file */ String fileName = "c:/temp/ifbo/WORR.xml"; // Get the size of the file // Create the byte array to hold the data. try { FileInputStream fis = new FileInputStream(input); } catch (UnsupportedEncodingException ee) { System.out.println("filelength = " + filelength); /* end Read file */ try { java.io.OutputStream bos = new FileOutputStream(input); OutputStreamWriter osw = new OutputStreamWriter(bos, "UTF8"); osw.write(new String(fileByteArray, "UTF8")); osw.close(); PostMethod fwPost = new PostMethod(url); Part parts[] = { new StringPart("envName", ICEncodingUtils.urlEncode(environment, "UTF8")), new FilePart(input.getName(), input, "text/xml", "UTF8") }; fwPost.setRequestEntity(new MultipartRequestEntity(parts, fwPost.getParams())); HttpConnectionManagerParams connectionParams = new HttpConnectionManagerParams(); connectionParams.setConnectionTimeout(5000); HttpConnectionManager connectionManager = new SimpleHttpConnectionManager(); connectionManager.setParams(connectionParams); HttpClient httpClient = new HttpClient(connectionManager); int result = httpClient.executeMethod(fwPost); int httpCode = fwPost.getStatusCode(); String httpResponseText = fwPost.getStatusText(); Header headr = fwPost.getResponseHeader("ApifwRC"); headr = fwPost.getResponseHeader("InternalReqID"); String xmlResult = new String(); BufferedReader bufRdr = null; java.io.InputStream inStrm = fwPost.getResponseBodyAsStream(); InputStreamReader ISR = new InputStreamReader(inStrm); String sTmp; while((sTmp = bufRdr.readLine()) != null) xmlResult = xmlResult + sTmp; bufRdr.close(); fwPost.releaseConnection(); System.out.println("xmlResult:" + xmlResult); //input.delete(); return; } catch (Exception e) { } } By webmaster | Integration Framework Business Objects (IFBO) | add new comment
what exactly is in the multi part POST?Add this code to see what the multi part POST consists of: PostMethod fwPost = new PostMethod(url); //Documentation of "Part". Part parts[] = { new StringPart("envName", ICEncodingUtils.urlEncode(environment, "UTF8")), new FilePart(input.getName(), input, "text }; java.io.OutputStream ostream = null; for (int k = 0; k < parts.length; k++) { parts[k].send(BAOS); System.out.println("parts k=" + k + " " + parts[k]); } By Anonymous | reply
|
Appendix G pared to the minimum
//Note that MultipartPostMethod is not used. This program uses Apache Jakarta Http Client 3.0.1 (commons-httpclient-3.0.1.jar) in which MultipartPostMethod is deprecated.
import java.io.*;
import javax.servlet.http.*;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.*;
import org.apache.commons.httpclient.params.HttpConnectionManagerParams;
public class ifboG {
public static void main(String[] args) {
String url;
String environment;
String filename;
url = "http://server:port/apifw/APIFWAdapterServlet";
environment = "";
PostMethod fwPost = new PostMethod(url);
String testXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE InboundRequest [<!ELEMENT InboundRequest (APIHeader, BusinessObject)><!ENTITY % APIHeader SYSTEM \"http://apifw//xml/APIHeader.Inbound.Request.V060000.dtd\">%APIHeader;<!ENTITY % BusinessObject SYSTEM \"http://apifw//xml/WorkOrder.Read.Request.V060000.dtd\">%BusinessObject;]><InboundRequest><APIHeader ReplyTypeOK=\"A\" ReplyTypeError=\"N\"><apiAPIVersion>V060000</apiAPIVersion><apiBusinessObject>WorkOrder</apiBusinessObject><apiBusObjectMethod>Read</apiBusObjectMethod><apiBusObjectVersion>V060000</apiBusObjectVersion><apiUserID></apiUserID><apiExtSystemID></apiExtSystemID><apiExtRequestID></apiExtRequestID><apiRoutingInfo></apiRoutingInfo></APIHeader><BusinessObject><WoHeader><ExtSystemId></ExtSystemId><ExternalKey></ExternalKey><WorkOrderNbr>00211997</WorkOrderNbr><SubMethod></SubMethod><WoTaskHeader><WorkOrderTask>01</WorkOrderTask></WoTaskHeader></WoHeader></BusinessObject></InboundRequest>";
try {
//Add the Parameters with the data literals URL encoded in UTF-8
fwPost.addParameter("envName", "UTF8");
fwPost.addParameter("request", testXML);
HttpClient httpClient = new HttpClient();
httpClient.setConnectionTimeout(5000);
int result = httpClient.executeMethod(fwPost);
//Get and parse response
int httpCode = fwPost.getStatusCode();
String httpResponseText = fwPost.getStatusText();
Header headr = fwPost.getResponseHeader("ApifwRC");
headr = fwPost.getResponseHeader("InternalReqID");
String xmlResult = new String();
String sTmp;
BufferedReader bufRdr = null;
InputStream inStrm = fwPost.getResponseBodyAsStream();
InputStreamReader ISR = new InputStreamReader(inStrm);
bufRdr = new BufferedReader(ISR, 3);
while((sTmp = bufRdr.readLine()) != null)
xmlResult = xmlResult + sTmp;
bufRdr.close();
System.out.println("xmlResult = " + xmlResult);
fwPost.releaseConnection();
} catch (Exception e) {
System.out.println("Exception error" + e.getMessage());
return;
}
return;
}
}