zoukankan      html  css  js  c++  java
  • sqlconnection 调用webservice服务

     1 package cn.itcast.service.urlconnection;
     2 
     3 import java.io.InputStream;
     4 import java.io.OutputStream;
     5 import java.net.HttpURLConnection;
     6 import java.net.URL;
     7 import java.net.URLConnection;
     8 
     9 /**
    10  * 通过UrlConnection调用Webservice服务
    11  * 
    12  * @author13  * 
    14  */
    15 public class App {
    16     public static void main(String[] args) throws Exception {
    17         // 指定webservice服务的请求地址
    18         String wsUrl = "http://192.168.1.108:5678/hello";
    19         URL url = new URL(wsUrl);
    20         URLConnection conn = url.openConnection();
    21         HttpURLConnection con = (HttpURLConnection) conn;
    22         // 设置请求方式
    23         con.setDoInput(true);
    24         con.setDoOutput(true);
    25         con.setRequestMethod("POST");
    26         con.setRequestProperty("content-type", "text/xml;charset=UTF-8");
    27 
    28         // 手动构造请求体  请求体通过 httpwatch eclipse tcp/ip工具 还要myeclipse调用wsdl的 查看请求信息 可以获取
    29         String requestBody = "<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" "
    30                 + " xmlns:q0="http://service.itcast.cn/" xmlns:xsd="http://www.w3.org/2001/XMLSchema " "
    31                 + " xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">"
    32                 + "<soapenv:Body><q0:sayHello><arg0>lisi</arg0> <arg1>10</arg1> </q0:sayHello></soapenv:Body></soapenv:Envelope>";
    33         
    34         //获得输出流
    35         OutputStream out = con.getOutputStream();
    36         out.write(requestBody.getBytes());
    37         
    38         out.close();
    39         
    40         int code = con.getResponseCode();
    41         if(code == 200){//服务端返回正常
    42             InputStream is = con.getInputStream();
    43              byte[] b = new byte[1024];
    44              StringBuffer sb = new StringBuffer();
    45              int len = 0;
    46              while((len = is.read(b)) != -1){
    47                  String str = new String(b,0,len,"UTF-8");
    48                  sb.append(str);
    49              }
    50              System.out.println(sb.toString());
    51              is.close();
    52         }
    53         con.disconnect();
    54     }
    55 }
  • 相关阅读:
    MyBatis学习教程
    【转载】Spring MVC 整合 Freemarker
    Java高效编程之四【C语言结构的替代】
    String相关的问题
    接口与抽象类的区别
    Java Garbage Collection基础详解------Java 垃圾回收机制技术详解
    数据库事物、隔离等级及数据库锁机制
    hadoop 多表join:Map side join及Reduce side join范例
    Java IO设计模式彻底分析 (转载)
    傅里叶变换的智慧[转]
  • 原文地址:https://www.cnblogs.com/friends-wf/p/3805606.html
Copyright © 2011-2022 走看看