当前位置:编程学习 > XML/UML >>

对于java用发送http请求,请求内容为xml格式

 
import java.io.BufferedInputStream; 
import java.io.BufferedReader; 
import java.io.ByteArrayOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.io.OutputStreamWriter; 
import java.net.URI; 
import java.net.URL; 
import java.net.URLConnection; 
 
import org.apache.commons.httpclient.HttpClient; 
import org.apache.commons.httpclient.HttpStatus; 
import org.apache.commons.httpclient.methods.PostMethod; 
 
/**
 * 测试调用一些meeting第三方接口
 * @author Jack.Song
 */ 
public class TestMeetingInte易做图ce { 
 
    /**
     * @param args
     */ 
    public static void main(String[] args) { 
         
        String url = "http://192.168.0.68/integration/xml"; 
        TestMeetingInte易做图ce tmi = new TestMeetingInte易做图ce(); 
        System.out.println(tmi.post(url,"listSummaryMeeting.xml")); 
         
        /*//判断当前系统是否支持Java AWT Desktop扩展
        if(java.awt.Desktop.isDesktopSupported()){
            try {
                URI path = tmi.getClass().getResource("/listSummaryMeeting.xml").toURI();
                System.out.println(path);
                //创建一个URI实例
//              java.net.URI uri = java.net.URI.create(path); 
                //获取当前系统桌面扩展
                java.awt.Desktop dp = java.awt.Desktop.getDesktop();
                //判断系统桌面是否支持要执行的功能
                if(dp.isSupported(java.awt.Desktop.Action.BROWSE)){
                    //获取系统默认浏览器打开链接
                    dp.browse(path);    
                }
            } catch (Exception e) {
                e.printStackTrace();
            }             
        }*/ 
    } 
     
     
 
    /** 
     * 发送xml数据请求到server端 
     * @param url xml请求数据地址 
     * @param xmlString 发送的xml数据流 
     * @return null发送失败,否则返回响应内容 
     */   
    public String post(String url,String xmlFileName){   
        //关闭  
        System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog");    
        System.setProperty("org.apache.commons.logging.易做图log.showdatetime", "true");    
        System.setProperty("org.apache.commons.logging.易做图log.log.org.apache.commons.httpclient", "stdout");   
         
        //创建httpclient工具对象  
        HttpClient client = new HttpClient();   
        //创建post请求方法  
        PostMethod myPost = new PostMethod(url);   
        //设置请求超时时间  
        client.setConnectionTimeout(300*1000); 
        String responseString = null;   
        try{   
            //设置请求头部类型  
            myPost.setRequestHeader("Content-Type","text/xml"); 
            myPost.setRequestHeader("charset","utf-8"); 
             
            //设置请求体,即xml文本内容,注:这里写了两种方式,一种是直接获取xml内容字符串,一种是读取xml文件以流的形式  
//          myPost.setRequestBody(xmlString);  
             
            InputStream body=this.getClass().getResourceAsStream("/"+xmlFileName); 
            myPost.setRequestBody(body); 
//            myPost.setRequestEntity(new StringRequestEntity(xmlString,"text/xml","utf-8"));    
            int statusCode = client.executeMethod(myPost);   
            if(statusCode == HttpStatus.SC_OK){   
                BufferedInputStream bis = new BufferedInputStream(myPost.getResponseBodyAsStream());   
                byte[] bytes = new byte[1024];   
                ByteArrayOutputStream bos = new ByteArrayOutputStream();   
                int count = 0;   
          &nbs
补充:软件开发 , Java ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,