eagle

          學無止境,細節決定成敗.
          posts - 12, comments - 11, trackbacks - 0, articles - 2
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          httpclient 實現java后臺post xml請求

          Posted on 2012-01-30 14:22 月下孤城 閱讀(6100) 評論(1)  編輯  收藏
          apache的httpClient工具包能實現http相關請求。以下是一個java后臺包裝xml參數請求服務的工具類。
          /**
               * 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 實現java后臺post xml請求  回復  更多評論   

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

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


          網站導航:
           
          主站蜘蛛池模板: 沙河市| 昔阳县| 县级市| 平舆县| 通海县| 深圳市| 高平市| 万宁市| 乐山市| 张家港市| 达州市| 昌都县| 祁连县| 新宾| 门源| 阿城市| 株洲市| 玉山县| 巴彦县| 门头沟区| 中宁县| 宣汉县| 南平市| 博客| 峨眉山市| 岑溪市| 辽宁省| 怀安县| 兴文县| 巨鹿县| 达日县| 西丰县| 马龙县| 阿拉善左旗| 精河县| 高碑店市| 巴彦淖尔市| 通道| 元江| 专栏| 保亭|