日期:2014-05-20 浏览次数:20739 次
String currname = ""; //提交的用户名 String currpwd = ""; //提交的秘密 //post的数据,其中的Username 和 Password 是表单上input的name属性名,大小写必须一致 String postdata = "Username=" + URLEncoder.encode(currname,"utf-8") +"&Password=" + URLEncoder.encode(currpwd, "utf-8"); URL url = new URL("http://localhost/xxx/names.nsf?Login"); //提交的地址 //STEP.1.建立连接 HttpURLConnection con = (HttpURLConnection) url.openConnection(); con.setRequestMethod("POST"); //设置方法为Post //STEP.2.设置Cookie 根据你的js做的 con.setRequestProperty("Cookie", "A="+currname+"; B="+currpwd+"; LastVisitUserName=" + currname +";"); //STEP.3.发送post数据 BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(con.getOutputStream())); writer.write(postdata); writer.flush(); writer.close(); //STEP.4.获取返回数据 BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream())); String line; while ((line = reader.readLine()) != null) { System.out.println(line); } reader.close(); con.disconnect();