webservice5j
来源:互联网 发布:java程序设计基础 编辑:程序博客网 时间:2024/06/10 12:09
第四种方式
通过urlConnection连接webservicce
1.服务必须启动
2..
ackage cn.itcast.service.uriconnection;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
public class App2 {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
String wsUrl = "http://localhost:5084/hello";
URL url = new URL(wsUrl);
URLConnection conn = url.openConnection();
HttpURLConnection con = (HttpURLConnection) conn;
// 设置请求方式
con.setDoInput(true);
con.setDoOutput(true);
con.setRequestMethod("POST");
con.setRequestProperty("content-type", "text/xml;charset=UTF-8");
// 手动构造请求体
String requestBody = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" "+
" xmlns:q0=\"http://service.itcast.cn/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" "+
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"> " +
"<soapenv:Body><q0:sayHello><arg0>lisi</arg0></q0:sayHello></soapenv:Body></soapenv:Envelope>";
;
//获得输出流
OutputStream out = con.getOutputStream();
out.write(requestBody.getBytes());
out.close();
int code = con.getResponseCode();
if(code == 200){//服务端返回正常
InputStream is = con.getInputStream();
byte[] b = new byte[1024];
StringBuffer sb = new StringBuffer();
int len = 0;
while((len = is.read(b)) != -1){
String str = new String(b,0,len,"UTF-8");
sb.append(str);
}
System.out.println(sb.toString());
is.close();
}
con.disconnect();
}
}
3.但必须解析xml
- webservice5j
- poj 1654
- 解决百度地图 service 配置 android:process=":remote" 导致application.oncreate创建多次
- Json转对象失败:"No suitable constructor found for type [simple type, class com.test.faster.domain.respons
- 为iOS项目添加Daily Build
- Android性能调优
- webservice5j
- bwareaopen
- git从版本库创建、从远程克隆或本地新建,配置KEY,合并分支到提交到远程版本库等步骤
- LinearAlloc exceeded capacity Problem
- php-search_interpolation
- 【Android】调用系统图库获取图像并裁剪,安卓 4.4可用
- java 关键字volatile的作用
- 通过Itext生成PDF实例-操作超全
- JAVA和C++的主要区别有那些?