Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Webservice call

Shulou Source: shulou.com Published: 2022-06-03 06:27:21 11月11日 Update

The first method: download apache cxf package, use wsdl2java tool to generate java client, failed to solve the problem with validation.

The second method: generate client according to wsdl file, also did not find a solution.

Finally, use httpclient to solve it.

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;//request timeout

static int connectTimeout = 30000;//Transfer timeout

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

// //Set 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, enter username and password here

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

// //Create HttpClientBuilder

// HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();

//// //Set 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, enter username and password here

// 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("" + patameterMap.get(name)+ "");

// }

return soapRequestData.toString();

}

}

Tags: Utf-8 customer client password method time user username generation method tool file problem transfer authentication Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Redmi Apple Huawei NVidia