eagle

          學(xué)無止境,細節(jié)決定成敗.
          posts - 12, comments - 11, trackbacks - 0, articles - 2
            BlogJava :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理
          apache的httpClient工具包能實現(xiàn)http相關(guān)請求。以下是一個java后臺包裝xml參數(shù)請求服務(wù)的工具類。
          /**
               * Post Http request with XML parameter
               * 
          @param requestUrl 
               * 
          @param xmlData    request xml parameter
               * 
          @param contentType such as "application/xml"
               * 
          @param charset such as "UTF-8" or "GBK"
               * 
          @return
               
          */

              
          public static HttpRespondResult postXmlRequest(String requestUrl, String xmlData, String contentType, String charset){
                  HttpRespondResult revObj 
          = new HttpRespondResult();
                  
          //init PostMethod object.
                  PostMethod post = new PostMethod(requestUrl);
                  
                  
          try {
                      
          //wrape the request entity.
                      RequestEntity requestEntity = new StringRequestEntity(xmlData, contentType, charset);
                      post.setRequestEntity(requestEntity);
                      HttpClient httpClient 
          = new HttpClient();
                      
          // send the post http request and reture status code.
                      int statusCode = httpClient.executeMethod(post);
                      
          // get reture content from server side.
                      String bodyContent = post.getResponseBodyAsString();
                      
                      
          // populate the reture values to vo.
                      revObj.setStatusCode(statusCode);
                      revObj.setRespondbodyContent(bodyContent);
                      
                  }
           catch (UnsupportedEncodingException e) {
                      revObj.handleExceptionMsg(e);
                      e.printStackTrace();
                  }
          catch (HttpException e) {
                      revObj.handleExceptionMsg(e);
                      e.printStackTrace();
                  }
           catch (IOException e) {
                      revObj.handleExceptionMsg(e);
                      e.printStackTrace();
                  }
           catch (Exception e){
                      revObj.handleExceptionMsg(e);
                      e.printStackTrace();
                  }
          finally {
                      
          //close the connection.
                      post.releaseConnection();
                  }

                  
                  
                  
          return revObj;
              }







          ---------------------
          月下孤城
          mail:eagle_daiqiang@sina.com

          評論

          # re: httpclient 實現(xiàn)java后臺post xml請求  回復(fù)  更多評論   

          2014-05-05 18:25 by zuidaima
          請參考代碼:java apache commons HttpClient發(fā)送get和post請求的學(xué)習(xí)整理,下載地址:http://www.zuidaima.com/share/1754065983409152.htm

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 陇川县| 四川省| 大邑县| 邳州市| 湖北省| 普定县| 林周县| 昌乐县| 怀宁县| 武山县| 利津县| 孟州市| 西峡县| 崇礼县| 图们市| 富裕县| 阿勒泰市| 屯昌县| 土默特右旗| 台中市| 湟中县| 福贡县| 泸定县| 会同县| 西丰县| 屏边| 西充县| 西乌珠穆沁旗| 冀州市| 巴塘县| 房山区| 内黄县| 砚山县| 定襄县| 蓝山县| 惠东县| 淳安县| 芜湖市| 福安市| 济南市| 马山县|