转载自:
这个也是转载,这一点的话我也需要在找到可以测试的网址之后进行测试完毕确认可以之后,再写入我的工具类中去
URL url = new URL("网页"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoOutput(true);// 允许连接提交信息 connection.setRequestMethod("POST");// 网页提交方式“GET”、“POST” String content = "username=admin&password=admin"; connection.setRequestProperty("Cookie", responseCookie);// 有网站需要将当前的session id一并上传 OutputStream os = connection.getOutputStream(); os.write(content.toString().getBytes("GBK")); os.close(); BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream())); String responseCookie = connection.getHeaderField("Set-Cookie");// 取到所用的Cookie String sessionIdString = ""; if (responseCookie != null) { sessionIdString = responseCookie.substring(0, responseCookie.indexOf(";")); } // 输出内容 String line = br.readLine(); while (line != null) { System.out.println(line); line = br.readLine(); } // access URL url1 = new URL("网页的登录后的页面"); HttpURLConnection connection1 = (HttpURLConnection) url1.openConnection(); connection1.setRequestProperty("Cookie", responseCookie);// 给服务器送登录后的cookie BufferedReader br1 = new BufferedReader(new InputStreamReader(connection1.getInputStream())); String line1 = br1.readLine(); while (line1 != null) { // TODO:操作 }