<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    方法一:
    (原始辦法)
    /*
    ?* @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 閱讀(6499) 評論(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

    搜索

    •  

    積分與排名

    • 積分 - 916920
    • 排名 - 40

    最新評論

    主站蜘蛛池模板: 亚洲av中文无码| 在线观看亚洲免费| 免费网站观看WWW在线观看| 一区二区三区免费看| 丁香花在线观看免费观看图片| 色www永久免费| 德国女人一级毛片免费| 亚洲第一黄片大全| 色网站在线免费观看| 午夜无码A级毛片免费视频| 免费无码A片一区二三区 | 国内精品免费久久影院| 亚洲一区二区三区无码中文字幕| 亚洲精彩视频在线观看| 国产午夜亚洲精品不卡免下载| 国产麻豆一精品一AV一免费 | 视频一区二区三区免费观看| 国产免费观看黄AV片| 亚洲视屏在线观看| 一级一片免费视频播放| 久久亚洲国产欧洲精品一| 亚洲最大的成人网| 男女作爱在线播放免费网站| 国产乱子伦精品免费无码专区| 边摸边吃奶边做爽免费视频99| 亚洲综合熟女久久久30p| 久久亚洲AV成人无码国产最大| 一区二区三区福利视频免费观看| 亚洲黄色在线电影| 国产成人涩涩涩视频在线观看免费| 亚洲视频免费在线看| 成人免费视频软件网站| 亚洲中文久久精品无码1| 一个人免费视频观看在线www| 久久夜色精品国产噜噜亚洲AV| 国产福利电影一区二区三区,免费久久久久久久精 | 国产亚洲精aa成人网站| 亚洲日韩精品无码专区加勒比☆| 最近中文字幕免费2019| 久久被窝电影亚洲爽爽爽| 亚洲三级高清免费|