我最近在做一个网页信息收集器(java),可现在碰到个问题,有些网站是要登陆之后才能访问的,如果我直接用URL访问的话,它会重定向回登陆页面,也就是说下载的是登陆页面的html源码,比喻说我要下载csdn上的“我的帖子”网页的信息,如果我直接创建一个指向“我的帖子”的URL或者URLConnection 下载下来的是登陆页面的源码,我在想是不是要在访问之前先向服务器发送登陆的cookie,或者有没更好的实现方法可以下载需登陆后才能访问的页面,高分悬赏啊...急...或者email交流[email protected]
不用大炮打鸟呢,呵呵,没用到spring,查资料可以用 http header 往服务器发送登陆所需的cookie即可,但还不知道怎么实现
可以用Flter来做,下面是个例子,希望对你有帮助
public class checkFilter implements Filter{
private FilterConfig filterConfig;
//登陆页面,当用户没有登陆,将直接跳转到这个页面
private String loginPage="/LoginPage.jsp";
......
......
public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain)throws IOException,ServletException{
HttpServletRequest req=(HttpServletRequest)request;
HttpServletResponse res=(HttpServletResponse)response;
//通过判断session中是否具有auth参数来判断用户是否已经登陆
HttpSession session=req.getSession(true);
//如果已经登陆
if(session.getAttribute("auth")!=null){
chain.doFilter(req,res);
return;
}
//如果没有登陆
else{
ServletContext ctx=filterConfig.getServletContext();
setForwardUrl(req);
ctx.getRequestDispatcher(loginPage).forward(req,res);
}
}
//设置原始的请求URL
private void setForwardURL(HttpServletRequest request){
//首先获得用户所请求的资源
StringBuffer requestURL=new StringBuffer(request.getRequestURL());
//以及所附的请求字符串,如showMsg?name=CGQ中的?后的内容
String queryString =request.getQueryString();
if(queryString!=null){
requestURL.append("?").append(queryString);
}
//将用户的原始请求放在request的属性中
request.setAttribute("orignURL",requestURL.toString());}}WEB.xml中的设置
所有对auth下的任何资源的访问都会首先通过这个过滤器,那么我们就可以再这个filter程序中去判断该用户是否得到了授权,如果登陆那么转到用户登陆的资源,否则跳转到LoginPage.jsp,让用户登陆,并且将用户原始的请求也发送到LoginPage.jsp中,这样当用户登陆成功后,就可以直接跳转到原来用户所请求的资源中。<filter>
<filter-name>CheckFilter</filter-name>
.....
.....
</filter>
<filter-mapping>
<filter-name>CheckFilter</filter-name>
<url-pattern>/auth/*</url-pattern>
</filter-mapping>登陆页面
LogingPage.jsp<html>
<head>
<title>
登陆页面
<title>
</head>
<body>
<from name="login" method="post" action="/first/logn.jsp">
姓名:<input type="text" name="user">
密码:<input type="password" name="pwd">
<input type="hidden" name="targetUrl" value="<%=request.getAttribute("orignURL")%>">
<input type="submit" name="sub" value="登陆">
</form>
</body>
</html>处理登陆的JSP
login.jsp<%
//处理登陆,此church代码略
session.setAttribute("auth","yes");
String targetUrl=request.getParameter("targetUrl");
if(targetUrl!=null){
response.sendRedirect(targetUrl);
}
else{
out.println("登陆成功!");
}
}
<jsp:include flush="true" page="../../header.jsp">
<jsp:param name="FunctionName" value="opn.print.print_lgp_label"/>
</jsp:include>
格式来做呀,对于这个参数,你进行进行在header.jsp 获得,并且进行查询,当前页的权限在数据库有无,同时,在加载页面,也可以使用session定义一参数,同时传给header.jsp进入数据库查询,如用户名,密码等信息!都可以实现,每加载一个页面,都可以动态载一下呢!
List<String> forbidString = null;
public HttpClientGetHTML(){
forbidString = new ForbidString().getList();
}
public String getURLHTML(String htmlURL,String userParm,
String username, String pswdParm, String pswd,
String host, int port, int reTryTimes) {
String html = null;
//create httpclient object
HttpClient client = new HttpClient();
//if needs proxy, set proxy
if(host!=null&&!"".equalsIgnoreCase(host)){
client.getHostConfiguration().setProxy(host, port);
} int tryTimes = 0;
do{
try{
//if needs username and password ,valid username and password
if(username!=null&&!"".equalsIgnoreCase(username)){
PostMethod post = new PostMethod(htmlURL);
NameValuePair[] data = {
new NameValuePair(userParm, username),
new NameValuePair(pswdParm, pswd)
};
post.setRequestBody(data);
client.executeMethod(post);
post.releaseConnection();
}
//get html
GetMethod get = new GetMethod(htmlURL);
client.executeMethod(get);
html = get.getResponseBodyAsString();
get.releaseConnection();
break;
}catch(IOException e){
break;
}catch(Exception e){
tryTimes ++;
}
}while(tryTimes < reTryTimes);
//return,
//if not exist the news, return null,
//else return html code
if(html==null||"".equals(html)){
return null;
}
int i = 0;
for(;i<forbidString.size();i++){
if(html.toLowerCase().indexOf(forbidString.get(i)) != -1){
break;
}
}
if(i == forbidString.size()){
return html;
}else{
return null;
}
}}