网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

webservice调用

第一种办法:下载apache cxf的包,使用wsdl2java工具生成java客户端,未能解决带验证的问题。

10年积累的成都网站建设、网站设计经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站设计后付款的网站建设流程,更有孟连免费网站建设让你可以放心的选择与我们合作。

第二种方法:根据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 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 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();

//        // 创建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 patameterMap) {

        StringBuffer soapRequestData = new StringBuffer();

        soapRequestData.append("");

        soapRequestData.append("");

//        Set nameSet = patameterMap.keySet();

//        for (String name : nameSet) {

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

//        }

        return soapRequestData.toString();

    }

   

}



新闻名称:webservice调用
文章起源:http://bjjierui.cn/article/jcppeo.html

其他资讯