Java中webService的几种调用方法
来源:互联网 发布:vscode的php插件 编辑:程序博客网 时间:2024/06/08 16:46
第一种,HttpClient模拟webService:
首先,导入jar包commons-httpclient-3.1.jar,准备模拟请求的基本数据sb
其次,封装请求request对象
再次,发送请求并接受返回结果
最后,得到的结果是XML形式的字符窜,可以用DoM,或sax技术进行报文解析。
形式1:
1
形式2:
public class test{
public static void main(String[] args) throws Exception {
HttpClient httpclient = new HttpClient();
String url = “http://locahost:9999/ws”;
HttpPost httppost = new HttpPost(url);
//定义json对象,里面放请求报文体
JSONObject json = new JSONObject();
//以下内容根据报文格式决定
json.put("user","zhangsan" );
json.put("pwd","123456" );
json.put("msgtype", "text");//格式
。。。。。
JSONObject detailA = new JSONObject();
detailA.put("title", "xxxxxx”);// 非必须,标题
detailA.put("content", "正文");// 必须,正文
json.put("text", detailA);
。。。。
StringEntity params = new StringEntity(json.toString(), "UTF-8");
httppost.setEntity(params);
HttpResponse response = httpclient.execute(httppost);//获得响应
int code=response.getStatusLine().getStatusCode() //获得响应吗,200为请求成功,作为判断使用
/* 读返回数据 */
String conResult = EntityUtils.toString(response.getEntity());
JSONObject sobj = new JSONObject();
sobj = sobj.fromObject(conResult);
String errmsg = sobj.getString("errmsg");
String errcode = sobj.getString("errcode");
。。。。
以下根据业务需求。。。。。
}
}
第二种,Jax_wbservice标准调用方式:服务器端地址改变,方便改
第三种,通过CXF调用web服务
1、 先通过(*wsimport 生成客户端)
2、把生成的代码拷贝到你的程序中
3、导入CXF的依赖jar包,
4、调用,com.zhj.service包就是通过import生成的文件包,拷贝到项目中。
下面说一说CXF+Spring调用webservice:
- TestClient.java
- package example.client;
- import org.apache.axiom.om.OMAbstractFactory;
- import org.apache.axiom.om.OMElement;
- import org.apache.axiom.om.OMFactory;
- import org.apache.axiom.om.OMNamespace;
- import org.apache.axis2.addressing.EndpointReference;
- import org.apache.axis2.client.Options;
- import org.apache.axis2.client.ServiceClient;
- public class TestClient {
- // targetEPR指定打包的Service(.aar文件)在容器中的物理位置。
- private static EndpointReference targetEPR=new EndpointReference
- ("http://localhost:8080/axis2/services/HelloWorld");
- public static OMElement getSayHelloOMElement(){
- //创建request SOAP包。
- OMFactory fac=OMAbstractFactory.getOMFactory();
- // OMNamespace指定此SOAP文档名称空间。
- OMNamespace omNs=fac.createOMNamespace("http://helloworld.com/","hw");
- //创建元素sayHello,并指定其在omNs指代的名称空间中。
- OMElement method=fac.createOMElement("sayHello",omNs);
- //指定元素的文本内容。
- method.setText("ZJ");
- return method;
- }
- public static void main(String[] args){
- try{
- Options options=new Options();
- options.setTo(targetEPR);
- ServiceClient sender=new ServiceClient();
- sender.setOptions(options);
- OMElement sayHello=TestClient.getSayHelloOMElement();
- //发出request SOAP,
- //同时将得到的远端由sayHello方法返回的信息保存到result。
- //通过services.xml能准确找到sayHello方法所在的文件。
- OMElement result=sender.sendReceive(sayHello);
- }
- catch(Exception axisFault){
- axisFault.printStackTrace();
- }
- }
- }
- Java中webService的几种调用方法
- java 调用webservice的几种方法
- Java调用.NET webservice方法的几种方式
- java调用WebService接口的几种方法
- java调用webservice接口 几种方法
- java调用webservice接口 几种方法
- java调用webservice接口 几种方法
- java调用webservice接口 几种方法
- java调用webservice几种方法
- java调用webservice接口 几种方法
- 几种调用WebService的方法
- 几种调用WebService的方法
- WebService的几种调用方法
- 几种调用WebService的方法
- 几种调用WebService的方法
- 几种调用WebService的方法
- js调用WebService的几种方法
- 几种调用webservice的方法
- PHP基础知识总结02(常用的数组函数)
- OpenGL shader编程
- 表单验证
- 腾讯云直播相关问题处理
- mybatis多表查询报java.lang.IllegalArgumentException: argument type mismatch
- Java中webService的几种调用方法
- Vue表单控件绑定的思考
- java小白何去何从
- 分布式常见问题解决方法论
- 理解SQL SERVER中的分区表
- JVM基础(一):内存空间分配及其回收原理
- 访问修饰符public、private、protected、以及不写(默认)时的区别
- 线程间的互斥量和条件变量
- 工作