欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

JAVA提交XML格式参数,java提交xml,提交XML格式的参数很简

来源: javaer 分享于  点击 27213 次 点评:221

JAVA提交XML格式参数,java提交xml,提交XML格式的参数很简


提交XML格式的参数很简单,仅仅是一个提交时候的ContentType问题,下面的例子演示从文件文件中读取XML信息并提交给服务器的过程,该过程可以用来测试Web服务。

import java.io.File;import java.io.FileInputStream;import org.apache.commons.httpclient.HttpClient;import org.apache.commons.httpclient.methods.EntityEnclosingMethod;import org.apache.commons.httpclient.methods.PostMethod;/** * 用来演示提交XML格式数据的例子 */public class PostXMLClient {    public static void main(String[] args) throws Exception {        File input = new File(“test.xml”);        PostMethod post = new PostMethod(“http://localhost:8080/httpclient/xml.jsp”);        // 设置请求的内容直接从文件中读取     post.setRequestBody(new FileInputStream(input));        if (input.length() < Integer.MAX_VALUE)            post.setRequestContentLength(input.length());        else                     post.setRequestContentLength(EntityEnclosingMethod.CONTENT_LENGTH_CHUNKED);        // 指定请求内容的类型     post.setRequestHeader("Content-type", "text/xml; charset=GBK");        HttpClient httpclient = new HttpClient();         int result = httpclient.executeMethod(post);         System.out.println("Response status code: " + result);        System.out.println("Response body: ");        System.out.println(post.getResponseBodyAsString());        post.releaseConnection();    }}//该片段来自于http://byrx.net
相关栏目:

用户点评