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 }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。