1. 程式人生 > >Java 呼叫webservice介面

Java 呼叫webservice介面

方法一:

//url為wsdl路徑
public static synchronized boolean sendMsgWebservice(String url,String content,String addresseeTel,String userAccount,String password,String appCode ) {
        String sendTime=DateUtil.dateFormat(new Date(),"yyyy-MM-dd HH:mm:ss").replace(" ","T");
        // 定義httpClient的例項
        CloseableHttpClient httpclient = HttpClients.createDefault();
        boolean flag=false;
        CloseableHttpResponse response2=null;
        try{
            HttpPost httpPost = new HttpPost(url);
            String wsdlData = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                    "<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">\n" +
                    "<soap12:Body>\n" +
                    "<SendSms xmlns=\"http://tempuri.org/\">\n" +
                    "<content>"+content+"</content>\n" +
                    "<addresseeTel>"+addresseeTel+"</addresseeTel>\n" +
                    "<sendTime>"+sendTime+"</sendTime>\n" +
                    "<userAccount>"+userAccount+"</userAccount>\n" +
                    "<password>"+password+"</password>\n" +
                    "<appCode>"+appCode+"</appCode>\n" +
                    "</SendSms>\n" +
                    "</soap12:Body>\n" +
                    "</soap12:Envelope>";
            StringEntity myEntity = new StringEntity(wsdlData,ContentType.create("text/xml", "UTF-8"));
            httpPost.setEntity(myEntity);
            response2 = httpclient.execute(httpPost);
            HttpEntity entity2 = response2.getEntity();
            String retVal=null;
            if (entity2 != null) {
                retVal = EntityUtils.toString(entity2, "UTF-8");
            }
            EntityUtils.consume(entity2);
            if(retVal!=null && retVal.contains("傳送成功")){
                flag=true;
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                if(response2!=null){
                    response2.close();
                }
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return flag;
    }

wsdl 資料格式

POST /SmpWebService/SmpWebService.asmx HTTP/1.1 
Host: localhost 
Content-Type: text/xml; charset=utf-8 
Content-Length: length
SOAPAction: "http://beyondbit.com/smp/SendSms" 
​
//引數定義如下:
<?xml version="1.0" encoding="utf-8"?> 
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"> 
  <soap12:Body> 
    <SendSms xmlns="http://beyondbit.com/smp"> 
        <content>string</content> 
        <addresseeTel>string</addresseeTel> 
        <sendTime>dateTime</sendTime>
        <userAccount>string</userAccount> 
        <password>string</password> 
        <appCode>string</appCode> 
    </SendSms> 
  </soap12:Body> 
</soap12:Envelope> 
​
//執行結果定義:
<?xml version="1.0" encoding="utf-8"?> 
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
    <soap12:Body> 
        <SendSmsResponse xmlns="http://beyondbit.com/smp"> 
            <SendSmsResult>string</SendSmsResult> 
        </SendSmsResponse> 
    </soap12:Body> 
</soap12:Envelope>