webservice优缺点&调用方式

 

 

 

 

复制代码
import com.gyf.weather.WeatherInterface;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

import java.util.List;

public class Main {

    public static void main(String[] args) {
        System.out.println("Hello World!");

        //使用CXF方式调用
        //1.创建式bean
        JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();

        //2.设置接口
        factoryBean.setServiceClass(WeatherInterface.class);

        //3.设置的地址
        factoryBean.setAddress("http://127.0.0.1:12345/weather?wsdl");

        //4.创建服务
        WeatherInterface ws = factoryBean.create(WeatherInterface.class);

        List<String > cities = ws.getCityNameByProvince("广西");

        System.out.println(ws.queryWeather("广州"));
        System.out.println(cities);
    }
}
复制代码

 

复制代码
import com.gyf.weather.WeatherInterfaceImpl;
import com.gyf.weather.WeatherInterfaceImplService;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class Main2 {

    public static void main(String[] args) throws Exception {

        //发送webservice的网络基于soap网络请求
        /**
         * HttpURLConnection
         */


        //1.创建URL【访问路径】
        URL url = new URL("http://127.0.0.1:12345/weather");

        //2.创建连接
        HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();

        //3.设置请求头
        httpConn.setRequestMethod("POST");
        httpConn.setRequestProperty("content-type","text/xml; charset=utf-8");

        //4.设置请求参数
        httpConn.setDoOutput(true);//可以写数据
        OutputStream os =  httpConn.getOutputStream();
        os.write(paramtersStr("广州").getBytes());

        //5.接收服务器响应数据
        InputStream is = httpConn.getInputStream();
        byte[] buf = new byte[1024];
        int len = 0;
        while ((len = is.read(buf)) != -1){
            //把字节转成字符串
            System.out.println(new String(buf,0,len,"utf-8"));
        }

        //6.关流
        is.close();
        os.close();


    }

    public static String paramtersStr(String cityName){
        return  "<?xml version=\"1.0\" ?>\n" +
                "<S:Envelope xmlns:S=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" +
                "\t<S:Body>\n" +
                "\t\t<ns2:queryWeather xmlns:ns2=\"http://weather.gyf.com/\">\n" +
                "\t\t\t<arg0>"+ cityName+"</arg0>\n" +
                "\t\t</ns2:queryWeather>\n" +
                "\t</S:Body>\n" +
                "</S:Envelope>";
    }
}
复制代码

 

posted @   expworld  阅读(604)  评论(0编辑  收藏  举报
编辑推荐:
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示