java調用webservice接口


1.直接AXIS調用遠程的webservice

注意:不同版本的Axis相差很大,大家最好以apache網站上的例子為准,這里僅僅用於說明其基本用法。

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
public class WebServiceClient {
public static void main(String[] args) {
try {
String endpoint = "http://172.19.0.153:8080/scs-web/webservice/SignService";
// 直接引用遠程的wsdl文件
// 以下都是套路
Service service = new Service();
call.setOperationName("signContract");// WSDL里面描述的接口名稱
Call call = (Call) service.createCall();
call.setTargetEndpointAddress(endpoint);
call.addParameter("channel",org.apache.axis.encoding.XMLType.XSD_DATE,javax.xml.rpc.ParameterMode.IN);// 接口的參數
call.addParameter("templateId",org.apache.axis.encoding.XMLType.XSD_DATE,javax.xml.rpc.ParameterMode.IN);// 接口的參數
call.addParameter("strJson",org.apache.axis.encoding.XMLType.XSD_DATE,javax.xml.rpc.ParameterMode.IN);// 接口的參數
call.addParameter("isSeal",org.apache.axis.encoding.XMLType.XSD_DATE,javax.xml.rpc.ParameterMode.IN);// 接口的參數
call.addParameter("callBackUrl",org.apache.axis.encoding.XMLType.XSD_DATE,javax.xml.rpc.ParameterMode.IN);// 接口的參數
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);// 設置返回類型
String templateId = "3021";
String channel = "1007";
String strJson = "{\"certApplys\":[{\"useridno\":\"N256613448988875\",\"address\":\"山東省德州臨邑縣恆源\",\"engName\":\"test\",\"telno\":\"15869611844\",\"email\":\"test@credithc.com\",\"username\":\"CHENG AI-HSIANG\",\"idTypeCode\":\"Z\"}],\"singnalElement\":{\"a\":\"\",\"b\":\"√\",\"product\":\"利投寶12_B\",\"amount\":\"100,000.00\",\"idType\":\"戶照\",\"year\":2016,\"paybankName\":\"興業銀行\",\"backbankCardNo\":\"622908357966352914\",\"idNo\":\"N213447\",\"month\":12,\"lockTime\":12,\"paybankCardNo\":\"622908357966352914\",\"bigAmount\":\"壹拾萬元整\",\"name\":\"CHENG AI-HSIANG\",\"customerId\":\"C_20161214000158\",\"contractId\":\"L_20161214291739\",\"backbankName\":\"興業銀行\",\"yearIrr\":\"9.6%\",\"payName\":\"CHENG AIHSIANG\",\"day\":14}}\r\n";
String isSeal = "1";
String callBackUrl = "?";

String result = (String) call.invoke(new Object[] {channel,templateId,isSeal,strJson,callBackUrl});
// 給方法傳遞參數,並且調用方法
System.out.println("result is :" + result);
} catch (Exception e) {
System.err.println(e.toString());
}
}
}

2.使用Http方式調用遠程的webservice

package com.webservice;

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

public class WebServiceClient2 {
public static void main(String[] args) {
try {
// 1 指定WebService服務的請求地址:
String wsUrl = "http://172.19.0.153:8080/scs-web/webservice/SignService";

// 2 創建URL:
URL url = new URL(wsUrl);
// 3 建立連接,並將連接強轉為Http連接
URLConnection conn = url.openConnection();
HttpURLConnection con = (HttpURLConnection) conn;

// 4,設置請求方式和請求頭:
con.setDoInput(true); // 是否有入參
con.setDoOutput(true); // 是否有出參
con.setRequestMethod("POST"); // 設置請求方式
con.setRequestProperty("content-type", "text/xml;charset=UTF-8");

// 5,手動構造請求體
String channel = "1007";
String templateId = "3021";
String isSeal = "1";
String strJson = "{\"certApplys\":[{\"useridno\":\"N256613448988875\",\"address\":\"山東省德州臨邑縣恆源\",\"engName\":\"test\",\"telno\":\"15869611844\",\"email\":\"test@credithc.com\",\"username\":\"CHENG AI-HSIANG\",\"idTypeCode\":\"Z\"}],\"singnalElement\":{\"a\":\"\",\"b\":\"√\",\"product\":\"利投寶12_B\",\"amount\":\"100,000.00\",\"idType\":\"戶照\",\"year\":2016,\"paybankName\":\"興業銀行\",\"backbankCardNo\":\"622908357966352914\",\"idNo\":\"N213447\",\"month\":12,\"lockTime\":12,\"paybankCardNo\":\"622908357966352914\",\"bigAmount\":\"壹拾萬元整\",\"name\":\"CHENG AI-HSIANG\",\"customerId\":\"C_20161214000158\",\"contractId\":\"L_20161214291739\",\"backbankName\":\"興業銀行\",\"yearIrr\":\"9.6%\",\"payName\":\"CHENG AIHSIANG\",\"day\":14}}\r\n";
String callBackUrl = "?";

  String  requestBody = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"";
requestBody += " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"";
requestBody += " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">";
requestBody += "<soapenv:Body>";
requestBody += "<q0:signContract xmlns:q0=\"http://impl.webservice.scs.credithc.com/\">";
requestBody += "<channel>" + channel + "</channel>";
requestBody += "<templateId>" + templateId + "</templateId> ";
requestBody += "<isSeal>" + isSeal + "</isSeal> ";
requestBody += "<strJson>" + strJson + "</strJson> ";
requestBody += "<callBackUrl>" + callBackUrl + "</callBackUrl> ";
requestBody += "</q0:signContract>";
requestBody += "</soapenv:Body>";

 

requestBody += "</soapenv:Envelope>";

// 6,通過流的方式將請求體發送出去:
OutputStream out = con.getOutputStream();
out.write(requestBody.getBytes());
out.close();
// 7,服務端返回正常:
int code = con.getResponseCode();
if (code == 200) {// 服務端返回正常
InputStream is = con.getInputStream();
byte[] b = new byte[1024];
StringBuffer sb = new StringBuffer();
int len = 0;
while ((len = is.read(b)) != -1) {
String str = new String(b, 0, len, "UTF-8");
sb.append(str);
}
System.out.println(sb.toString());
    is.close();
}
con.disconnect();
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}https://blog.csdn.net/java_cainiao2016/article/details/80032377

3.springboot 動態調用

package com.credithc.re.sign.webservice;
 
import com.credithc.re.sign.service.RedisService;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.xml.namespace.QName;
import java.util.UUID;
 
@WebService(name = "reSignService")
@Component
public class ReSignService {
 
    @Autowired
    private RedisService redisService;
 
    @WebMethod
    @WebResult
    public String reSign(@WebParam(name = "channel") String channel,
                         @WebParam(name = "templateId") String templateId,
                         @WebParam(name = "isSeal") int isSeal,
                         @WebParam(name = "strJson") String strJson,
                         @WebParam(name = "callBackUrl") String callBackUrl) {
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        redisService.set(uuid, callBackUrl);
        callBackUrl = "http://172.19.0.153:8081/re/sign/callback" + "?id=" + uuid;
        
        // 創建動態客戶端
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient("http://172.19.0.153:8080/scs-web/webservice/SignService?wsdl");
        Object[] objects = new Object[0];
        try {
            QName opName = new QName("http://webservice.scs.credithc.com/", "signContract");
            objects = client.invoke(opName, channel, templateId, isSeal, strJson, callBackUrl);
            System.out.println("返回數據:" + objects[0]);
        } catch (java.lang.Exception e) {
            e.printStackTrace();
        }
        return "請求成功";
    }
}
package com.credithc.re.sign.config;

import com.credithc.re.sign.webservice.ReSignService;
import org.apache.cxf.Bus;
import org.apache.cxf.jaxws.EndpointImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.xml.ws.Endpoint;

@Configuration
public class CxfConfig {

    @Autowired
    private Bus bus;

    @Autowired
    private ReSignService reSignService;

    @Bean
    public Endpoint endpoint(){
        EndpointImpl endpoint = new EndpointImpl(bus,reSignService);
        endpoint.publish("/reSignService");
        return endpoint;
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM