eagle

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

          httpclient 實現java后臺post xml請求

          Posted on 2012-01-30 14:22 月下孤城 閱讀(6090) 評論(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

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


          網站導航:
           
          主站蜘蛛池模板: 勐海县| 综艺| 岳阳市| 滦南县| 泸定县| 法库县| 云安县| 台中市| 无棣县| 景德镇市| 拉孜县| 宁武县| 福海县| 应用必备| 广灵县| 棋牌| 吐鲁番市| 锦屏县| 安岳县| 荣昌县| 罗甸县| 城固县| 海南省| 岳池县| 旅游| 伊宁县| 谢通门县| 乌什县| 河西区| 商南县| 洪江市| 哈尔滨市| 绥棱县| 忻城县| 卢湾区| 镇雄县| 林口县| 湘乡市| 兰西县| 衢州市| 金堂县|