You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ol...@apache.org on 2003/02/17 15:26:35 UTC
cvs commit: jakarta-commons/httpclient/src/examples PostXML.java
olegk 2003/02/17 06:26:35
Modified: httpclient/src/examples PostXML.java
Log:
PostXML example refinement
Contributed by Oleg Kalnichevski
Revision Changes Path
1.8 +35 -33 jakarta-commons/httpclient/src/examples/PostXML.java
Index: PostXML.java
===================================================================
RCS file: /home/cvs/jakarta-commons/httpclient/src/examples/PostXML.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- PostXML.java 13 Feb 2003 09:14:43 -0000 1.7
+++ PostXML.java 17 Feb 2003 14:26:35 -0000 1.8
@@ -75,6 +75,7 @@
*
* @author Sean C. Sullivan
* @author Ortwin Gl�ck
+ * @author Oleg Kalnichevski
*/
public class PostXML {
@@ -98,39 +99,40 @@
System.out.println();
System.exit(1);
}
-
+ // Get target URL
String strURL = args[0];
-
- URL u = new URL(strURL);
-
+ // Get file to be posted
String strXMLFilename = args[1];
-
- InputStream input = null;
-
- input = new FileInputStream(strXMLFilename);
-
- PostMethod post = new PostMethod();
-
- post.setRequestBody(input);
-
- if ((u.getPath() == null) || (u.getPath().length() == 0)) {
- post.setPath("/");
- }
- else {
- post.setPath(u.getPath());
+ File input = new File(strXMLFilename);
+ // Prepare HTTP post
+ PostMethod post = new PostMethod(strURL);
+ // Request content will be retrieved directly
+ // from the input stream
+ post.setRequestBody(new FileInputStream(input));
+ // Per default, the request content needs to be buffered
+ // in order to determine its length.
+ // Request body buffering can be avoided when
+ // = content length is explicitly specified
+ // = chunk-encoding is used
+ if (input.length() < Integer.MAX_VALUE) {
+ post.setRequestContentLength((int)input.length());
+ } else {
+ post.setRequestContentLength(EntityEnclosingMethod.CONTENT_LENGTH_CHUNKED);
}
- post.setRequestHeader("Content-type", "text/xml");
-
- HttpClient hc = new HttpClient();
- HostConfiguration cfg = new HostConfiguration();
- cfg.setHost(new URI(u));
-
- int iResultCode = hc.executeMethod(post);
-
- System.out.println("iResultCode = " + iResultCode);
-
- System.out.println("Server response:" + post.getResponseBodyAsString() );
-
+ // Specify content type and encoding
+ // If content encoding is not explicitly specified
+ // ISO-8859-1 is assumed
+ post.setRequestHeader("Content-type", "text/xml; charset=ISO-8859-1");
+ // Get HTTP client
+ HttpClient httpclient = new HttpClient();
+ // Execute request
+ int iResultCode = httpclient.executeMethod(post);
+ // Display status code
+ System.out.println("Response status code: " + iResultCode);
+ // Display response
+ System.out.println("Response body: ");
+ System.out.println(post.getResponseBodyAsString());
+ // Release current connection to the connection pool
post.releaseConnection();
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org