08-12
05
java.net.HttpURLConnection的使用
作者:Java伴侣 日期:2008-12-05
突然需要做一个Java的Ajax代理,只好又操起java来了。。。。。。写好了再放出源码
转贴内容:
web登陆无非就是网页获取,cookie 的管理,post和get方式的模拟。
1.网页内容获取
2.cookie管理
1.直接的方式
取得cookie:
发送设置cookie:
2.利用的jcookie包(http://jcookie.sourceforge.net/ )
获取cookie:
新的请求,利用上面获取的cookie:
3.post方式的模拟
转贴内容:
web登陆无非就是网页获取,cookie 的管理,post和get方式的模拟。
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());
}
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(";"));
}
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();
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);
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);
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();
}
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();
}
评论: 0 | 引用: 0 | 查看次数: 518
发表评论