模拟浏览器发送http请求(偷懒篇)

         OutputStreamWriter out = null;
		BufferedReader in = null;
		InputStreamReader isr = null;
		FileWriter fileWriter = null;
		HttpsURLConnection conn = null;
		try {
			URL realUrl = new URL(dataUrl);
			
			conn = (HttpsURLConnection) realUrl.openConnection();
	        conn.setSSLSocketFactory(new TCITLSSocketConnectionFactory());

		    conn.setRequestMethod("POST");
			conn.setDoOutput(true);
			conn.setDoInput(true);
			conn.setUseCaches(false);

			conn.setConnectTimeout(180000);
			conn.setReadTimeout(180000);
	        conn.setRequestProperty("accept", "*/*");
	        conn.setRequestProperty("connection", "Keep-Alive");
	        conn.setRequestProperty("Host", "apps.tampagov.net");
	        conn.setRequestProperty("Referer", "https://apps.tampagov.net/CallsForService_Webapp/Default.aspx?type=TPD");
	        conn.setRequestProperty("Origin", "https://apps.tampagov.net");
	        conn.setRequestProperty("Cookie", "ASP.NET_SessionId=4pz44jsikfvfeu5jgzjeynsz; _ga=GA1.2.969372624.1587627191; __utma=51000313.969372624.1587627191.1587693225.1587693225.1; __utmc=51000313; __utmz=51000313.1587693225.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); ASPSESSIONIDSUTRTQSB=ONMDEOBBKJEFIMBLLJJEPNFL; ASPSESSIONIDQUSQQSSD=FJFFFPBBKPLGBGHPLMMBKHAE; _gid=GA1.2.354408890.1587970939; _gat_gtag_UA_52588819_1=1");
	        conn.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.129 Safari/537.36");
	        conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");

//	        conn.setRequestProperty("Content-Type", "application/x-excel");
			conn.setRequestProperty("Sec-Fetch-Dest", "document");
			conn.setRequestProperty("Sec-Fetch-Mode", "navigate");
			conn.setRequestProperty("Sec-Fetch-Site", "Sec-Fetch-Site");
			conn.setRequestProperty("Sec-Fetch-User", "?1");
			conn.setRequestProperty("Upgrade-Insecure-Requests", "1");
			conn.setRequestProperty("Pragma", "no-cache");
			conn.setRequestProperty("Cache-Control", "no-cache");
			conn.setRequestProperty("Content-Length", "40550");
			

	        Map map = new HashMap();
		  	map.put("__VIEWSTATE", "lHP0yp6Wmg73330ijCYTUVeS/hiZKn0lCm60G3t8qfS8/przGSRoYFpSkwDGXZCmpDkmF6isrIm7LBIsaN9t");
		    map.put("ctl00$MainContent$btndata", "Export to Excel");
		    map.put("__EVENTVALIDATION", "2ZxxriHSoAEU04Tl/4Ta8B+44gZKQ0w/Reo1oqRuER4MxnDqZDjJ+tBnPvABHHkO1XdWBEHhjoqQftKSL3ezi");
	    
	    	StringBuffer buffer = new StringBuffer();
		    for(String key : map.keySet()) {
			    buffer.append("------WebKitFormBoundary7MA4YWxkTrZu0gW\r\n");
			    buffer.append("Content-Disposition: form-data; name=\"");
			    buffer.append(key);
			    buffer.append("\"\r\n\r\n");
			    buffer.append(map.get(key));
			    buffer.append("\r\n");
		   }
		   if(map != null)
			   buffer.append("------WebKitFormBoundary7MA4YWxkTrZu0gW--\r\n");

//		    conn.connect();
			out = new OutputStreamWriter(conn.getOutputStream());
			out.write(buffer.toString());
			out.flush();
			if(conn.getResponseCode() == 200){
				isr = new InputStreamReader(conn.getInputStream());
				in = new BufferedReader(isr);
				String line;
				fileWriter = new FileWriter(xmlFile);
			    while ((line = in.readLine()) != null) {
			    	 fileWriter.write(line);
				}
			    fileWriter.flush();
			 	if (fileWriter != null) {
			 	    fileWriter.close();
			 	}
			 	if (isr != null) {
			 		isr.close();
			 	}
			 	if (in != null) {
			 		in.close();
			 	}
			}else{
				LOGGER.info("Connection failed"); 
				return;
			}
		
		} catch (Exception e) {
		   e.printStackTrace();
     	}finally{
             
	    }

模拟浏览器发送http请求(偷懒篇)_第1张图片

点进去其实是这样的 

模拟浏览器发送http请求(偷懒篇)_第2张图片

 但其实存在一个导出表格按钮的,如果点导出表格,会变成post请求,然后可以保存导出的文件

模拟浏览器发送http请求(偷懒篇)_第3张图片

其实成功拿到数据是用postman试出来的,postman拿到了数据之后我再根据post写得这个,比如map三个属性是不能缺的。。其实有点死的感觉,不过也没办法,能拿到数据就不错了。。纪念一下,只是为了方便我下次能直接搬代码。。。

你可能感兴趣的:(码农之路)