nighty

          折騰的年華
          posts - 37, comments - 143, trackbacks - 0, articles - 0

          httpclient3.1使用小結(jié)

          Posted on 2008-06-11 15:18 寒武紀(jì) 閱讀(6929) 評(píng)論(8)  編輯  收藏 所屬分類: Java

          Jakarta的httpclient3.1是最新版本,項(xiàng)目中需要用程序模擬瀏覽器的GET和POST動(dòng)作。在使用過(guò)程中遇到不少問(wèn)題。
          1. 帶附件的POST提交
              最開(kāi)始都是使用MultipartPostMethod這個(gè)類,現(xiàn)在已經(jīng)廢棄這個(gè)類了。API說(shuō)明:Deprecated. Use MultipartRequestEntity in conjunction with PostMethod instead.   使用PostMethod可以實(shí)現(xiàn)的功能,就沒(méi)有必要再弄一個(gè)MultipartPostMethod了。下面是一段最簡(jiǎn)單的示例:

          PostMethod post = new PostMethod();
                  NameValuePair[] pairs 
          = new NameValuePair[2];
                  pairs[
          0= new NameValuePair("para1""value1");
                  pairs[
          0= new NameValuePair("para2""value2");
                  post.setRequestBody(pairs);
                  HttpClient client 
          = new HttpClient();
                  
          try {
                      client.executeMethod(post);
                  }
           catch (HttpException e) {
                      e.printStackTrace();
                  }
           catch (IOException e) {
                      e.printStackTrace();
                  }
             這是針對(duì)一般的form形式的提交,而且這個(gè)form里面不帶附件的。如果帶附件,那么這種方法就不起作用,附件上傳的參數(shù)和普通參數(shù)無(wú)法一同在服務(wù)器獲取到。org.apache.commons.httpclient.methods.multipart 這個(gè)包就是為處理文件上傳這種多形式參數(shù)的情況的。最主要的類是Part(代表一種post object),它有二個(gè)比較重要的子類:FilePart和StringPart,一個(gè)是文件的參數(shù),另一個(gè)就是普通的文本參數(shù)。它的典型使用方法如下:
          String url = "http://localhost:8080/HttpTest/Test";
                   PostMethod postMethod 
          = new PostMethod(url);
                   
                   StringPart sp 
          = new StringPart("TEXT""testValue");
                   FilePart fp 
          = new FilePart("file""test.txt"new File("./temp/test.txt"));
                   
                   MultipartRequestEntity mrp
          = new MultipartRequestEntity(new Part[]{sp, fp}, postMethod
                           .getParams());
                   postMethod.setRequestEntity(mrp);
                   
                   
          //執(zhí)行postMethod
                   HttpClient httpClient = new HttpClient();
                   
          try {
                      httpClient.executeMethod(postMethod);
                  }
           catch (HttpException e) {
                      e.printStackTrace();
                  }
           catch (IOException e) {
                      e.printStackTrace();
                  }
              在第二行PostMethod postMethod = new PostMethod();后面,有人說(shuō)需要使用postMehtod.setRequestHeader("Content-type", "multipart/form-data"); Content-type的請(qǐng)求類型進(jìn)行更改。但是我在使用過(guò)程沒(méi)有加上這一句,查了一下httpCleint的默認(rèn)Content-type是application/octet-stream。應(yīng)該是沒(méi)有影響的。對(duì)于MIME類型的請(qǐng)求,httpclient建議全用MulitPartRequestEntity進(jìn)行包裝,就是上面的用法。

          2.  參數(shù)中文的處理問(wèn)題
              httpclient的默認(rèn)編碼都是ISO-8859-1,那肯定就無(wú)法支持中文參數(shù)了。引用一下這篇文章:http://thinkbase.net/w/main/Wiki?HttpClient+POST+%E7%9A%84+UTF-8+%E7%BC%96%E7%A0%81%E9%97%AE%E9%A2%98 ,按照作者的說(shuō)法,就可以正常解決中文編碼的問(wèn)題。其中最關(guān)鍵的是修改EncodingUtil這個(gè)類的一個(gè)方法實(shí)現(xiàn)。另外,F(xiàn)ilePart和StringPart的構(gòu)造方法都有一個(gè)帶編碼指定的參數(shù),為了減少問(wèn)題的出現(xiàn),建議所有的都帶上統(tǒng)一的編碼,包括postMethod.getParams()。示例如下:
          String url = "http://localhost:8080/HttpTest/Test";
                   PostMethod postMethod 
          = new PostMethod(url);
                   
                   StringPart sp 
          = new StringPart("TEXT""testValue""GB2312");
                   FilePart fp 
          = new FilePart("file""test.txt"new File("./temp/test.txt"), null"GB2312");
                   
                   postMethod.getParams().setContentCharset(
          "GB2312");
                   MultipartRequestEntity mrp
          = new MultipartRequestEntity(new Part[]{sp, fp}, postMethod
                           .getParams());
                   postMethod.setRequestEntity(mrp);
                   
                   
          //執(zhí)行postMethod
                   HttpClient httpClient = new HttpClient();
                   
          try {
                      httpClient.executeMethod(postMethod);
                  }
           catch (HttpException e) {
                      e.printStackTrace();
                  }
           catch (IOException e) {
                      e.printStackTrace();
                  }



          剛進(jìn)場(chǎng)的時(shí)候戲就落幕

          Feedback

          # re: httpclient3.1使用小結(jié)  回復(fù)  更多評(píng)論   

          2008-06-13 15:49 by ~上善若水~
          傳智播客ajax全套內(nèi)部視頻獨(dú)家發(fā)布,免費(fèi)下載

          1.ajax 入門

          2.ajax 原理

          3.ajax 簡(jiǎn)單實(shí)例

          4.ajax 無(wú)限級(jí)聯(lián)動(dòng)菜單

          5.ajax 簡(jiǎn)易聊天室

          6.ajax 開(kāi)源框架簡(jiǎn)介

          7.DWR 框架源碼分析一

          8.DWR 框架源碼分析二

          9.DWR 框架源碼分析三

          10.DWR 框架源碼分析四

          11.DWR框架源碼分析五

          12.SSH + DWR完成商城驅(qū)動(dòng)

          13. Extjs 簡(jiǎn)介

          14 Extjs  簡(jiǎn)單實(shí)例

          15.SSH + Extjs 開(kāi)發(fā)系列之OA一

          16. SSH + Extjs 開(kāi)發(fā)系列之OA二

          17. SSH + Extjs 開(kāi)發(fā)系列之OA三

          18. SSH + Extjs 開(kāi)發(fā)系列之OA四

          19 .SSH + Extjs 開(kāi)發(fā)系列之OA五

          20. SSH + Extjs 開(kāi)發(fā)系列之OA六

          21. SSH + Extjs 開(kāi)發(fā)系列之OA七

          22. SSH + Extjs 開(kāi)發(fā)系列之OA八

          23.SSH + Extjs 開(kāi)發(fā)系列之OA九

          24.SSH + Extjs 開(kāi)發(fā)系列之OA十

          25. ajax 前景之我見(jiàn)

          下載地址:http://www.ibeifeng.com/read.php?tid=2338&u=5043

          # re: httpclient3.1使用小結(jié)  回復(fù)  更多評(píng)論   

          2008-09-05 13:49 by HttpClient

          標(biāo)題 請(qǐng)輸入標(biāo)題
          姓名 請(qǐng)輸入你的姓名
          主頁(yè)
          請(qǐng)輸入驗(yàn)證碼
          驗(yàn)證碼 *

          內(nèi)容(請(qǐng)不要發(fā)表任何與政治相關(guān)的內(nèi)容) 請(qǐng)輸入評(píng)論內(nèi)容


          Remember Me?
          登錄 使用高級(jí)評(píng)論 新用戶注冊(cè) 返回頁(yè)首 恢復(fù)上次提交
          [使用Ctrl+Enter鍵可以直接提交]




          相關(guān)鏈接:
          怎么優(yōu)化使用asp.net ajax的網(wǎng)站速度
          碰到技術(shù)難題?馬上使用找找看!
          如何使用Div+CSS布局?
          淘金高階4級(jí)全真試題(2002.1--2007.12)13套題(MP3版)

          # Seo News  回復(fù)  更多評(píng)論   

          2009-05-18 10:05 by Seo News
          Hello everyone. Looks are part of business. A businessman should never stand out more than his customers. His mannerisms, his clothes, everything about him... Moderation is the key.
          I am from Barbados and , too, and now am writing in English, give true I wrote the following sentence: "Professional seo services increase natural rankings.Webmarketing is a california seo search engine optimization company with offices in san francisco los angeles offering seo search engine."

          Thanks for the help ;-), Kristin.

          # re: httpclient3.1使用小結(jié)  回復(fù)  更多評(píng)論   

          2010-10-23 15:28 by 張路
          http://www.aygfsteel.com/nighty/archive/2008/06/11/207121.htm

          # shi  回復(fù)  更多評(píng)論   

          2010-11-09 16:29 by uk dress
          too, and now am writing in English, give true I wrote the following sentence: "Professional seo services increase natural rankings.Webmarketing is a california seo search engine optimization company with offices in san francisco los angeles offering seo search engine."

          # tag heuer replica  回復(fù)  更多評(píng)論   

          2011-01-07 10:29 by tag heuer replica
          In 1990, the john henry has tortuous lavish re-birth when ponderous was registered further.

          # re: httpclient3.1使用小結(jié)  回復(fù)  更多評(píng)論   

          2011-05-24 14:11 by hair extensions
          I like it, I learned something today! Thanks!

          # shox r2  回復(fù)  更多評(píng)論   

          2011-06-02 14:29 by shox r2
          ok,i know.
          主站蜘蛛池模板: 都匀市| 昔阳县| 安仁县| 梅州市| 连山| 新巴尔虎右旗| 汾西县| 浦东新区| 石楼县| 新竹县| 浦县| 望奎县| 濉溪县| 贵南县| 南召县| 南投市| 上蔡县| 麻阳| 栖霞市| 永寿县| 射阳县| 金堂县| 阿勒泰市| 孟州市| 金沙县| 辛集市| 合肥市| 万安县| 万盛区| 微博| 卢龙县| 阳泉市| 北宁市| 通榆县| 屏南县| 文水县| 志丹县| 黔江区| 蒙城县| 高台县| 都匀市|