08-12
05
重写URL时候,Session失效解决办法
作者:Java伴侣 日期:2008-12-05
我目的是把JSP页面转换成静态的HTML,url.openConnection()中的url是我项目中的JSP文件的路径,由于项目的页面有验证session是否失效,所以当url.openConnection()打开的页面时候session是空的,处理Session:ts.setRequestProperty("Cookie", "JSESSIONID=" + sessionId);request.getSession().getId()
代码如下:
代码如下:
复制内容到剪贴板 程序代码
public void getHtmlReport(String[] urlList,String protocol,String host,String sessionId ){
try
{
if(urlList.length < 1)
{
log.debug("Use: java TestSocket 所要转换的页面的URL [转换后要保存的文件名]");
//System.out.println("Use: java TestSocket 所要转换的页面的URL [转换后要保存的文件名]");
return;
}
String urlString = urlList[0];
URL url = new URL(urlString);
HttpURLConnection ts = (HttpURLConnection)url.openConnection();
ts.setRequestProperty("Cookie", "JSESSIONID=" + sessionId); //我从request中获得的sessionID ;request.getSession().getId()
ts.setRequestMethod("GET");
ts.setRequestProperty("Connection", "Keep-Alive");
ts.setRequestProperty("User-Agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)");
ts.setDoOutput(true);
ts.setDoInput(true);
ts.setAllowUserInteraction(false);
ts.connect();
InputStream socketInput = ts.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(socketInput));
String fileName = url.getContent().toString() + ".html";
if (urlList.length > 1)
{
fileName = urlList[1];
}
log.debug("to:"+fileName);
PrintWriter file = new PrintWriter(new FileWriter(fileName), true);
while (true)
{
try
{
String s = in.readLine();
if (s == null)
{
break;
}
file.print(s);
}
catch (Exception e)
{
System.out.println(e);
break;
}
}
file.close();
log.debug("转换完成");
//System.out.println("转换完成");
}
catch(Exception e)
{
log.debug(e);
//System.out.println(e);
}
}
}
try
{
if(urlList.length < 1)
{
log.debug("Use: java TestSocket 所要转换的页面的URL [转换后要保存的文件名]");
//System.out.println("Use: java TestSocket 所要转换的页面的URL [转换后要保存的文件名]");
return;
}
String urlString = urlList[0];
URL url = new URL(urlString);
HttpURLConnection ts = (HttpURLConnection)url.openConnection();
ts.setRequestProperty("Cookie", "JSESSIONID=" + sessionId); //我从request中获得的sessionID ;request.getSession().getId()
ts.setRequestMethod("GET");
ts.setRequestProperty("Connection", "Keep-Alive");
ts.setRequestProperty("User-Agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)");
ts.setDoOutput(true);
ts.setDoInput(true);
ts.setAllowUserInteraction(false);
ts.connect();
InputStream socketInput = ts.getInputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(socketInput));
String fileName = url.getContent().toString() + ".html";
if (urlList.length > 1)
{
fileName = urlList[1];
}
log.debug("to:"+fileName);
PrintWriter file = new PrintWriter(new FileWriter(fileName), true);
while (true)
{
try
{
String s = in.readLine();
if (s == null)
{
break;
}
file.print(s);
}
catch (Exception e)
{
System.out.println(e);
break;
}
}
file.close();
log.debug("转换完成");
//System.out.println("转换完成");
}
catch(Exception e)
{
log.debug(e);
//System.out.println(e);
}
}
}
评论: 0 | 引用: 0 | 查看次数: 697
发表评论