是想实现模拟登录,带验证码的,在本地已经实现登录成功了,本地实现的思路是把验证码图片保存在C:\1.jpg ,然后启动程序,再在命令行手动输入验证码登录。
请问如何把这种程序发布成一个web页面,该怎么写
下面是本地跑的代码
public class Login {
static String cookie = "ASP.NET_SessionId=t5lgai45j5h1v0au5cddp055" ;
static HttpClient hc = new DefaultHttpClient();
public static void main(String[] args) throws Exception{
loginjw();
}
public static void loginjw() throws Exception {
String __VIEWSTATE = "********";
String typeName = "***";
String url = "http://www.***.com/***.aspx";
//获取验证码
byte[] temp = getValidateCode();
byte2image(temp);
//手动输入验证码
Scanner sca=new Scanner(System.in);
String cCode=sca.next();
String yzm=sca.next();
List<NameValuePair> nvps2 = new ArrayList<NameValuePair>();
nvps2.add(new BasicNameValuePair("__VIEWSTATE", __VIEWSTATE));
nvps2.add(new BasicNameValuePair("typeNmae",typeName));
nvps2.add(new BasicNameValuePair("id","123"));
nvps2.add(new BasicNameValuePair("psw", "123"));
nvps2.add(new BasicNameValuePair("cCode", cCode));
Login(url,nvps2);
}
public static void Login(String url, List<NameValuePair> params) {
String body = null;
int htmlCode = 1;
try {
// Post请求
HttpPost httppost = new HttpPost(url);
httppost.addHeader("cookie", cookie);
httppost.addHeader("User-Agent","Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko");
httppost.addHeader(new BasicHeader("Referer","http://www.asd.com"));
// 设置参数
httppost.setEntity(new UrlEncodedFormEntity(params));
// 发送请求
HttpResponse httpresponse = hc.execute(httppost);
HttpEntity entity = httpresponse.getEntity();
System.out.println(httpresponse.getAllHeaders().toString());
System.out.println("Response Line: " + httpresponse.getStatusLine());
htmlCode = httpresponse.getStatusLine().getStatusCode();
body=EntityUtils.toString(entity);
EntityUtils.consume(entity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(body);
}
//get验证码
public static byte[] getValidateCode() {
byte[] cCode = null ;
try {
// Get请求
HttpGet httpget = new HttpGet("http://www.***.com/ValidateCode.aspx");
httpget.addHeader("Cookie", cookie);
httpget.addHeader(new BasicHeader("Referer","http://www.**.com/***.aspx"));
HttpResponse httpresponse = hc.execute(httpget);
HttpEntity entity = httpresponse.getEntity();
cCode = EntityUtils.toByteArray(entity);
Header[] headers = httpget.getAllHeaders();
for(int i=0;i<headers.length;i++){
System.out.println("httpget-getallheaders-----"+headers[i]);
}
Header[] headers2 = httpresponse.getAllHeaders();
for(int i=0;i<headers2.length;i++){
System.out.println("httpgetresponse-getallheaders-----"+headers2[i]);
}
if (entity != null) {
entity.consumeContent();
}
} catch (ParseException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return cCode;
}
//把验证码输出到本地文件
public static void byte2image(byte[] data){
String path ="C:/1.jpg";
if(data.length<3||path.equals("")) return;
try{
FileImageOutputStream imageOutput = new FileImageOutputStream(new File(path));
imageOutput.write(data, 0, data.length);
imageOutput.close();
System.out.println("Make Picture success,Please find image in " + path);
} catch(Exception ex) {
System.out.println("Exception: " + ex);
ex.printStackTrace();
}
}
}
------解决思路----------------------
你做个servlet 请求调用你上面写的方法。