eagle

          學(xué)無(wú)止境,細(xì)節(jié)決定成敗.
          posts - 12, comments - 11, trackbacks - 0, articles - 2
            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理
          apache的httpClient工具包能實(shí)現(xiàn)http相關(guān)請(qǐng)求。以下是一個(gè)java后臺(tái)包裝xml參數(shù)請(qǐng)求服務(wù)的工具類(lèi)。
          /**
               * 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

          評(píng)論

          # re: httpclient 實(shí)現(xiàn)java后臺(tái)post xml請(qǐng)求  回復(fù)  更多評(píng)論   

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

          只有注冊(cè)用戶(hù)登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 武冈市| 兰州市| 余姚市| 承德市| 正蓝旗| 登封市| 抚松县| 全椒县| 万盛区| 北辰区| 永清县| 泾阳县| 广汉市| 锡林郭勒盟| 澄江县| 横峰县| 海晏县| 崇礼县| 德阳市| 衡阳县| 长子县| 秭归县| 介休市| 辛集市| 达尔| 兴安县| 林西县| 色达县| 沈阳市| 扎囊县| 佛冈县| 绩溪县| 丰镇市| 安多县| 沈丘县| 鄄城县| 皮山县| 张家港市| 玉树县| 呼玛县| 沂南县|