学习记录(webservice)

WebService 的发布与调用

发布:https://wenku.baidu.com/view/2edb9cff941ea76e58fa042c.html

JAVA调用Webservice

RPC 方式,强烈推荐。这种方式不多说,直接看代码就懂了

  1. public String getOnline(String url){
  2. int errCode=0;
  3. JSONObject resultJson= new JSONObject();
  4. String result= "";
  5. Service service = new Service();
  6. Call call;
  7. try {
  8. call=(Call) service.createCall();
  9. QName opAddEntry = new QName("urn:demo", "GetOnlineInfo"); //设置命名空间和需要调用的方法名
  10. call.setTargetEndpointAddress(url); //设置请求路径
  11. call.setOperationName( "GetNcgOnlineInfo"); //调用的方法名
  12. call.setTimeout(Integer.valueOf( 2000)); //设置请求超时
  13. call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING); //设置返回类型
  14. result= (String) call.invoke(opAddEntry, new Object[]{});
  15.  
  16. } catch (ServiceException e) {
  17. // TODO Auto-generated catch block
  18. System.out.println( "查询在线状态1:"+e.getMessage());
  19. errCode= 1;
  20. } catch (RemoteException e) {
  21. // TODO Auto-generated catch block
  22.                         System.out.println( "查询在线状态2:"+e.getMessage());
  23. errCode= 2;
  24. }
  25. resultJson.put( "errCode", errCode);
  26. resultJson.put( "data", result);
  27.  
  28. return resultJson.toString();
  29. }

里面注释比较全。还有些别的设置也比较简单,自己琢磨就知道了。例如编码方式、解析时间等。

      说说这种方式的问题吧。我在使用的时候遇到的是:和我对接的人编写了两个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拼接和解析报文进行调用。

    还是上面那个查询设备的方法。只不过改了下。当然,我这是知道报文后的解决办法。

  1. public String ncgConnection(String url,String method){
  2. URL wsUrl;
  3. int errCode=0;
  4. JSONObject resultJson= new JSONObject();
  5. String result= "";
  6. try {
  7. wsUrl = new URL(url+"/"+method);
  8. HttpURLConnection conn = (HttpURLConnection) wsUrl.openConnection();
  9.  
  10. conn.setDoInput( true);
  11. conn.setDoOutput( true);
  12. conn.setRequestMethod( "POST");
  13. conn.setRequestProperty( "Content-Type", "text/xml;charset=UTF-8");
  14. conn.setConnectTimeout( 2000);
  15. conn.setReadTimeout( 2000);
  16. OutputStream os = conn.getOutputStream();
  17. //请求体
  18.  
  19. //<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>
  20.  
  21. String soap = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" "
  22. + "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>";
  23. os.write(soap.getBytes());
  24. InputStream is = conn.getInputStream();
  25.  
  26. byte[] b = new byte[1024];
  27. int len = 0;
  28. String s = "";
  29. while((len = is.read(b)) != -1){
  30. String ss = new String(b,0,len,"UTF-8");
  31. s += ss;
  32. }
  33.                                  result=s.split( "<response xsi:type=\"xsd:string\">")[1].split("</response>")[0];
  34.  
  35. is.close();
  36. os.close();
  37. conn.disconnect();
  38. } catch (MalformedURLException e) {
  39. // TODO Auto-generated catch block
  40. System.out.println( "通讯模块1:"+e.getMessage());
  41. errCode= 1;
  42. } catch (IOException e) {
  43. // TODO Auto-generated catch block
  44. System.out.println( "通讯模块2:"+e.getMessage());
  45. errCode= 2;
  46. }
  47. resultJson.put( "errCode", errCode);
  48. resultJson.put( "data", result);
  49.  
  50. return resultJson.toString();
  51. }

    正常来说,利用HttpURLConnection实现很多的调用不需要自己拼接请求头和解析返回结果的(例如java端提供的一些action或者controller),可是在这儿调用WebService,确确实实的需要自己手写。对比上面那个Wireshark抓包的结果可以发现,在请求体部分按照对方提供的wsdl进行拼接,结果部分也进行相同的解析。可以正确获得结果。

第四种,利用httpclient

    简单来说,httpClient可以算是加强版的HttpURLConnection,httpClient的API比较多,也比较稳定,不容易扩展。HttpURLConnection比较轻量级,容易根据自己的需求进行扩展。但是稳定性不如httpClient。

     这种方法具体实现思路和HttpURLConnection一样。只是有点小区别。代码如下:

  1. public void demo(String url){
  2.  
  3. HttpClient httpClient= new HttpClient();
  4. PostMethod postMethod= new PostMethod();
  5. postMethod.setPath(url+ "/ncg.wsdl"); //路径和wsdl名
  6.  
  7. String soap = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" "
  8. + "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>";
  9.  
  10. try {
  11. byte[] b=soap.getBytes("utf-8");
  12.  
  13. InputStream is = new ByteArrayInputStream(b, 0, b.length);
  14. RequestEntity re = new InputStreamRequestEntity(is, b.length,
  15. "application/soap+xml; charset=utf-8");
  16. postMethod.setRequestEntity(re);
  17. int statusCode = httpClient.executeMethod(postMethod);
  18.  
  19. String soapResponseData = postMethod.getResponseBodyAsString();
  20.  
  21. postMethod.releaseConnection();
  22.                         //解析
  23.                         System.out.println(soapResponseData.split( "<response xsi:type=\"xsd:string\">")[1].split("</response>")[0]);
  24. } catch (UnsupportedEncodingException e1) {
  25. // TODO Auto-generated catch block
  26. e1.printStackTrace();
  27. } catch (HttpException e) {
  28. // TODO Auto-generated catch block
  29. e.printStackTrace();
  30. } catch (IOException e) {
  31. // TODO Auto-generated catch block
  32. e.printStackTrace();
  33. }
  34.  
  35. }

      结果:我这儿没有做更多的判断,直接输出,这种方式我以前其实并没有用到。如果有需要可以更具返回的状态判断是否成功。如果你去抓包的话,你会发现这个会和上面HttpURLConnection抓的一样。

    

转载至  https://blog.csdn.net/qq_31183297/article/details/79522746

仅供个人学习参考

猜你喜欢

转载自www.cnblogs.com/texboom/p/9583049.html
今日推荐