About verification code login (1)


When crawling some websites, some require a login to gain access. If you just need to log in, here is a tool that you can recommend, it's very useful

There is a plug-in firebug (need to be installed) in Firefox browsing. Through this plug-in, you can view the website access process (link jump and access sequence) in detail, as well as the request header information and response header information of each link. You can view the data submitted by the post. Of course, there are some development tools in IE and Google browsers, which can be called directly by F12, but I personally feel that Firefox's firebug is easier to use, and I occasionally use IE's and Google's.

It is very easy to obtain the detailed process of the simulation through the tools described above, and then simulate the login.

Here I am introducing that if you need a verification code to log in, it will be a little troublesome. I think of a solution here, which is more commonly used, that is, to pop up the verification code

The implementation is as follows, simulating login

public class LoginByCode {
	
	public static void main(String[] args) {
		CloseableHttpClient httpClient = HttpClientBuilder.create().build();
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddhhmmss");
		String path = "d:/img/tmp/" + format.format(new Date()) + ".jpg";
		try {
			String imgurl = "http://www.shanghaiip.cn/wasWeb/login/Random.jsp";
			HttpUriRequest get = new HttpGet(imgurl);
			HttpResponse res = httpClient.execute(get);
			res.setHeader("Content-Type", "image/gif");
			byte[] img = EntityUtils.toByteArray(res.getEntity());//下载验证码图片
			saveFile(path, img);
			String code = new ImgDialog().showDialog(null, path);//弹出验证码,获取填写验证码
			String login = "http://www.shanghaiip.cn/wasWeb/login/loginServer.jsp";
			HttpPost post = new HttpPost(login);
			List<NameValuePair> data = new ArrayList<NameValuePair>();
			data.add(new BasicNameValuePair("username", "zhpatent"));
			data.add(new BasicNameValuePair("password", "5ca072839350b0733a2a456cc4004371"));//火狐里面用firebug可以查看密码是加密后的
			data.add(new BasicNameValuePair("newrandom", code));
			post.setEntity(new UrlEncodedFormEntity(data));
			res = httpClient.execute(post);
			Header[] headers = res.getHeaders("Location");//获取跳转链接
			get = new HttpGet(headers[0].getValue());
			res = httpClient.execute(get);
			String body = EntityUtils.toString(res.getEntity());
			if (body.contains("zhpatent")) {
				System.out.println("模拟登录成功:" + body.substring(body.indexOf("zhpatent") - 40, body.indexOf("zhpatent") + 40));
			}
		} catch (Exception e) {
			System.out.println("异常:" + e.getMessage());
		} finally {
			File file = new File(path);
			if (file.exists()) {
				file.delete();
			}
			try {
				httpClient.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	private static void saveFile(String path, byte[] data) {
		int size = 0;
		byte[] buffer = new byte[10240];
		try (BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(path));
			ByteArrayInputStream is = new ByteArrayInputStream(data)) {
			while ((size = is.read(buffer)) != -1) {
				bos.write(buffer, 0, size);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}


CAPTCHA TOOLS

public class ImgDialog {
	
	public String message = null;
	private JButton confirm;
	private JDialog dialog = null;
	private TextField field;
	String result = "";

	public String showDialog(JFrame father, String path) {
		JLabel label = new JLabel();
		label.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		label.setBounds(10, 10, 125, 51);
		label.setIcon(new ImageIcon(path));
		
		field = new TextField();
		field.setBounds(145, 10, 65, 20);
		
		confirm = new JButton("确定");
		confirm.setBounds(145, 40, 65, 20);
		confirm.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				result = field.getText();
				ImgDialog.this.dialog.dispose();
			}
		});
		
		dialog = new JDialog(father, true);
		dialog.setTitle("请输入图片中的验证码");
		Container pane = dialog.getContentPane();
		pane.setLayout(null);
		pane.add(label);
		pane.add(field);
		pane.add(confirm);
		dialog.pack();
		dialog.setSize(new Dimension(235, 110));
		dialog.setLocation(750, 430);
//		dialog.setLocationRelativeTo(father);
		dialog.setVisible(true);
		return result;
	}
}


The experimental results are as follows

Running will download the verification code and pop up

Enter the verification code and get my user information on the page jumped after logging in.

I use httpclient to simulate login here. httpclient does not need to manage cookies, so it is convenient to use, and there will be no problem that the verification code does not match the number.

If you use Jsoup to simulate login, it will be a little more troublesome. You have to manage cookies yourself. When you visit the verification code page, you must download the verification code and get the cookies at the same time, and then you need to bring the cookies when you simulate the login.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325915601&siteId=291194637