<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 閱讀(6485) 評論(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

    搜索

    •  

    積分與排名

    • 積分 - 914354
    • 排名 - 40

    最新評論

    主站蜘蛛池模板: 亚洲成色WWW久久网站| 免费人成视频在线观看不卡| 亚洲αv在线精品糸列| 全黄A免费一级毛片| 国产jizzjizz免费视频| 亚洲AV成人无码网天堂| 全免费a级毛片免费看不卡| 亚洲日韩精品无码专区| 午夜爱爱免费视频| 真正全免费视频a毛片| www.亚洲精品.com| 国产精品福利片免费看| 亚洲综合AV在线在线播放| 中文毛片无遮挡高清免费| 亚洲AV午夜成人片| 久草视频免费在线观看| 国产色在线|亚洲| 国产极品粉嫩泬免费观看| 四虎精品成人免费视频| 久久亚洲国产欧洲精品一| 男人j进入女人j内部免费网站| 亚洲av无码成人黄网站在线观看| 桃子视频在线观看高清免费视频| 亚洲精品中文字幕麻豆| 国产在线a免费观看| 午夜亚洲国产精品福利| 国产亚洲精品不卡在线| 久9久9精品免费观看| 亚洲一区精品视频在线| 国产国产人免费视频成69大陆| 国产VA免费精品高清在线| 亚洲AV天天做在线观看| 国产精品久久久久久久久久免费| 国产综合激情在线亚洲第一页| 亚洲一级黄色视频| 1000部拍拍拍18勿入免费视频软件| 亚洲暴爽av人人爽日日碰| 国产中文在线亚洲精品官网| 无码人妻久久一区二区三区免费| 亚洲欧美成aⅴ人在线观看| 久久精品国产亚洲7777|