同样的代码,在main下能运行,在struts2的action中不能正常运行
我有个java类,在main下能正常运行
这个带main方法的类已经确定能够正常运行
在struts2的action中
package com.haozhong.recharge.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.haozhong.recharge.bean.recharge.LianLianInterface;
/**
*
*/
public class Lianlian {
private MyHttp http = new MyHttp();
public String getCheckCode(String path){
//始初化验证码变量
String code = null;
for(int i = 0; i < 10; i++)
{
//图片的网址
String imgUrl = "http://www.lianlian.com/servlet/CheckCodeServlet";
//图片的本地路径
String imgSaveUrl = path+"/temp.jpg";
http.saveImg(imgUrl, imgSaveUrl);
code = unCode(imgSaveUrl);
if(code != null && code.equals("null") && code.length() == 4)
{
break;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("得到破解的验证码为="+code);
return code ;
}
public String login(String path ,LianLianInterface lianLianInterface) throws InterruptedException{
try {
String url = "http://www.lianlian.com/login";
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("userName",lianLianInterface.getLoginName()));
nvps.add(new BasicNameValuePair("checkCode", getCheckCode(path)));
nvps.add(new BasicNameValuePair("password", lianLianInterface.getPassword()));
String outdata = http.post(url, nvps);
if ( outdata.indexOf("短信验证码必须是") >= 0)
{
System.out.println("检测到返回的html代码中含有<短信码>字样,登陆成功");
}
else
{
System.out.println("检测到返回的html代码中没有含有<短信码>字样,登陆失败," +
"下面是返回的html代码,请查看失败原因");
System.out.println("返回的网址html字符="+outdata);
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null ;
}
public String getCookie(String path,LianLianInterface lianLianInterface ){
try {
String outdata = login(path,lianLianInterface);
Pattern p = Pattern.compile("http(.)+?LLISP_ZJ/ISPSimulateLogin.do\\?params=(.)+?\"");
String url = "" ;
Matcher m = p.matcher(outdata);
if(m.find())
{
url = m.group();
url = url.substring(0, url.length()-1);
}
System.out.println("正则后的URL="+url);
http.get(url);
String cookies = http.getCookie( "llagtzj.lianlian.com.cn");
System.out.println("***************************");
System.out.println(cookies);
return cookies ;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null ;
}
public static void main(String[] args) throws Exception {
LianLianInterface lianLianInterface = new LianLianInterface();
lianLianInterface.setLoginName("15988641192");
lianLianInterface.setPassword("100860");
//ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext*.xml");
String path = "c:";
new Lianlian().getCookie(path,lianLianInterface);
}
/**
* 根据传入的图片本地路径破解验证码
* @param imgSaveUrl
* @return
*/
public String unCode(String imgSaveUrl)
{
String code = "";
try {
code = OCRUtil.recognizeValidation(imgSaveUrl);
//doSoming
//请完善破解验证码的代码
} catch (Exception e) {
// TODO: handle exception
}
return code;
}
public MyHttp getHttp() {
return http;
}
public void setHttp(MyHttp http) {
this.http = http;
}
}
这个带main方法的类已经确定能够正常运行
在struts2的action中
public String saveCookies() throws InterruptedException{
String path = "c:";
LianLianInterface lianLianInterface = new LianLianInterface();
lianLianInterface = lianLianInterfaceService.findByStatus();
String cookie = new Lianlian().getCookie(path,lianLianInterface);
lianLianInterface.setJsessionid(cookie.split("=")[1]);
rechargeService.updateLianLianInterface(lianLianInterface);
request.setAttribute("cookie", cookie);
return "saveCookies" ;
}
...全文
相关推荐
测试struts2中action
测试struts2中的action
关于struts2中Action名字的大小写问题浅谈
主要给大家介绍了关于struts2中Action名字大小写问题的相关资料,文中介绍的非常详细,对大家具有一定的参考学习价值,需要的朋友们下面跟着小编一起来学习学习吧。
Struts2框架单元测试代码
Struts2框架单元测试代码 一个完整的Eclipse工程
struts2源代码分析
struts2源代码分析struts2源代码分析struts2源代码分析struts2源代码分析struts2源代码分析struts2源代码分析struts2源代码分析struts2源代码分析
maven-web项目 Struts2 Struts注解 Struts2返回json
maven-web项目 Struts2 Struts注解 Struts2返回json


