webservice調用

第一種辦法:下載apache cxf的包,使用wsdl2java工具生成java客戶端,未能解決帶驗證的問題。

10年積累的成都網站建設、網站設計經驗,可以快速應對客戶對網站的新想法和需求。提供各種問題對應的解決方案。讓選擇我們的客戶得到更好、更有力的網絡服務。我雖然不認識你,你也不認識我。但先網站設計后付款的網站建設流程,更有孟連免費網站建設讓你可以放心的選擇與我們合作。

第二種方法:根據(jù)wsdl文件生成客戶端,也沒找到解決方法。

最后使用httpclient解決。

public class DynamicHttpclientCall {

    private String namespace;

    private String methodName;

    private String wsdlLocation;

    private String soapResponseData;

private String username;

private String password;

private String myWorkStation;

static int socketTimeout = 30000;// 請求超時時間  

   static int connectTimeout = 30000;// 傳輸超時時間 

    public DynamicHttpclientCall(String namespace, String methodName,

        String wsdlLocation,String username,String password,String myWorkStation) {

  

        this.namespace = namespace;

        this.methodName = methodName;

        this.wsdlLocation = wsdlLocation;

        

        this.username = username;

        this.password = password;

        this.myWorkStation = myWorkStation;

    }

    public HttpClientContext createBasicAuthContext(HttpHost host,String username, String password) { 

        CredentialsProvider credsProvider = new BasicCredentialsProvider(); 

        Credentials defaultCreds = new UsernamePasswordCredentials(username, password); 

        credsProvider.setCredentials(new AuthScope(host.getHostName(), host.getPort()), defaultCreds); 

 

        AuthCache authCache = new BasicAuthCache(); 

        BasicScheme basicAuth = new BasicScheme(); 

        authCache.put(host, basicAuth); 

 

        HttpClientContext context = HttpClientContext.create(); 

        context.setCredentialsProvider(credsProvider); 

        context.setAuthCache(authCache); 

        return context; 

    }

    public String invoke(Map<String, String> patameterMap) throws Exception {

    //HttpHost host = new HttpHost("piprd.shandongair.com.cn",50000);

    //HttpClientContext createBasicAuthContext = createBasicAuthContext(host,username,password);

    HttpPost postMethod = new HttpPost(wsdlLocation);

    postMethod.setHeader("Content-type", "application/soap+xml; charset=utf-8");

//    HttpRequest postMethod = new HttpRequest(wsdlLocation);

        String soapRequestData = buildRequestData(patameterMap);

  

//        byte[] bytes = soapRequestData.getBytes("utf-8");

//        InputStream inputStream = new ByteArrayInputStream(bytes, 0,bytes.length);

//        RequestEntity requestEntity = new InputStreamRequestEntity(inputStream, bytes.length, "application/soap+xml; charset=utf-8");

//        postMethod.setRequestEntity(requestEntity);

        postMethod.setEntity(new StringEntity(soapRequestData));

        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();  

//      // 設置BasicAuth  

        CredentialsProvider provider = new BasicCredentialsProvider();  

//      // Create the authentication scope  

        AuthScope scope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM);  

//      // Create credential pair,在此處填寫用戶名和密碼  

        UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password);  

//      // Inject the credentials  

        provider.setCredentials(scope, credentials);  

//      // Set the default credentials provider  

        httpClientBuilder.setDefaultCredentialsProvider(provider);  

//      // HttpClient  

        CloseableHttpClient closeableHttpClient = httpClientBuilder.build();  

        

//        httpClient.getParams().setParameter(HttpProtocolParams.HTTP_CONTENT_CHARSET, "utf-8");

//        NTCredentials creds = new NTCredentials(username,password,myWorkStation,"");

        CloseableHttpResponse execute = closeableHttpClient.execute(postMethod);

        int statusCode = execute.getStatusLine().getStatusCode();

        String text = IOUtils.toString(execute.getEntity().getContent(), "utf-8");

        return text;

    }

//    public int invoke(Map<String, String> patameterMap) throws Exception {

//        PostMethod postMethod = new PostMethod(wsdlLocation);

//        String soapRequestData = buildRequestData(patameterMap);

//  

//        byte[] bytes = soapRequestData.getBytes("utf-8");

//        InputStream inputStream = new ByteArrayInputStream(bytes, 0,bytes.length);

//        RequestEntity requestEntity = new InputStreamRequestEntity(inputStream, bytes.length, "application/soap+xml; charset=utf-8");

//        postMethod.setRequestEntity(requestEntity);

//  

////        HttpClient httpClient = new HttpClient();

//        // 創(chuàng)建HttpClientBuilder  

//        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();  

////        // 設置BasicAuth  

//        CredentialsProvider provider = new BasicCredentialsProvider();  

////        // Create the authentication scope  

//        AuthScope scope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM);  

////        // Create credential pair,在此處填寫用戶名和密碼  

//        UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password);  

////        // Inject the credentials  

//        provider.setCredentials(scope, credentials);  

////        // Set the default credentials provider  

//        httpClientBuilder.setDefaultCredentialsProvider(provider);  

////        // HttpClient  

//        CloseableHttpClient closeableHttpClient = httpClientBuilder.build();  

//        

////        httpClient.getParams().setParameter(HttpProtocolParams.HTTP_CONTENT_CHARSET, "utf-8");

////        NTCredentials creds = new NTCredentials(username,password,myWorkStation,"");

//        

//       

//        

//        int statusCode = httpClient.executeMethod(postMethod);

//        soapResponseData = postMethod.getResponseBodyAsString();

//        return statusCode;

//    }

 

    private String buildRequestData(Map<String, String> patameterMap) {

        StringBuffer soapRequestData = new StringBuffer();

        soapRequestData.append("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:com=\"com:sda:querydata\"><soapenv:Header/>");

        soapRequestData.append("<soapenv:Body><com:MT_DD_Request><MSGRP></MSGRP></com:MT_DD_Request></soapenv:Body></soapenv:Envelope>");

//        Set<String> nameSet = patameterMap.keySet();

//        for (String name : nameSet) {

//            soapRequestData.append("<" + name + ">" + patameterMap.get(name)+ "</" + name + ">");

//        }

        return soapRequestData.toString();

    }

   

}


當前名稱:webservice調用
文章來源:http://muchs.cn/article40/jcppeo.html

成都網站建設公司_創(chuàng)新互聯(lián),為您提供標簽優(yōu)化、虛擬主機、網站設計、App開發(fā)網站營銷、云服務器

廣告

聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

成都網站建設