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

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

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

    方法一:
    (原始辦法)
    /*
    ?* @remark: 獲取用戶資料
    ?* @param: url發(fā)送數(shù)據(jù)的URL
    ?* @param: param獲取用戶資料的參數(shù)
    ?* @return: 獲取用戶資料狀態(tài)信息
    ?*/
    ?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");??? //默認(rèn)是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;
    ?}
    該方法寫的很簡(jiǎn)單,不夠完善。
    方法二:
    <%@ 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>


    接下來(lái)就就是我們的程序部分了:

    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("頁(yè)面請(qǐng)求返回值為:"+status);
    ??????????? }
    ??????? } catch (HttpException e) {
    ??????????? e.printStackTrace(System.err);
    ??????? } catch (IOException e) {
    ??????????? e.printStackTrace(System.err);
    ??????? }finally{
    ??????????? httpPost.releaseConnection();
    ??????? }
    ??? }
    ??? /**
    ???? * 使用POST方式提交數(shù)據(jù)
    ???? * @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方式提交數(shù)據(jù)
    ???? * @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這三個(gè)JAR包加到我們程序的classpath中,才能使用跑起來(lái).

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


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

    模擬是什么,不要你手操作鼠標(biāo)點(diǎn)擊,而只用程序完成點(diǎn)擊就是模擬

    點(diǎn)擊提交按鈕是POST,程序?qū)崿F(xiàn)同樣是POST  回復(fù)  更多評(píng)論
      
    # re: JAVA模擬POST
    2008-07-08 19:20 | 這一
    大家要把commons-codec-1.3.jar,commons-httpclient-3.0.jar,commons-logging.jar這三個(gè)JAR包加到我們程序的classpath中,才能使用跑起來(lái).
    請(qǐng)問(wèn)怎樣加的啊,能不能具體一點(diǎn)???  回復(fù)  更多評(píng)論
      
    # re: JAVA模擬POST
    2008-07-08 21:29 | 274
    @這一 :

    classpath 的相關(guān)設(shè)置:

    1.最簡(jiǎn)單的當(dāng)然是設(shè)置在環(huán)境變量里。

    2.在netbeans 下 項(xiàng)目 右鍵 屬性 庫(kù) 添加jar

    3.eclipse下 項(xiàng)目 右鍵 buildpath 下 添加 add 額外 jar。

      回復(fù)  更多評(píng)論
      
    # re: JAVA模擬POST
    2008-12-15 18:07 | Mars.CN
    你說(shuō)的那是那個(gè)包能不能給個(gè)下載地址?  回復(fù)  更多評(píng)論
      
    # re: JAVA模擬POST[未登錄](méi)
    2008-12-15 19:49 | -274°C
    @Mars.CN

    你在apache tomcat 下找,這里并沒(méi)有詳細(xì)說(shuō)明,因?yàn)閷慗2EE的一般都知道這幾個(gè)jar。  回復(fù)  更多評(píng)論
      

    常用鏈接

    留言簿(21)

    隨筆分類(265)

    隨筆檔案(242)

    相冊(cè)

    JAVA網(wǎng)站

    關(guān)注的Blog

    搜索

    •  

    積分與排名

    • 積分 - 914554
    • 排名 - 40

    最新評(píng)論

    主站蜘蛛池模板: 久久久久久A亚洲欧洲AV冫| 亚洲精品国产日韩无码AV永久免费网 | 又大又硬又粗又黄的视频免费看| 亚洲天堂中文字幕在线观看| 亚洲人成人77777网站| 国产免费无遮挡精品视频| 国产免费丝袜调教视频| 久久久精品免费国产四虎| 一级做a爰片久久毛片免费陪| 亚洲kkk4444在线观看| 亚洲国产精品综合久久网各| 久久久青草青青亚洲国产免观| 日韩亚洲精品福利| 国产精品色午夜免费视频 | 亚洲AV成人无码天堂| 亚洲免费在线视频| 亚洲国产精品va在线播放| 中文字幕亚洲一区| 亚洲人成人网站在线观看| 免费v片在线观看无遮挡| 日韩免费一级毛片| 国产青草视频在线观看免费影院| 永久免费的网站在线观看| h片在线免费观看| 91网站免费观看| 国产精品永久免费10000| aⅴ在线免费观看| 97碰公开在线观看免费视频| 亚洲一区二区三区免费视频| 亚洲精品国产免费| 四虎在线最新永久免费| 久久笫一福利免费导航| 久久WWW免费人成人片| 免费av欧美国产在钱| 大陆一级毛片免费视频观看| 免费观看理论片毛片| 国产美女无遮挡免费网站| 免费一级毛片正在播放| 亚洲国产综合无码一区二区二三区 | 成年女人男人免费视频播放| 毛片免费在线观看网站|