首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

急啊!各位大哥帮帮忙, HttpClient 自动登录https服务器有关问题!

2011-12-19 
急啊!!!!各位大哥帮帮忙, HttpClient 自动登录https服务器问题!!!https自动登陆,取得了目的页面的url,可是

急啊!!!!各位大哥帮帮忙, HttpClient 自动登录https服务器问题!!!
https自动登陆,     取得了目的页面的url,     可是重定向的时候又回到了登录页面,     怎么办啊?????  

    我的代码如下:各位大哥帮我分析一下,   错哪了.

        package   com.venustech.utm30.mgrweb.action;

import   java.io.IOException;


import   javax.servlet.http.HttpServletRequest;
import   javax.servlet.http.HttpServletResponse;


import   org.apache.commons.httpclient.Cookie;
import   org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import   org.apache.commons.httpclient.Header;
import   org.apache.commons.httpclient.HttpClient;
import   org.apache.commons.httpclient.HttpException;
import   org.apache.commons.httpclient.HttpStatus;
import   org.apache.commons.httpclient.NameValuePair;
import   org.apache.commons.httpclient.methods.PostMethod;
import   org.apache.commons.httpclient.params.HttpMethodParams;
import   org.apache.commons.httpclient.protocol.Protocol;
import   org.apache.struts.action.Action;
import   org.apache.struts.action.ActionForm;
import   org.apache.struts.action.ActionForward;
import   org.apache.struts.action.ActionMapping;

import   com.venustech.utm30.mgrweb.utils.MySecureProtocolSocketFactory;


public   class   ProcessDataCenterAction   extends   Action   {

@Override
public   ActionForward   execute(ActionMapping   arg0,   ActionForm   arg1,
HttpServletRequest   arg2,   HttpServletResponse   response)
throws   Exception   {

HttpClient   httpClient   =   new   HttpClient();
String   logonSite   =   this.getLogonSite();
PostMethod   firstMethod   =   new   PostMethod(logonSite);
preparedInfos(httpClient,   firstMethod);
processLogon(httpClient,   firstMethod);

int   statuscode   =   firstMethod.getStatusCode();
String   newuri   =   " ";
if   ((statuscode   ==   HttpStatus.SC_MOVED_TEMPORARILY)
||   (statuscode   ==   HttpStatus.SC_MOVED_PERMANENTLY)
||   (statuscode   ==   HttpStatus.SC_SEE_OTHER)
||   (statuscode   ==   HttpStatus.SC_TEMPORARY_REDIRECT))   {

Header   redirect   =   firstMethod.getResponseHeader( "location ");

if   (redirect   !=   null)   {
newuri   =   redirect.getValue();
if   ((newuri   ==   null)   ||   (newuri.equals( " ")))
newuri   =   "/ ";
}

Header[]   headers   =   firstMethod.getResponseHeaders();
for   (int   index   =   0;   index   <   headers.length;   index++){
if(headers[index]   !=   null)
      response.addHeader(headers[index].getName(),   headers[index].getValue());

System.out.println( "headers:::: "   +   headers[index]);
}
}

Cookie[]   logoncookies   =   httpClient.getState().getCookies();
String   temp   =   "? ";
for   (int   index   =   0;   index   <   logoncookies.length;   index++)   {
Cookie   tem_cookie   =   logoncookies[index];
javax.servlet.http.Cookie   cookie   =   new   javax.servlet.http.Cookie(tem_cookie.getName(),   tem_cookie.getValue());


cookie.setDomain(tem_cookie.getDomain());
cookie.setComment(tem_cookie.getComment());
cookie.setPath(tem_cookie.getPath());
cookie.setSecure(tem_cookie.getSecure());
cookie.setMaxAge(5*24*60*60);
response.addCookie(cookie);

String   name   =   tem_cookie.getName();
String   value   =   tem_cookie.getValue();
temp   =   temp   +   name.trim()   +   "= "   +   value.trim()   +   "& ";
}
int   pos   =   temp.lastIndexOf( "& ");
if   (pos   ==   temp.length()   -   1)
temp   =   temp.substring(0,   pos);
//newuri   =   newuri   +   temp;

response.sendRedirect(newuri);
return   null;
}

private   void   preparedInfos(HttpClient   httpClient,   PostMethod   firstMethod){

//String   logonSite   =   this.getLogonSite();
int   logonPort   =   443;
Protocol   myhttps   =   new   Protocol( "https ",
new   MySecureProtocolSocketFactory(),   logonPort);
Protocol.registerProtocol( "https ",   myhttps);

firstMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new   DefaultHttpMethodRetryHandler());
String   username   =   "admin ";
String   pwd   =   "firewall ";
NameValuePair   userid   =   new   NameValuePair( "username ",   username);
NameValuePair   password   =   new   NameValuePair( "password ",   pwd);
firstMethod.setRequestBody(new   NameValuePair[]   {   userid,   password   });


}

private   void   processLogon(HttpClient   client,   PostMethod   post)   {
try   {
client.executeMethod(post);
//System.out.println(post.getResponseBodyAsString());
}   catch   (HttpException   e)   {
e.printStackTrace();
}   catch   (IOException   e)   {
e.printStackTrace();
}   finally   {
post.releaseConnection();
}
}

private   String   getLogonSite(){
String   logonSite   =   "https://192.168.29.25/login.process.do ";
return   logonSite;
}

}



[解决办法]
struts的配置问题
你让他登陆成功之后的forword转到操作页面去
[解决办法]
vvvvv
[解决办法]
有可能是session 的问题,你手工登录下,分析下登录成功的页面的session(一般就是用cookie实现的)里面都包括些什么信息

热点排行