java之通过URL类发送SOUP请求详解各参数意义

BufferedReader reader = null;
OutputStream outputStream = null;
InputStreamReader inputStreamReader = null;
// 此路径不用多讲只要能根据此路径获取wsdl文件即可
String wsdl = "http://localhost:8080/webservice/server?wsdl";
int timeout = 10000;
StringBuffer sb = new StringBuffer("");
sb.append("");
sb.append("
        + "xmlns:tns=\"http://service.com/\" "  //此为wsdl文件中schema元素的属性
        + "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/'>");
sb.append("");// 此处也是固定的
sb.append("");// 在wsdl文件中的
sb.append("测试");// 元素也是通过wsdl中的
sb.append("");
sb.append("");
sb.append("");
// HttpURLConnection 发送SOAP请求
System.out.println("HttpURLConnection 发送SOAP请求");
URL url = null;
HttpURLConnection conn = null;
try {
    byte[] requestBytes = sb.toString().getBytes("utf-8");
    url = new URL(wsdl);
    conn = (HttpURLConnection) url.openConnection();
    // 设置请求参数
    conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
    // SOAPAction的地址是命名空间加上 portType元素的name值加上方法operation元素的name值,此处也可以是空字符串
    conn.setRequestProperty("SOAPAction", "http://service.com/SendService/sendOA");
    conn.setRequestProperty("Content-Length", String.valueOf(requestBytes.length));
    conn.setRequestMethod("POST");
    conn.setUseCaches(false);
    // post请求需要开启下面的两个
    conn.setDoInput(true);
    conn.setDoOutput(true);
    conn.setConnectTimeout(timeout);
    conn.setReadTimeout(timeout);
    outputStream = conn.getOutputStream();
    outputStream.write(requestBytes);
    outputStream.flush();
    // 接收响应的两种方式
    // 第一种,通过org.apache.cxf.helpers.IOUtils;工具类,此种不必关闭响应流,底层已经关闭
//            IOUtils.toString(conn.getInputStream(), "utf-8");
    // 第二种,此种就是第一种的底层
    reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
    String line = null;
    // 组织响应结果
    StringBuffer strBuf = new StringBuffer();
    while ((line = reader.readLine()) != null) {
        strBuf.append(line);
    }
    reader.close();
    System.out.println(strBuf.toString());
} catch (Exception e) {
    e.printStackTrace();
} finally {
    try {
        if (outputStream != null) {
            outputStream.close();
        }
        if (reader != null) {
            reader.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

通过地址获取的wsdl文件


<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
                  xmlns:tns="http://service.com/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
                  xmlns:ns1="http://schemas.xmlsoap.org/soap/http" name="SendServiceImplService"
                  targetNamespace="http://service.com/">
    <wsdl:types>
        <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://service.com/"
                   elementFormDefault="unqualified" targetNamespace="http://service.com/" version="1.0">
            <xs:element name="sendOA" type="tns:sendOA"/>
            <xs:element name="sendOAResponse" type="tns:sendOAResponse"/>
            <xs:element name="sendOrg" type="tns:sendOrg"/>
            <xs:element name="sendOrgResponse" type="tns:sendOrgResponse"/>
            <xs:complexType name="sendOrg">
                <xs:sequence>
                    <xs:element minOccurs="0" name="arg0" type="xs:string"/>
                xs:sequence>
            xs:complexType>
            <xs:complexType name="sendOrgResponse">
                <xs:sequence>
                    <xs:element minOccurs="0" name="return" type="xs:string"/>
                xs:sequence>
            xs:complexType>
            <xs:complexType name="sendOA">
                <xs:sequence>
                    <xs:element minOccurs="0" name="arg0" type="xs:string"/>
                xs:sequence>
            xs:complexType>
            <xs:complexType name="sendOAResponse">
                <xs:sequence>
                    <xs:element minOccurs="0" name="return" type="xs:string"/>
                xs:sequence>
            xs:complexType>
        xs:schema>
    wsdl:types>
    <wsdl:message name="sendOrg">
        <wsdl:part element="tns:sendOrg" name="parameters">
        wsdl:part>
    wsdl:message>
    <wsdl:message name="sendOrgResponse">
        <wsdl:part element="tns:sendOrgResponse" name="parameters">
        wsdl:part>
    wsdl:message>
    <wsdl:message name="sendOA">
        <wsdl:part element="tns:sendOA" name="parameters">
        wsdl:part>
    wsdl:message>
    <wsdl:message name="sendOAResponse">
        <wsdl:part element="tns:sendOAResponse" name="parameters">
        wsdl:part>
    wsdl:message>
    <wsdl:portType name="SendService">
        <wsdl:operation name="sendOrg">
            <wsdl:input message="tns:sendOrg" name="sendOrg">
            wsdl:input>
            <wsdl:output message="tns:sendOrgResponse" name="sendOrgResponse">
            wsdl:output>
        wsdl:operation>
        <wsdl:operation name="sendOA">
            <wsdl:input message="tns:sendOA" name="sendOA">
            wsdl:input>
            <wsdl:output message="tns:sendOAResponse" name="sendOAResponse">
            wsdl:output>
        wsdl:operation>
    wsdl:portType>
    <wsdl:binding name="SendServiceImplServiceSoapBinding" type="tns:SendService">
        <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
        <wsdl:operation name="sendOrg">
            <soap:operation soapAction="" style="document"/>
            <wsdl:input name="sendOrg">
                <soap:body use="literal"/>
            wsdl:input>
            <wsdl:output name="sendOrgResponse">
                <soap:body use="literal"/>
            wsdl:output>
        wsdl:operation>
        <wsdl:operation name="sendOA">
            <soap:operation soapAction="" style="document"/>
            <wsdl:input name="sendOA">
                <soap:body use="literal"/>
            wsdl:input>
            <wsdl:output name="sendOAResponse">
                <soap:body use="literal"/>
            wsdl:output>
        wsdl:operation>
    wsdl:binding>
    <wsdl:service name="SendServiceImplService">
        <wsdl:port binding="tns:SendServiceImplServiceSoapBinding" name="SendServiceImplPort">
            <soap:address location="http://localhost:8080/webservice/server"/>
        wsdl:port>
    wsdl:service>
wsdl:definitions>

你可能感兴趣的:(网络通信,webservice)