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

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

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

    posts - 23,comments - 66,trackbacks - 0
    from: java實(shí)現(xiàn)web登陸

    web登陸無(wú)非就是網(wǎng)頁(yè)獲取,cookie 的管理,post和get方式的模擬。

    1.網(wǎng)頁(yè)內(nèi)容獲取
    ???? java.io.InputStream??in;
    ????? java.net.URL url = new java.net.URL(www.xyz.com/content.html);
    ????? java.net.HttpURLConnection connection = (java.net.HttpURLConnection)
    ????????? url.openConnection();
    ????? connection = (java.net.HttpURLConnection) url.openConnection();
    ???? //模擬成IE
    ????? connection.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)");
    ????? connection.connect();
    ??????in = connection.getInputStream();
    ????? java.io.BufferedReader breader =
    ????????? new BufferedReader(new InputStreamReader(in , "GBK"));
    ????? String str=breader.readLine());
    ????? while(st != null){
    ??????? System.out.println(str);
    ????????str=breader.readLine());
    ????? }
    2.cookie管理

    ?
    1.直接的方式
    取得cookie:
    HttpURLConnection huc= (HttpURLConnection) url.openConnection();
    InputStream is = huc.getInputStream();
    // 取得sessionID.
    String cookieVal = hc.getHeaderField("Set-Cookie");
    String sessionId;
    if(cookieVal != null)
    {
    ??? sessionId = cookieVal.substring(0, cookieVal.indexOf(";"));
    }

    ?發(fā)送設(shè)置cookie:
    HttpURLConnection huc= (HttpURLConnection) url.openConnection();
    if(sessionId != null)
    {
    huc.setRequestProperty("Cookie", sessionId);
    }
    InputStream is = huc.getInputStream();

    2.利用的jcookie包(http://jcookie.sourceforge.net/ )
    獲取cookie:

    URL url = new URL("http://www.site.com/");
    HttpURLConnection huc = (HttpURLConnection) url.openConnection();
    huc.connect();
    InputStream is = huc.getInputStream();
    Client client = new Client();
    CookieJar cj = client.getCookies(huc);

    新的請(qǐng)求,利用上面獲取的cookie:

    url = new URL("http://www.site.com/");
    huc = (HttpURLConnection) url.openConnection();
    client.setCookies(huc, cj);

    3.post方式的模擬
    URL url = new URL("www.xyz.com");
    ??HttpURLConnection huc = (HttpURLConnection) url.openConnection();
    //設(shè)置允許output
    ??huc.setDoOutput(true);
    //設(shè)置為post方式
    ??huc.setRequestMethod("POST");
    ??huc.setRequestProperty("User-Agent","Mozilla/4.7 [en] (Win98; I)");
    ??StringBuffer sb = new StringBuffer();
    ??sb.append("userName="+userNme);
    ??sb.append("&password="+password);

    //post信息
    ?OutputStream os = huc.getOutputStream();
    ?os.write(sb.toString().getBytes("GBK"));
    ?os.close();

    BufferedReader br = new BufferedReader(new InputStreamReader(huc.getInputStream()))

    huc.connect();

    String line = br.readLine();

    while(line != null){

    l

    System.out.printli(line);

    line = br.readLine();

    }



    結(jié)束語(yǔ):
    ?????上面的代碼基本就可以實(shí)現(xiàn)網(wǎng)站的登陸、信息獲取的功能??吹降顷懫姹溶浖u(mài)的那么火,我們也做一個(gè)去吧.

    參考:
    1.A recipe for cookie management
    http://www.javaworld.com/javaworld/jw-04-2002/jw-0426-cookie.html

    posted on 2006-03-21 21:47 rd2pm 閱讀(1335) 評(píng)論(0)  編輯  收藏

    只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     

    主站蜘蛛池模板: 亚洲s码欧洲m码吹潮| 在线免费不卡视频| 无人视频在线观看免费播放影院| 99久久亚洲综合精品成人网| 红杏亚洲影院一区二区三区| 精品久久洲久久久久护士免费| 五月婷婷综合免费| 日韩免费人妻AV无码专区蜜桃| 国产精品成人免费观看| 日韩在线视频播放免费视频完整版| 亚洲欧洲日韩国产一区二区三区 | 在线观看亚洲专区| 亚洲中文无码线在线观看| 国产av天堂亚洲国产av天堂 | 久久毛片免费看一区二区三区| 精品韩国亚洲av无码不卡区| 国产精品亚洲精品| 亚洲一级片在线观看| 亚洲日韩国产精品无码av| 亚洲春色另类小说| 亚洲综合久久成人69| 久久精品国产亚洲av水果派| 久久精品亚洲综合专区| 亚洲国产日韩在线视频| 亚洲中文字幕不卡无码| 亚洲啪啪AV无码片| 亚洲中文字幕无码爆乳AV| 中文亚洲AV片在线观看不卡| 久久精品亚洲乱码伦伦中文| 亚洲黄片手机免费观看| 国产成人精品久久亚洲| 国产成人综合亚洲亚洲国产第一页| 亚洲国产精品成人久久蜜臀| 亚洲成人一区二区| 亚洲午夜福利精品无码| 国产亚洲精品影视在线产品| 久久99国产亚洲高清观看首页| 亚洲国产精品一区二区第一页| 亚洲AV无码久久精品成人| 亚洲天堂中文资源| tom影院亚洲国产一区二区|