<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    pzxsheng

    有種相見(jiàn)不敢見(jiàn)的傷痛,有種愛(ài)還埋藏在心中

    Java Webservice調(diào)用總結(jié)

    一、調(diào)用ASP.NET發(fā)布的WebService服務(wù)
    以下是SOAP1.2請(qǐng)求事例
        POST /user/yfengine.asmx HTTP/1.1
        Host: oserver.palm-la.com
        Content-Type: application/soap+xml; charset=utf-8
        Content-Length: length
        <?xml version="1.0" encoding="utf-8"?>
        <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
              <soap12:Body>
                <Login xmlns="Loginnames">
                      <userId>string</userId>
                      <password>string</password>
                </Login>
              </soap12:Body>
        </soap12:Envelope>
    1、方式一:通過(guò)AXIS調(diào)用
    String serviceEpr = "http://127.0.0.1/rightproject/WebServices/RightService.asmx";
    public String callWebServiceByAixs(String userId, String password, String serviceEpr){
                  
            try {
                   Service service = new Service();
                   Call call = (Call)service.createCall();
                   call.setTargetEndpointAddress(new java.net.URL(serviceEpr));
                   //服務(wù)名
                   call.setOperationName(new QName("http://tempuri.org/""Login"));   
                   //定義入口參數(shù)和參數(shù)類型
                   call.addParameter(new QName("http://tempuri.org/""userId"),XMLType.XSD_STRING, ParameterMode.IN);
                   call.addParameter(new QName("http://tempuri.org/""password"),XMLType.XSD_STRING, ParameterMode.IN);
                   call.setUseSOAPAction(true);
                   //Action地址
                   call.setSOAPActionURI("http://tempuri.org/Login");
                   //定義返回值類型
                   call.setReturnType(XMLType.XSD_INT);
                   
                   //調(diào)用服務(wù)獲取返回值    
                   String result = String.valueOf(call.invoke(new Object[]{userId, password}));
                   System.out.println("返回值 : " + result);
                   return result;
                  } catch (ServiceException e) {
                   e.printStackTrace();
                  } catch (RemoteException e) {
                   e.printStackTrace();
                  } catch (MalformedURLException e) {
                   e.printStackTrace();
                  }
            
            return null;
        }
    2、方式二: 通過(guò)HttpClient調(diào)用webservice
    soapRequest 為以下Xml,將請(qǐng)求的入口參數(shù)輸入
    <?xml version="1.0" encoding="utf-8"?>
            <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
              <soap12:Body>
                <Login xmlns="Loginnames">
                      <userId>張氏</userId>
                      <password>123456</password>
                </Login>
              </soap12:Body>
        </soap12:Envelope>
    String serviceEpr = "http://127.0.0.1/rightproject/WebServices/RightService.asmx";
    String contentType = "application/soap+xml; charset=utf-8";
    public static String callWebService(String soapRequest, String serviceEpr, String contentType){
            
            PostMethod postMethod = new PostMethod(serviceEpr);
            //設(shè)置POST方法請(qǐng)求超時(shí)
            postMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 5000);
            
            try {
                
                byte[] b = soapRequest.getBytes("utf-8");
                InputStream inputStream = new ByteArrayInputStream(b, 0, b.length);
                RequestEntity re = new InputStreamRequestEntity(inputStream, b.length, contentType);
                postMethod.setRequestEntity(re);
                
                HttpClient httpClient = new HttpClient();
                HttpConnectionManagerParams managerParams = httpClient.getHttpConnectionManager().getParams(); 
                // 設(shè)置連接超時(shí)時(shí)間(單位毫秒)
                managerParams.setConnectionTimeout(30000);
                // 設(shè)置讀數(shù)據(jù)超時(shí)時(shí)間(單位毫秒)
                managerParams.setSoTimeout(600000); 
                int statusCode = httpClient.executeMethod(postMethod);
                if (statusCode != HttpStatus.SC_OK)  
                    throw new IllegalStateException("調(diào)用webservice錯(cuò)誤 : " + postMethod.getStatusLine()); 
                
                String soapRequestData =  postMethod.getResponseBodyAsString();
                inputStream.close();
                return soapRequestData;
            } catch (UnsupportedEncodingException e) {
                return "errorMessage : " + e.getMessage();
            } catch (HttpException e) {
                return "errorMessage : " + e.getMessage();
            } catch (IOException e) {
                return "errorMessage : " + e.getMessage();
            }finally{
                 postMethod.releaseConnection(); 
            }
        }

    二、調(diào)用其他WebService服務(wù)
    1、方式一:通過(guò)AIXS2調(diào)用
    serviceEpr:服務(wù)地址
    nameSpace:服務(wù)命名空間
    methodName:服務(wù)名稱
    Object[] args = new Object[]{"請(qǐng)求的數(shù)據(jù)"};
    DataHandler dataHandler = new DataHandler(new FileDataSource("文件路徑"));
    傳文件的話,"請(qǐng)求的數(shù)據(jù)"可以用DataHandler對(duì)象,但是WebService服務(wù)需提供相應(yīng)的處理即:
    InputStream inputStream = DataHandler.getInputStream();
    然后將inputStream寫入文件即可。還可以將文件讀取為二進(jìn)制流進(jìn)行傳遞。
    public static String callWebService(String serviceEpr, String nameSpace, Object[] args, String methodName){
            try{
            
                RPCServiceClient serviceClient = new RPCServiceClient();
                Options options = serviceClient.getOptions();
                EndpointReference targetEPR = new EndpointReference(serviceEpr);
                options.setTo(targetEPR);
                
                //===========可以解決多次調(diào)用webservice后的連接超時(shí)異常========
                options.setManageSession(true);   
                options.setProperty(HTTPConstants.REUSE_HTTP_CLIENT,true);   
               
                //設(shè)置超時(shí)
                options.setTimeOutInMilliSeconds(60000L);
                // 設(shè)定操作的名稱
                QName opQName = new QName(nameSpace, methodName);
                // 設(shè)定返回值
                
    // 操作需要傳入的參數(shù)已經(jīng)在參數(shù)中給定,這里直接傳入方法中調(diào)用
                Class[] opReturnType = new Class[] { String[].class };
                //請(qǐng)求并得到返回值
                Object[] response = serviceClient.invokeBlocking(opQName, args, opReturnType);
                String sResult = ((String[]) response[0])[0];
                //==========可以解決多次調(diào)用webservice后的連接超時(shí)異常=======
                serviceClient.cleanupTransport(); 
                return sResult;
            
            }catch(AxisFault af){
                return af.getMessage();
            }
        }
        
    2、方式二:
    serviceEpr:服務(wù)器地址
    nameSpace:服務(wù)命名空間
    methodName:服務(wù)名稱
    private static void callWebService(String serviceEpr, String nameSpace, String methodName) {
            try {
                EndpointReference endpointReference = new EndpointReference(serviceEpr);
                // 創(chuàng)建一個(gè)OMFactory,下面的namespace、方法與參數(shù)均需由它創(chuàng)建
                OMFactory factory = OMAbstractFactory.getOMFactory();
                // 創(chuàng)建命名空間
                OMNamespace namespace = factory.createOMNamespace(nameSpace, "urn");
                // 參數(shù)對(duì)數(shù)
                OMElement nameElement = factory.createOMElement("arg0"null);
                nameElement.addChild(factory.createOMText(nameElement, "北京"));
                // 創(chuàng)建一個(gè)method對(duì)象
                OMElement method = factory.createOMElement(methodName, namespace);
                method.addChild(nameElement);
                Options options = new Options();
                // SOAPACTION
                
    //options.setAction("sayHi");
                options.setTo(endpointReference);
                
                options.setSoapVersionURI(org.apache.axiom.soap.SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI); 
                ServiceClient sender = new ServiceClient();
                sender.setOptions(options);
                // 請(qǐng)求并得到結(jié)果
                OMElement result = sender.sendReceive(method);
                System.out.println(result.toString());
            } catch (AxisFault ex) {
                ex.printStackTrace();
            }
        }
        
    3、方式三:通過(guò)CXF調(diào)用
    serviceEpr:服務(wù)器地址
    nameSpace:服務(wù)命名空間
    methodName:服務(wù)名稱
    public static String callWebService(String serviceEpr, String nameSpace, String methodName){
            
            JaxWsDynamicClientFactory clientFactory = JaxWsDynamicClientFactory.newInstance();
            Client client = clientFactory.createClient(serviceEpr);
            Object[] resp = client.invoke(methodName, new Object[]{"請(qǐng)求的內(nèi)容"});
            System.out.println(resp[0]);
        }
        
        //傳文件,將文件讀取為二進(jìn)制流進(jìn)行傳遞,“請(qǐng)求內(nèi)容”則為二進(jìn)制流
        private byte[] getContent(String filePath) throws IOException{  
            
         FileInputStream inputStream = new FileInputStream(filePath);    
         ByteArrayOutputStream outputStream = new ByteArrayOutputStream(1024);  
            System.out.println("bytes available: " + inputStream.available());  
      
            byte[] b = new byte[1024];        
            int size = 0;  
              
            while((size = inputStream.read(b)) != -1)   
                outputStream.write(b, 0, size);   
              
         inputStream.close();   
            byte[] bytes = outputStream.toByteArray();  
            outputStream.close();
     

            return bytes;  
        } 
        

    posted on 2013-02-19 16:58 科菱財(cái)神 閱讀(18204) 評(píng)論(0)  編輯  收藏 所屬分類: Webservice

    導(dǎo)航

    <2013年2月>
    272829303112
    3456789
    10111213141516
    17181920212223
    242526272812
    3456789

    統(tǒng)計(jì)

    常用鏈接

    留言簿(1)

    隨筆分類

    隨筆檔案

    搜索

    最新評(píng)論

    閱讀排行榜

    評(píng)論排行榜

    主站蜘蛛池模板: 久久久久久A亚洲欧洲AV冫| 在线观看AV片永久免费| 亚洲午夜日韩高清一区| 99视频免费在线观看| 在线亚洲人成电影网站色www| 永久在线观看免费视频| 亚洲福利视频网址| 国产精品视频永久免费播放| 国产99久久亚洲综合精品 | 亚洲综合久久综合激情久久| 95免费观看体验区视频| 亚洲伊人久久大香线蕉啊| 在线观看成人免费视频| 亚洲爆乳少妇无码激情| 亚洲国产精品成人久久蜜臀| 在线观看特色大片免费网站| 五月天婷婷精品免费视频| 亚洲日本一区二区| 日韩a级毛片免费视频| 国产日韩AV免费无码一区二区三区| 久久精品国产亚洲夜色AV网站| 1024免费福利永久观看网站| 偷自拍亚洲视频在线观看99| 亚洲欧洲无卡二区视頻| 亚洲av无码成人黄网站在线观看| 亚洲区小说区图片区QVOD| 妞干网手机免费视频| 青娱分类视频精品免费2| 特级毛片在线大全免费播放| 黄色三级三级三级免费看| 亚洲美女激情视频| 亚洲精品久久久www| 日韩精品无码区免费专区| 久草视频免费在线观看| 在线看片v免费观看视频777| 永久免费在线观看视频| a级在线观看免费| 一级毛片aaaaaa免费看| 久久亚洲色WWW成人欧美| 亚洲最大在线视频| 亚洲欭美日韩颜射在线二|