程序人生
          一個(gè)程序只做一件事,但要做好 一個(gè)類只提供一套服務(wù),但要完善...........

          2008年8月14日

          因?yàn)轫?xiàng)目的原因,使用到了HttpClient這個(gè)Apache又一個(gè)好用的包,這里就簡(jiǎn)單的介紹下它的使用吧。

          使用場(chǎng)景

                      因?yàn)轫?xiàng)目中有一功能是需要和另一個(gè)系統(tǒng)進(jìn)行數(shù)據(jù)交換的,一開(kāi)始就考慮采用HttpUrlConnection連接遠(yuǎn)程地址,發(fā)送流實(shí)現(xiàn)數(shù)據(jù)交換。后來(lái)發(fā)現(xiàn)了HttpClient,也就賴的再寫一些連接的函數(shù)了,所以就“拿來(lái)主義”了!

          安裝:

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

          使用:

                      我們知道,HTTP協(xié)議的連接方法有GET、POST、PUT和HEAD方式,在創(chuàng)建Method實(shí)例的時(shí)候可以更具具體的方法來(lái)創(chuàng)建。HttpClient的使用一般分如下幾步:

                      1、創(chuàng)建HttpClient實(shí)例。

                      2、創(chuàng)建具體連接方法的實(shí)例。如POST方法創(chuàng)建PostMethod的實(shí)例,在實(shí)例化時(shí)從構(gòu)造函數(shù)中傳入待連接的URL地址。

                      3、對(duì)post的發(fā)送內(nèi)容等信息進(jìn)行配置

                      4、執(zhí)行HttpClient的execute方法

                      5、如果返回的狀態(tài)碼正常,表明連接成功,可以讀取response的內(nèi)容

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

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

          19// 指定請(qǐng)求內(nèi)容的類型 
          20        post.setRequestHeader("Content-type""text/xml; charset=utf-8");
          21//創(chuàng)建HttpClient 實(shí)例 
          22        HttpClient httpclient = new HttpClient();
          23int result;
          24try {
          25//執(zhí)行post方法 
          26            result = httpclient.executeMethod(post);
          27//如果連接正常,獲得返回內(nèi)容          
          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 @ 2008-08-14 14:50 劍客阿飛 閱讀(555) | 評(píng)論 (0)編輯 收藏
           
          主站蜘蛛池模板: 临夏市| 景宁| 浦县| 蒙山县| 工布江达县| 包头市| 义马市| 集贤县| 宜宾市| 观塘区| 兰溪市| 广东省| 灵山县| 万源市| 泸定县| 历史| 中牟县| 马龙县| 高要市| 乳山市| 郎溪县| 五大连池市| 南安市| 大港区| 喀什市| 重庆市| 鱼台县| 巴彦淖尔市| 天津市| 青河县| 湟源县| 砀山县| 博客| 洞头县| 合阳县| 朝阳区| 莱阳市| 贵州省| 麟游县| 宽城| 庆安县|