NutzCN Logo
短点 wenti_9a7c2b98
发布于 3083天前 作者 明天会吹什么风 1474 次浏览 复制 上一个帖子 下一个帖子
标签:

查看完整内容

import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class HttpServlet {

	String urlStr = "http://jw.zbvc.cn/st/login.aspx";
	// 使用GET方式提取参数
	
	public String[] getParmeters() throws Exception{
		
		URL url = new URL(urlStr);
		HttpURLConnection oc = (HttpURLConnection) url.openConnection();
		oc.setRequestMethod("GET");
		oc.setConnectTimeout(5000);
		
		StringBuffer sb = new StringBuffer();
		InputStream is = oc.getInputStream();
		int len =0;
		byte[] b = new byte[1024];
		while((len = is.read(b))!=-1){
			sb.append(new String(b, 0, len));
		}
		String htmlInfo = sb.toString();
		//System.out.println(htmlInfo);
		Pattern pattern = Pattern.compile("<input type=\"hidden\".*>");
		Pattern subPat = Pattern.compile("value=\".*\"");
		Matcher mat = pattern.matcher(htmlInfo);
		
		String[] strs = new String[3];
		int i=0;
		while(mat.find()){
			Matcher m =subPat.matcher(mat.group());
			if(m.find()){
				String info =m.group();
				strs[i++]=info.substring(info.indexOf("\"")+1, info.lastIndexOf("\""));
			}
		}
		
		return strs;
	}
	public static void main(String[] args) {

		try {
			String[] strs = new HttpServlet().getParmeters();
			
			String urlStr="__VIEWSTATE="+strs[0]
					+"&__VIEWSTATEGENERATOR="+strs[1]
					+"&__EVENTVALIDATION="+strs[2]
					+"&txt_卡学号=201404140001&txt_密码=123456&Button_登陆.x=55&Button_登陆.y=23&Rad_角色=学生";
			String enUrl = URLEncoder.encode(urlStr,"UTF-8");

			System.out.println(enUrl);
			URL url = new URL("http://jw.zbvc.cn/st/login.aspx");
			HttpURLConnection oc = (HttpURLConnection) url.openConnection();
			oc.setRequestMethod("POST");
			oc.setDoOutput(true);
			oc.setDoInput(true);
			oc.setConnectTimeout(5000);
			
			oc.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
			oc.setRequestProperty("Accept-Encoding", "gzip, deflate");
			oc.setRequestProperty("Accept-Language", "en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4");
			oc.setRequestProperty("Cache-Control", "max-age=0");
			oc.setRequestProperty("Connection", "keep-alive");
			oc.setRequestProperty("Content-Length", String.valueOf(enUrl.getBytes().length));
			oc.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
			oc.setRequestProperty("Host", "jw.zbvc.cn");
			oc.setRequestProperty("Origin", "http://jw.zbvc.cn");
			oc.setRequestProperty("Referer", "http://jw.zbvc.cn/st/login.aspx");
			oc.setRequestProperty("Upgrade-Insecure-Requests", "1");
			oc.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36");
			
			OutputStream os = oc.getOutputStream();
			os.write(enUrl.getBytes());
			os.flush();
			
//			InputStream is = oc.getInputStream();
//			int len =0;
//			byte[] b = new byte[1024];
//			while((len = is.read(b))!=-1){
//				System.out.println(new String(b, 0, len));
//			}
			System.out.println("-------------");
			Map<String, List<String>> header =  oc.getHeaderFields();
			for(String key:header.keySet()){
				//if(key!= null && key.equalsIgnoreCase("Location")){
					for(String str: header.get(key)){
						System.out.println(key+": "+str+"\n");
					}
//				}else{
//					System.out.println("key== null");
//				}
			}
			
			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

0 回复
添加回复
请先登陆
回到顶部