WebService 的发布与调用
发布:https://wenku.baidu.com/view/2edb9cff941ea76e58fa042c.html
JAVA调用Webservice
RPC 方式,强烈推荐。这种方式不多说,直接看代码就懂了
- public String getOnline(String url){
- int errCode=0;
- JSONObject resultJson=new JSONObject();
- String result="";
- Service service = new Service();
- Call call;
- try {
- call=(Call) service.createCall();
- QName opAddEntry = new QName("urn:demo", "GetOnlineInfo"); //设置命名空间和需要调用的方法名
- call.setTargetEndpointAddress(url); //设置请求路径
- call.setOperationName("GetNcgOnlineInfo"); //调用的方法名
- call.setTimeout(Integer.valueOf(2000));//设置请求超时 ???
- call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);//设置返回类型
- result= (String) call.invoke(opAddEntry,new Object[]{});
- } catch (ServiceException e) {
- // TODO Auto-generated catch block
- System.out.println("查询在线状态1:"+e.getMessage());
- errCode=1;
- } catch (RemoteException e) {
- // TODO Auto-generated catch block
- System.out.println("查询在线状态2:"+e.getMessage());
- errCode=2;
- }
- resultJson.put("errCode", errCode);
- ???resultJson.put("data", result);
- return resultJson.toString();
- }
里面注释比较全。还有些别的设置也比较简单,自己琢磨就知道了。例如编码方式、解析时间等。
说说这种方式的问题吧。我在使用的时候遇到的是:和我对接的人编写了两个WebService。但是由于这两个中有许多部分是相同的,他就把这两个合并了,同时提供了两个命名空间(具体怎么操作的我也不清楚),那么问题了,这其中有一个命名空间的所有方法我都能成功调用,但是都无法收到返回值。当时我就方了,开始还是好好的,怎么就突然不行了,于是我继续执行,查看报错消息,同时抓包查看报文内容。终于给我发现了问题。
下图是返回结果报的错,大体意识就是说我设置的命名空间和对方的命名空间不匹配。然后RPC解析就失败了。
然后我利用Wireshark抓包,得到一下结果。可以看看出,我请求的是命名空间是 ns1="urn:ncg"(其余的都是wsdl默认自带的)。可是我收到的返回报文就变了。变成了这样的 xmlns:dag="http://tempuri.org/dag.xsd" xmlns:dag="urn:dag" xmlns:ncg="urn:ncg" 足足有三个啊。RPC按照默认设置的 ns1="urn:ncg" 去解析,那肯定什么都解析不了的。所以只有自己去解析了。这种情况可以利用第三种或者第四种方式进行调用。
第三种:利用HttpURLConnection拼接和解析报文进行调用。
还是上面那个查询设备的方法。只不过改了下。当然,我这是知道报文后的解决办法。
- public String ncgConnection(String url,String method){
- URL wsUrl;
- ???????int errCode=0;
- JSONObject resultJson=new JSONObject();
- String result="";
- try {
- wsUrl = new URL(url+"/"+method);
- HttpURLConnection conn = (HttpURLConnection) wsUrl.openConnection();
- ???????conn.setDoInput(true);
- ???????conn.setDoOutput(true);
- ???????conn.setRequestMethod("POST");
- ???????conn.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
- ???????conn.setConnectTimeout(2000);
- ???????conn.setReadTimeout(2000);
- ???????OutputStream os = conn.getOutputStream();
- ???????//请求体
- ???????//<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soapenv:Body><ns1:DeleteCascadeFromCms soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="urn:ncg"><ncg-code-list xsi:type="xsd:string">["11241525"]</ncg-code-list></ns1:DeleteCascadeFromCms></soapenv:Body></soapenv:Envelope>
- ???????String soap = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" "
- ???????+ "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><soapenv:Body><ns1:"+method+" soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:ns1=\"urn:ncg\"/></soapenv:Body></soapenv:Envelope>";
- ???????os.write(soap.getBytes());
- ???????InputStream is = conn.getInputStream();
- ???????byte[] b = new byte[1024];
- ???????int len = 0;
- ???????String s = "";
- ???????while((len = is.read(b)) != -1){
- ???????????String ss = new String(b,0,len,"UTF-8");
- ???????????s += ss;
- ???????}
- result=s.split("<response xsi:type=\"xsd:string\">")[1].split("</response>")[0];
- ???????is.close();
- ???????os.close();
- ???????conn.disconnect();
- } catch (MalformedURLException e) {
- // TODO Auto-generated catch block
- System.out.println("通讯模块1:"+e.getMessage());
- errCode=1;
- } catch (IOException e) {
- // TODO Auto-generated catch block
- System.out.println("通讯模块2:"+e.getMessage());
- errCode=2;
- }
- resultJson.put("errCode", errCode);
- resultJson.put("data", result);
- ???????return resultJson.toString();
- }
正常来说,利用HttpURLConnection实现很多的调用不需要自己拼接请求头和解析返回结果的(例如java端提供的一些action或者controller),可是在这儿调用WebService,确确实实的需要自己手写。对比上面那个Wireshark抓包的结果可以发现,在请求体部分按照对方提供的wsdl进行拼接,结果部分也进行相同的解析。可以正确获得结果。
第四种,利用httpclient
简单来说,httpClient可以算是加强版的HttpURLConnection,httpClient的API比较多,也比较稳定,不容易扩展。HttpURLConnection比较轻量级,容易根据自己的需求进行扩展。但是稳定性不如httpClient。
这种方法具体实现思路和HttpURLConnection一样。只是有点小区别。代码如下:
- public void demo(String url){
- HttpClient httpClient=new HttpClient();
- PostMethod postMethod=new PostMethod();
- postMethod.setPath(url+"/ncg.wsdl"); //路径和wsdl名
- String soap = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" "
- ???????+ "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><soapenv:Body><ns1:GetNcgOnlineInfo soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:ns1=\"urn:ncg\"/></soapenv:Body></soapenv:Envelope>";
- try {
- byte[] b=soap.getBytes("utf-8");
- InputStream is = new ByteArrayInputStream(b, 0, b.length);
- RequestEntity re = new InputStreamRequestEntity(is, b.length,
- ????????????????"application/soap+xml; charset=utf-8");
- postMethod.setRequestEntity(re);
- int statusCode = httpClient.executeMethod(postMethod);
- String soapResponseData = postMethod.getResponseBodyAsString();
- postMethod.releaseConnection();
- //解析
- System.out.println(soapResponseData.split("<response xsi:type=\"xsd:string\">")[1].split("</response>")[0]);
- } catch (UnsupportedEncodingException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- } catch (HttpException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
结果:我这儿没有做更多的判断,直接输出,这种方式我以前其实并没有用到。如果有需要可以更具返回的状态判断是否成功。如果你去抓包的话,你会发现这个会和上面HttpURLConnection抓的一样。
转载至 https://blog.csdn.net/qq_31183297/article/details/79522746
仅供个人学习参考
学习记录(webservice)
原文地址:https://www.cnblogs.com/texboom/p/9583049.html