100分求答案(数据采集cookie)
怎么能读取网页有cookie的内容,java怎么实现?
/**
* 读取网络的一个文本文件(图片不行)
* @param url
* @return String
*/
public String urlConnnect(String url)
{
String inputLine;
String temp = " ";
try
{
URL yahoo = new URL(url);
BufferedReader in = new BufferedReader(new InputStreamReader(yahoo.openStream()));
while ((inputLine=in.readLine()) != null)
temp = temp + inputLine + "\n ";
in.close();
}
catch(Exception e)
{
e.printStackTrace();
System.out.println(e);
}
return temp;
}
这是不带cookie的读取方法,带的要怎么实现?
------解决方案--------------------不懂就一边玩去
去下个HttpClient的包
HttpClient client=new HttpClient();
GetMethod method=new GetMethod(url);
int status=client.excute..Method(method);这单词有点长,记不住了.
Cookies[] cookies=client.getStatu.getCookie();
这就行了!
------解决方案--------------------package test;
import javax.servlet.http.*;
import java.io.*;
public class myCookie {
public myCookie() {
}
/*
* 本文件集中了读、写、删除COOKIE的方法
*/
// 定义ICP的域名.xerllent.cn
public static String hostName = ".xerllent.cn ";
/**
* <p> Desc: 读取的COOKIE的方法 </p>
* @param request HttpServletRequest的请求句本.
* @param cookieName COOKIE对象的名称.
* @return 返回对应于COOKIE对象的名称的内容.
*/
public static String getCookieValue(HttpServletRequest request, String cookieName) {
Cookie cookie, aryCookie[];
aryCookie = request.getCookies();
if (aryCookie == null) {
return null;
}
for (int i = 0; i < aryCookie.length; i++) {
cookie = aryCookie[i];
if (cookie.getName().equals(cookieName)) {
return cookie.getValue();
}
}
return null;
}
/**
* <p> Desc: 删除的COOKIE的方法 </p>
*
* @param request HttpServletRequest的请求句本.
* @param response HttpServletResponse的请求句本.
* @param cookieName COOKIE对象的名称.
*
* @return 返回对应于COOKIE对象的名称的内容..
*/
public static void removeCookie(HttpServletRequest request,
HttpServletResponse response,
String cookieName) {
Cookie cookie, aryCookie[];
aryCookie = request.getCookies();
int i;
for (i = 0; i < aryCookie.length; i++) {
cookie = aryCookie[i];
if (cookie.getName().equals(cookieName)) {
cookie.setValue(null);
response.addCookie(cookie);
}
else {
Cookie cook = new Cookie(cookieName, null);
cook.setVersion(0);
response.addCookie(cook);
}
}
}
/**
* <p> Desc: 写的COOKIE的方法 </p>
*
* @param request HttpServletRequest的请求句本.
* @param response HttpServletResponse的请求句本.
* @param cookieName COOKIE对象的名称.
* @param cookieValue 对应于COOKIE对象的名称的内容.
*
*/
public static void addCookie(HttpServletResponse response,
String cookieName,
String cookieValue) {
Cookie cookie;
cookie = new Cookie(cookieName, cookieValue);