zoukankan      html  css  js  c++  java
  • java 通过webservice 地址得到其xml文实现方法

    import java.io.*;
    
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.HttpStatus;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.dom4j.Document;
    import org.dom4j.io.SAXReader;
    import org.xml.sax.InputSource;
    
    
    public class GetxmFromurl {
    
        /**
         * 根据url获得服务器上返回的xml
         * @param url
         * @return
         */
        public static Document getResultInfo(String url){
            HttpClient httpClient = new DefaultHttpClient();
            HttpGet get = new HttpGet(url);
            Document doc = null;
            try{
                HttpResponse response = httpClient.execute(get);
                // 先从响应头得到实体
                HttpEntity entity = response.getEntity();
                int status = response.getStatusLine().getStatusCode();
                if (status == HttpStatus.SC_OK) {
                    // 得到实体输入流
                    InputStream inSm = entity.getContent();
                    BufferedReader br = new BufferedReader(new InputStreamReader(
                            inSm, "UTF-8"));
                    String xmlString = "";
                    for (String temp = br.readLine(); temp != null; xmlString += temp, temp = br
                            .readLine())
                        ;
                    // 去除字符串中的换行符,制表符,回车符。
                    InputStream stream2 = new ByteArrayInputStream(xmlString
                            .getBytes("UTF-8"));
    
                    SAXReader saxReader = new SAXReader(); 
                    saxReader.setEncoding("UTF-8");
                    doc = (Document) saxReader.read(new InputSource(stream2));
                    System.out.println(doc.asXML());
                }
            }catch(Exception e){
                e.printStackTrace();
            }
            return doc;
        }
        public static void main(String[] args) {
            GetxmFromurl.getResultInfo("http://webservice.webxml.com.cn/WebServices/TraditionalSimplifiedWebService.asmx?wsdl");
        }
    }
  • 相关阅读:
    我的大学生涯
    如何设计一个好的Windows 8应用
    [置顶] 十大高明的Google搜索技巧
    [置顶] 走出困境靠自己
    Android代码混淆前后分析
    百度高级搜索
    未来手机什么样 十款未来概念手机图赏
    如何看懂Java混淆后的反编译代码
    最值得一看的几条简单的谷歌 Google 搜索技巧,瞬间提升你的网络搜索能力!
    各种网页尺寸判断方法
  • 原文地址:https://www.cnblogs.com/kunpengit/p/2883389.html
Copyright © 2011-2022 走看看