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 不高兴 阅读(1626) 评论(0)  编辑  收藏 所属分类: Java

只有注册用户登录后才能发表评论。


网站导航:
 
 
Copyright © 不高兴 Powered by: 博客园 模板提供:沪江博客