WebService 的发布与调用
 
发布:https://wenku.baidu.com/view/2edb9cff941ea76e58fa042c.html
JAVA调用Webservice
RPC 方式,强烈推荐。这种方式不多说,直接看代码就懂了 
- 
public String getOnline(String url){ 
 
- 
- 
		JSONObject resultJson=new JSONObject(); 
 
- 
- 
		Service service = new Service(); 
 
- 
- 
- 
			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) { 
 
- 
- 
			System.out.println("查询在线状态1:"+e.getMessage()); 
 
- 
- 
		} catch (RemoteException e) { 
 
- 
- 
                        System.out.println("查询在线状态2:"+e.getMessage()); 
 
- 
- 
- 
		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){ 
 
- 
- 
- 
			JSONObject resultJson=new JSONObject(); 
 
- 
- 
- 
				wsUrl = new URL(url+"/"+method); 
 
- 
				 HttpURLConnection conn = (HttpURLConnection) wsUrl.openConnection(); 
 
- 
  
- 
- 
- 
			        conn.setRequestMethod("POST"); 
 
- 
			        conn.setRequestProperty("Content-Type", "text/xml;charset=UTF-8"); 
 
- 
			        conn.setConnectTimeout(2000); 
 
- 
			        conn.setReadTimeout(2000); 
 
- 
			        OutputStream os = conn.getOutputStream(); 
 
- 
- 
  
- 
- 
  
- 
			        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]; 
 
- 
- 
- 
			        while((len = is.read(b)) != -1){ 
 
- 
			            String ss = new String(b,0,len,"UTF-8"); 
 
- 
- 
- 
                                 result=s.split("<response xsi:type=\"xsd:string\">")[1].split("</response>")[0]; 
 
- 
  
- 
- 
- 
- 
			} catch (MalformedURLException e) { 
 
- 
- 
				System.out.println("通讯模块1:"+e.getMessage()); 
 
- 
- 
			} catch (IOException e) { 
 
- 
- 
				System.out.println("通讯模块2:"+e.getMessage()); 
 
- 
- 
- 
			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");  
 
- 
  
- 
		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>"; 
 
- 
  
- 
- 
			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) { 
 
- 
- 
- 
		} catch (HttpException e) { 
 
- 
- 
- 
		} catch (IOException e) { 
 
- 
- 
- 
- 
  
- 
      结果:我这儿没有做更多的判断,直接输出,这种方式我以前其实并没有用到。如果有需要可以更具返回的状态判断是否成功。如果你去抓包的话,你会发现这个会和上面HttpURLConnection抓的一样。
 
    
转载至  https://blog.csdn.net/qq_31183297/article/details/79522746
仅供个人学习参考 
学习记录(webservice)
原文:https://www.cnblogs.com/texboom/p/9583049.html