SOAP调用Webservice接口。

java 专栏收录该内容
6 篇文章 0 订阅
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;
    }





}
  • 0
    点赞
  • 0
    评论
  • 1
    收藏
  • 打赏
    打赏
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页

打赏作者

weixin_42190864

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值