程序人生
          一個程序只做一件事,但要做好 一個類只提供一套服務,但要完善...........

          因為項目的原因,使用到了HttpClient這個Apache又一個好用的包,這里就簡單的介紹下它的使用吧。

          使用場景

                      因為項目中有一功能是需要和另一個系統進行數據交換的,一開始就考慮采用HttpUrlConnection連接遠程地址,發送流實現數據交換。后來發現了HttpClient,也就賴的再寫一些連接的函數了,所以就“拿來主義”了!

          安裝:

          HttpClient的使用需要三個包,分別是commons-httpclient-3.1.jar、commons-logging-1.0.4.jar、commons-codec-1.3.jar,都可以在Apache的官網上下載。

          使用:

                      我們知道,HTTP協議的連接方法有GET、POST、PUT和HEAD方式,在創建Method實例的時候可以更具具體的方法來創建。HttpClient的使用一般分如下幾步:

                      1、創建HttpClient實例。

                      2、創建具體連接方法的實例。如POST方法創建PostMethod的實例,在實例化時從構造函數中傳入待連接的URL地址。

                      3、對post的發送內容等信息進行配置

                      4、執行HttpClient的execute方法

                      5、如果返回的狀態碼正常,表明連接成功,可以讀取response的內容

           1//將創建的xml生成String類型等待發送 
           2String strxml = CreateXML.creteCustomerXml(list);
           3//創建post發法的實例 
           4        PostMethod post = new PostMethod(
           5"http://127.0.0.1:8088/CustomerReceive");
           6// 使用系統提供的默認的恢復策略 
           7        post.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
           8new DefaultHttpMethodRetryHandler());
           9//設置發送的內容        
          10post.setRequestBody(strxml);
          11//對發送的內容長度進行設定 
          12if (strxml.length() < Integer.MAX_VALUE) {
          13            post.setRequestContentLength(strxml.length());
          14        }

          15else {
          16            post
          17                    .setRequestContentLength(EntityEnclosingMethod.CONTENT_LENGTH_CHUNKED);
          18        }

          19// 指定請求內容的類型 
          20        post.setRequestHeader("Content-type""text/xml; charset=utf-8");
          21//創建HttpClient 實例 
          22        HttpClient httpclient = new HttpClient();
          23int result;
          24try {
          25//執行post方法 
          26            result = httpclient.executeMethod(post);
          27//如果連接正常,獲得返回內容          
          28if (result == HttpStatus.SC_OK) {
          29                InputStream input = post.getResponseBodyAsStream();
          30                SAXReader readers = new SAXReader();
          31try {
          32                    Document document = readers.read(input);
          33                    System.out.println(document.asXML());
          34                }
           catch (DocumentException e) {
          35// TODO Auto-generated catch block 
          36                    e.printStackTrace();
          37                }

          38            }

          39        }
           catch (HttpException e) {
          40// TODO Auto-generated catch block 
          41            e.printStackTrace();
          42        }
           catch (IOException e) {
          43// TODO Auto-generated catch block 
          44            e.printStackTrace();
          45        }
           finally {
          46            post.releaseConnection();
          47        }

          48    }
          49
          posted on 2008-08-14 14:50 劍客阿飛 閱讀(552) 評論(0)  編輯  收藏

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


          網站導航:
           
           
          主站蜘蛛池模板: 漯河市| 常州市| 鹰潭市| 绥棱县| 邵阳县| 鱼台县| 吴旗县| 广宗县| 秀山| 两当县| 镇巴县| 上高县| 英超| 湘乡市| 依安县| 滦平县| 武隆县| 张家口市| 涞水县| 铅山县| 香格里拉县| 巴南区| 北京市| 永康市| 舟山市| 济源市| 米脂县| 山西省| 高碑店市| 广德县| 长葛市| 罗江县| 彩票| 娱乐| 五指山市| 当雄县| 邓州市| 高邮市| 宣武区| 云南省| 扬中市|