加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

webservice调用

发布时间:2020-12-16 21:40:42 所属栏目:安全 来源:网络整理
导读:第一种办法: 下载apache cxf的包,使用wsdl2java工具生成java客户端,未能解决带验证的问题。 第二种方法:根据wsdl文件生成客户端,也没找到解决方法。 最后使用httpclient解决。 public class DynamicHttpclientCall { ? ? private String namespace; ? ?
第一种办法: 下载apache cxf的包,使用wsdl2java工具生成java客户端,未能解决带验证的问题。

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

// ? ? ? ?

// ? ? ??

// ? ? ? ?

// ? ? ? ?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();

? ? }

? ?

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读