Post方式调用webservice

本文介绍通过POST方式调用WebService的方法,示例代码展示了如何使用Java实现与WebService的交互,包括设置请求头、构造SOAP消息体及读取响应。

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

Post方式调用webservice

调用的webservice地址:http://127.0.0.1:8080/AmsIFXjjsbt/services/UserGroupCatchServiceImpl?wsdl
方法简介: public int CA_UpdateForSame(String xmlString, String strTrustId)
调用实例见下图

import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
/**
 * Created by Administrator on 2017/4/20.
 */
public class text3 {
    public static void main(String[] args) throws Exception {
        //服务的地址
        URL wsUrl = new URL("http://127.0.0.1:8080/AmsIFXjjsbt/services/UserGroupCatchServiceImpl");
        HttpURLConnection conn = (HttpURLConnection) wsUrl.openConnection();
        conn.setDoInput(true);
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
        OutputStream os = conn.getOutputStream();
        //请求体
        String soap = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:impl=\"http://impl.service.unis.com\">\n" +
                "   <soapenv:Header/>\n" +
                "   <soapenv:Body>\n" +
                "      <impl:CA_UpdateForSame>\n" +
                "         <impl:xmlString>" +
                "&lt;task>\n" +
                "\t&lt;org>\n" +
                "\t\t&lt;oper>del&lt;/oper>\n" +
                "\t\t&lt;id>34000003@gd.zg&lt;/id>\n" +
                "\t\t&lt;pid>34000002@gd.zg&lt;/pid>\n" +
                "\t&lt;/org>\n" +
                "&lt;/task>" +
                "</impl:xmlString>\n" +
                "         <impl:strTrustId>111</impl:strTrustId>\n" +
                "      </impl:CA_UpdateForSame>\n" +
                "   </soapenv:Body>\n" +
                "</soapenv:Envelope>";
        os.write(soap.getBytes());
        InputStream is = conn.getInputStream();
        byte[] b = new byte[1024];
        int len = 0;
        String s = "";
        while((len = is.read(b)) != -1){
            String ss = new String(b,0,len,"UTF-8");
            s += ss;
        }
        System.out.println(s);
        is.close();
        os.close();
        conn.disconnect();
    }
}

在请求体中
参数原始是这样的:

<task>
    <org>
        <oper>del</oper>
        <id>34000003</id>
        <pid>34000002</pid>
    </org>
</task>

但发现这样访问不了webservice,所以将“<”转义为“&lt;*” 可以正常访问,原因不太知道,请知道的大神留言指导

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值