• 08.HttpUrlconnection方式调用


    package com.rl.client;
    
    import java.io.BufferedInputStream;
    import java.io.BufferedReader;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.io.StringReader;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.net.URLConnection;
    import java.util.Iterator;
    import java.util.List;
    
    import org.dom4j.Document;
    import org.dom4j.Element;
    import org.dom4j.io.SAXReader;
    
    public class TestHttpURLConnection {
    
        
        
          public static void main(String[] args) throws URLException {
            URL url = new URL("http://127.0.0.1:8080/hello?wsdl");
            
            URLConnection uc = url.openConnection();
            
            HttpURLConnection httpuc = (HttpURLConnection) uc;
            
            httpuc.setDoInput(true);
            httpuc.setDoOutput(true);//开启输入输出
            httpuc.setRequestMethod("POST");
            httpuc.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
            String data = "<soapenv:Envelop xmlns:soapenv="http://schemas.xmlsoap.org/soap">"
                    + "<soapenv:Body>"
                    + "<q0:sayBye>"
                    + "<arg0>wangwu</arg0>"
                    + "</q0:sayBye>"
                    + "</soapenv:Body>"
                    + "</soapenv:Envelope>";
            
            OutputStream  out  = httpuc.getOutputStream();
            out.write(data.getBytes());//已经把消息给写过去了.
            
            if(httpuc.getResponseCode()==200){
                InputStream in = httpuc.getInputStream();//通过输入流把消息接收完
                BufferedReader br = new BufferedReader(new InputStreamReader(in));
                //写Socket的那种输入流的方式来读它
                String line  = null;
                StringBuffer sb = new StringBuffer();
                while((line = br.readLine())!=null ){
                    sb.append(line);
                }
                //StringBuffer是一个可变字符串,我们要把它变成一个文档来解析
                //java代码里面解析XML
                SAXReader reader = new SAXReader();
                Document doc = reader.read(new StringReader(sb.toString()));//它的返回值是一个文档对象Document
                List<Element> eList = doc.selectNodes("//return");
                for (Element element : eList) {
                    System.out.println(element.getText());
                }
            }
        }
    }
  • 相关阅读:
    2.8 Classes of Restricted Estimators
    navicat远程登录windows服务器
    面试题
    【南阳OJ分类之语言入门】80题题目+AC代码汇总
    基于‘BOSS直聘招聘信息’分析企业到底需要什么样的PHPer
    数据开源
    Pyhton爬虫实战
    Python爬虫框架Scrapy实战
    做网站用UTF-8编码还是GB2312编码?
    【南阳OJ分类之大数问题】题目+AC代码汇总
  • 原文地址:https://www.cnblogs.com/ZHONGZHENHUA/p/7704376.html
Copyright © 2020-2023  润新知