java call webservice two ways of calling ways wsdl interface

About wsdl Interface for me is troublesome basically not sounded. Look ignorant on the Internet search looking very good to me to write this so bad, and very thin blue, I would like this essay commemorating half a month I stepped pit ,,,

  Background: In addition to the general development of just two weeks I received two webservice interfaces, rarely engage in such an interface is very ignorant;

  Development Tools: eclipse, idea

  method one:

    The first is the personal feel that is easy to understand method, as wsdl interface, we can use the development tools into the wsdl interface to a local file, so that you can look at their methods and parameters of the call.

· To eclipse, for example, in your project right choice other

 

 

· Then search for web, select web service client

  

 

· After you enter the path of wsdl, click on it to see the finish

  

 

· This time to write a main method like tune the same way as local enough to call

  

 

· Idea of ​​where the path will generate talk about:

  

  

 

 

· General wsdl call, then more formal specific calls will be written in XXXservicePort.java then write a method call to call like

· Also attach my test:

  ps: Because I have developed here need to add a proxy to the proxy agent plus access (which is also a pit ah !!!)

 // to force the java process plus agency 
        System.getProperties () PUT ( "proxySet", "to true." );
        System.getProperties().put("proxyHost", "172.30.87.202");
        System.getProperties().put("proxyPort", "9999");
        System.out.println("Hello World!");
        TaskService service = new TaskService();
        TaskServicePort port = service.getTaskServicePort();
        B2BParameter para1 = new B2BParameter();
        B2BParameter para2 = new B2BParameter();
        B2BParameter para3 = new B2BParameter();
        B2BParameter para4 = new B2BParameter();
        B2BParameter para5 = new B2BParameter();
        B2BParameter para6 = new B2BParameter();
        para1.setName("loginId");
        para1.setValue("AWP_B2B_CN");
        para2.setName("password");
        para2.setValue("audatex");
        para3.setName("maxCount");
        para3.setValue("100");
        para4.setName("startAtIndex");
        para4.setValue("1");
        para5.setName("fieldsToReturn");
        para5.setValue("ResponsibleUserLoginId,CreatorLoginId,CreationDate,CaseId,TaskId,ClaimNumber,ManufacturerName,ModelName,PlateNumber,VIN,BusinessStatusKind");
        para6.setName("returnPayloadAsXML");
        para6.setXsltParameter(true);
        B2BRequest request = new B2BRequest();
        request.getParameter().add(para1);
        request.getParameter().add(para2);
        request.getParameter().add(para3);
        request.getParameter().add(para4);
        request.getParameter().add(para5);
        request.getParameter().add(para6);
        request.setPayload("CreationDate >\"2019-07-01T11:00:00\" and CreationDate < \"2019-08-08T12:00:00\" ");

        B2BResponse response = port.findTasks(request);
        System.out.println(response.getHostName());
        System.out.println(response.getLoginId());
        System.out.println(response.getPayload());
        System.out.println(response.getReturnCode());

 

 

 

 

 

方法二:

    另外就是用httpClient的方式去调用。下面我将附上我的代码,希望能帮到(pps:有加代理

 
 

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

 
 

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.io.SAXReader;

// 这里引得依赖  包的话需要自己找了



public
static InputStream postXmlRequestInputStream(String requestUrl, String xmlData) throws IOException{ PostMethod postMethod = new PostMethod(requestUrl); byte[] b = xmlData.getBytes("utf-8"); InputStream is = new ByteArrayInputStream(b, 0, b.length); RequestEntity re = new InputStreamRequestEntity(is, b.length, "text/xml;charset=utf-8"); postMethod.setRequestEntity(re); HttpClient httpClient = new HttpClient(); httpClient.getParams().setAuthenticationPreemptive(true); httpClient.getHostConfiguration().setProxy(CommonPptsUtil.get("PROXY_HOST"), Integer.valueOf(CommonPptsUtil.get("PROXY_PORT"))); int statusCode = httpClient.executeMethod(postMethod); logger.debug("responseCode:"+statusCode); if (statusCode != 200) { return null; } return postMethod.getResponseBodyAsStream(); } public static void main(String[] args) { String reqJsonStr = "{\"workId\":\"20171018161622\",\"status\":\"201\",\"startTime\":\"2017-10-18 16:16:22\"}"; String xmlData = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://service.interfacemodule.cic.com/\"><soapenv:Header/><soapenv:Body><ser:statusWriteBack><jsonString>" + "{\"workId\":\"314\",\"orderId\":\"5207675\",\"longitude\":\"104.068310\",\"latitude\":\"30.539503\",\"sendTime\":\"2019-08-13 08:38:45\",\"servicePerName\":\"于学飞\",\"servicePerPhone\":\"18410187680\"}" + "</jsonString></ser:statusWriteBack></soapenv:Body></soapenv:Envelope>"; String url = "http://61.138.246.88:7103/avs/services/CCService?wsdl"; SAXReader reader = new SAXReader(); String result = ""; try { InputStream in = postXmlRequestInputStream(url,xmlData); if(in!=null){ Document doc = reader.read(in); result = doc.getRootElement().element("Body").element("statusWriteBackResponse").element("return").getText(); logger.debug("result:"+result); } } catch (Exception e) { logger.error("error:",e); e.printStackTrace(); } }

 

Guess you like

Origin www.cnblogs.com/yangchengdebokeyuan/p/11352950.html