Energy of Love  
          日歷
          <2009年6月>
          31123456
          78910111213
          14151617181920
          21222324252627
          2829301234
          567891011
          統計
          • 隨筆 - 70
          • 文章 - 0
          • 評論 - 80
          • 引用 - 0

          導航

          常用鏈接

          留言簿

          隨筆分類

          隨筆檔案

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

           

          1.網頁內容獲取
          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(";"));
          }

          發送設置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);


          新的請求,利用上面獲取的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();
          //設置允許output
          huc.setdooutput(true);
          //設置為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();

          }


          本文來自CSDN博客,轉載請標明出處:http://blog.csdn.net/hyde82/archive/2007/06/06/1640817.aspx

          posted on 2009-06-05 09:33 不高興 閱讀(1638) 評論(0)  編輯  收藏 所屬分類: Java
           
          Copyright © 不高興 Powered by: 博客園 模板提供:滬江博客
          主站蜘蛛池模板: 中卫市| 大田县| 英吉沙县| 皋兰县| 西充县| 利辛县| 拉孜县| 哈巴河县| 南安市| 诏安县| 无棣县| 宜城市| 上蔡县| 沭阳县| 根河市| 岳普湖县| 两当县| 张家口市| 五大连池市| 吉木乃县| 水富县| 奉节县| 双鸭山市| 香港| 齐河县| 金湖县| 马尔康县| 金溪县| 怀柔区| 得荣县| 宁城县| 龙州县| 华亭县| 兰西县| 汉寿县| 枣阳市| 六盘水市| 嘉义市| 闻喜县| 临汾市| 浙江省|