SOAP调用Webservice接口。

本文介绍了一个使用Java实现的SOAP接口调用示例,主要目的是发送短信。通过构造SOAP请求并设置HTTP连接参数,该示例展示了如何与远程服务进行交互,包括设置SOAPAction、Content-Type等HTTP头,以及读取响应结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

public class JxSendSmsTest {


    public    String gets(String phone) {
        StringBuilder sb = new StringBuilder();
        sb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>"
                + "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
                + "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" "
                + "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
                + "<soap:Body>    <GetUserFromTelegramAndUpdate xmlns=\"http://tempuri.org/\">"
                + "<phone>" + phone
                + "</phone>    </GetUserFromTelegramAndUpdate>"
                + "</soap:Body></soap:Envelope>");
        return sb.toString();
    }

    public   String sendSms(String phone){
        try {
            String soaps = gets(phone);
            if (soaps == null) {
                return null;
            }
            String urlString = "接口地址";
            String soapActionString = "http://WebXml.com.cn/GetUserFromTelegramAndUpdate";//要调的方法
            URL url = new URL(urlString);
            HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
            httpConn.setRequestProperty("Content-Length", Integer.toString(soaps.length()));
            httpConn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
            httpConn.setRequestProperty("soapActionString", soapActionString);
            httpConn.setRequestMethod("POST");
            httpConn.setDoOutput(true);
            httpConn.setDoInput(true);
            OutputStream os = httpConn.getOutputStream();
            OutputStreamWriter osw = new OutputStreamWriter(os, "utf-8");
            osw.write(soaps);
            osw.close();
            byte[] datas = readInputStream(httpConn.getInputStream());

            String result= new String(datas);
            //打印返回结果
            System.out.println(result);
            return result;
        } catch (Exception e) {
            System.out.println("result:error!");
        }
        return null;
    }



    /**
     * 从输入流中读取数据
     * @param //inStream
     * @return
     * @throws Exception
     */
   public static byte[] readInputStream(InputStream inStream) throws Exception{
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = 0;
        while( (len = inStream.read(buffer)) !=-1 ){
            outStream.write(buffer, 0, len);
        }
        byte[] data = outStream.toByteArray();//网页的二进制数据
        outStream.close();
        inStream.close();
        return data;
    }





}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值