方法一:
          (原始辦法)
          /*
          ?* @remark: 獲取用戶資料
          ?* @param: url發送數據的URL
          ?* @param: param獲取用戶資料的參數
          ?* @return: 獲取用戶資料狀態信息
          ?*/
          ?String GetUser(String url, String param)
          ?{
          ??URL httpurl = null;
          ??HttpURLConnection httpConn = null;
          ??String line = "";
          ?? try
          ?? {
          ???? httpurl = new URL(url);
          ???? httpConn = (HttpURLConnection)httpurl.openConnection();??
          ???? httpConn.setRequestMethod("POST");??? //默認是post
          ???? httpConn.setDoOutput(true);
          ???? httpConn.setDoInput(true);?
          ???? PrintWriter outs = new PrintWriter(httpConn.getOutputStream());
          ???? outs.print(param);
          ???? outs.flush();
          ???? outs.close();???
          ???? BufferedReader? in = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));??
          ???? while ((line = in.readLine()) != null)
          ??? {
          ?? ? ? if (line.indexOf("#") == -1)
          ??? ??? continue;
          ??? ? return line;
          ??? }
          ??? in.close();
          ?
          ?? }catch(Exception e)
          ?? {
          ?? ??return line;
          ?? }
          ?? finally
          ?? {
          ???? if(httpConn!=null)
          ?????? httpConn.disconnect();
          ?? }
          ??return line;
          ?}
          該方法寫的很簡單,不夠完善。
          方法二:
          <%@ page language="java"? pageEncoding="GBK"%>
          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
          <html>?
          ? <body>
          ??? <%
          ??????? String name1=request.getParameter("name1");
          ??????? if(null!=name1){
          ??????????? out.print(name1);out.print("<br>");
          ??????? }
          ??????? String name2=request.getParameter("name2");
          ??????? if(null!=name1){
          ??????????? out.print(name2);out.print("<br>");
          ??????? }
          ??? %>
          ? </body>
          </html>


          接下來就就是我們的程序部分了:

          import java.io.IOException;

          import org.apache.commons.httpclient.HttpClient;
          import org.apache.commons.httpclient.HttpException;
          import org.apache.commons.httpclient.HttpMethod;
          import org.apache.commons.httpclient.NameValuePair;
          import org.apache.commons.httpclient.methods.GetMethod;
          import org.apache.commons.httpclient.methods.PostMethod;

          public class Test {
          ??? /**
          ???? * @param args
          ???? */
          ??? public static void main(String[] args) {
          ??????? HttpClient client = new HttpClient();
          ??????? HttpMethod httpPost =getPostMethod();
          ??????? //HttpMethod httpPost =getGetMethod();
          ??????? client.getHostConfiguration().setHost("localhost", 8080, "http");
          ??????? try {
          ??????????? int status = client.executeMethod(httpPost);
          ??????????? if(status==200)
          ??????????? {
          ??????????????? System.out.println(httpPost.getResponseBodyAsString());
          ??????????? }
          ??????????? else
          ??????????? {
          ??????????????? System.out.println("頁面請求返回值為:"+status);
          ??????????? }
          ??????? } catch (HttpException e) {
          ??????????? e.printStackTrace(System.err);
          ??????? } catch (IOException e) {
          ??????????? e.printStackTrace(System.err);
          ??????? }finally{
          ??????????? httpPost.releaseConnection();
          ??????? }
          ??? }
          ??? /**
          ???? * 使用POST方式提交數據
          ???? * @return
          ???? */
          ??? private static HttpMethod getPostMethod(){
          ??????? PostMethod post = new PostMethod("/PostTest/index.jsp");
          ??????? NameValuePair simcard = new NameValuePair("name1","1330227");
          ??????? NameValuePair simcard2 = new NameValuePair("name2","kjasdfklalsf");
          ??????? post.setRequestBody(new NameValuePair[] { simcard,simcard2});
          ??????? return post;
          ??? }
          ??? /**
          ???? * 使用GET方式提交數據
          ???? * @return
          ???? */
          ??? private static HttpMethod getGetMethod(){
          ??????? return new GetMethod("/PostTest/index.jsp?name1=1330227&name2=asdfsdf");
          ??? }
          }

          注意:大家要把commons-codec-1.3.jar,commons-httpclient-3.0.jar,commons-logging.jar這三個JAR包加到我們程序的classpath中,才能使用跑起來.

          posted on 2007-12-23 23:38 -274°C 閱讀(6484) 評論(5)  編輯  收藏 所屬分類: JAVA


          FeedBack:
          # re: JAVA模擬POST
          2007-12-24 08:49 | 隔葉黃鶯
          確切講,這不是模擬,是切切實實的POST請求

          模擬是什么,不要你手操作鼠標點擊,而只用程序完成點擊就是模擬

          點擊提交按鈕是POST,程序實現同樣是POST  回復  更多評論
            
          # re: JAVA模擬POST
          2008-07-08 19:20 | 這一
          大家要把commons-codec-1.3.jar,commons-httpclient-3.0.jar,commons-logging.jar這三個JAR包加到我們程序的classpath中,才能使用跑起來.
          請問怎樣加的啊,能不能具體一點???  回復  更多評論
            
          # re: JAVA模擬POST
          2008-07-08 21:29 | 274
          @這一 :

          classpath 的相關設置:

          1.最簡單的當然是設置在環境變量里。

          2.在netbeans 下 項目 右鍵 屬性 庫 添加jar

          3.eclipse下 項目 右鍵 buildpath 下 添加 add 額外 jar。

            回復  更多評論
            
          # re: JAVA模擬POST
          2008-12-15 18:07 | Mars.CN
          你說的那是那個包能不能給個下載地址?  回復  更多評論
            
          # re: JAVA模擬POST[未登錄]
          2008-12-15 19:49 | -274°C
          @Mars.CN

          你在apache tomcat 下找,這里并沒有詳細說明,因為寫J2EE的一般都知道這幾個jar。  回復  更多評論
            

          常用鏈接

          留言簿(21)

          隨筆分類(265)

          隨筆檔案(242)

          相冊

          JAVA網站

          關注的Blog

          搜索

          •  

          積分與排名

          • 積分 - 914173
          • 排名 - 40

          最新評論

          主站蜘蛛池模板: 宝兴县| 新营市| 水富县| 海宁市| 鄂温| 鄢陵县| 平陆县| 彭山县| 宣武区| 海伦市| 井冈山市| 攀枝花市| 巴彦县| 建始县| 仁化县| 灵寿县| 安达市| 洮南市| 青田县| 清徐县| 云林县| 兰州市| 龙门县| 当雄县| 邓州市| 开化县| 汉沽区| 聂拉木县| 西昌市| 兰考县| 家居| 宜阳县| 武威市| 淄博市| 武山县| 襄汾县| 黄浦区| 江北区| 合水县| 佛坪县| 保亭|