public static void loginBai() {
URL url = null;
HttpURLConnection httpurlconnection = null;
try {
url = new URL("http://www..com/");
httpurlconnection = (HttpURLConnection) url.openConnection();
httpurlconnection.setRequestProperty("User-Agent",
"Internet Explorer");
httpurlconnection.setRequestProperty("Host", "www..com");
httpurlconnection.connect();
String cookie0 = httpurlconnection.getHeaderField("Set-Cookie");
System.out.println(cookie0);//列印出cookie
httpurlconnection.disconnect();
// String cookie0 =
// "BAIDUID=:FG=1;BDSTAT=;
// BDUSE=deleted";
url = new URL("http://passport..com/?login");
String strPost = "username=xxxxxx&password=yyyyyyy&mem_pass=on";
httpurlconnection = (HttpURLConnection) url.openConnection();
httpurlconnection.setFollowRedirects(true);
httpurlconnection.setInstanceFollowRedirects(true);
httpurlconnection.setDoOutput(true); // 需要向伺服器寫數據
httpurlconnection.setDoInput(true); //
httpurlconnection.setUseCaches(false); // 獲得伺服器最新的信息
httpurlconnection.setAllowUserInteraction(false);
httpurlconnection.setRequestMethod("POST");
httpurlconnection
.addRequestProperty(
"Accept",
"image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/msword, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/x-silverlight, */*");
httpurlconnection
.setRequestProperty("Referer",
"http://passport..com/?login&tpl=mn&u=http%3A//www..com/");
httpurlconnection.setRequestProperty("Accept-Language", "zh-cn");
httpurlconnection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
httpurlconnection.setRequestProperty("Accept-Encoding",
"gzip, deflate");
httpurlconnection
.setRequestProperty(
"User-Agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Foxy/1; .NET CLR 2.0.50727;MEGAUPLOAD 1.0)");
httpurlconnection.setRequestProperty("Host", "passport..com");
httpurlconnection.setRequestProperty("Content-Length", strPost
.length()
+ "");
httpurlconnection.setRequestProperty("Connection", "Keep-Alive");
httpurlconnection.setRequestProperty("Cache-Control", "no-cache");
httpurlconnection.setRequestProperty("Cookie", cookie0);
httpurlconnection.getOutputStream().write(strPost.getBytes());
httpurlconnection.getOutputStream().flush();
httpurlconnection.getOutputStream().close();
httpurlconnection.connect();
int code = httpurlconnection.getResponseCode();
System.out.println("code " + code);
String cookie1 = httpurlconnection.getHeaderField("Set-Cookie");
System.out.print(cookie0 + "; " + cookie1);
httpurlconnection.disconnect();
url = new URL("http://www..com/");
httpurlconnection = (HttpURLConnection) url.openConnection();
httpurlconnection.setRequestProperty("User-Agent",
"Internet Explorer");
httpurlconnection.setRequestProperty("Host", "www..com");
httpurlconnection.setRequestProperty("Cookie", cookie0 + "; "
+ cookie1);
httpurlconnection.connect();
InputStream urlStream = httpurlconnection.getInputStream();
BufferedInputStream buff = new BufferedInputStream(urlStream);
Reader r = new InputStreamReader(buff, "gbk");
BufferedReader br = new BufferedReader(r);
StringBuffer strHtml = new StringBuffer("");
String strLine = null;
while ((strLine = br.readLine()) != null) {
strHtml.append(strLine + "\r\n");
}
System.out.print(strHtml.toString());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (httpurlconnection != null)
httpurlconnection.disconnect();
}
}
呵呵,改了下程序中錯誤的地方。學習了,原來這樣也可以。
以前只知道可以這樣做,還從來沒有花過心思去做過。
閱讀了些代碼,真是獲益匪淺啊。
程序可以運行了,用戶名和密碼改為你的帳戶就可以直接運行了。可以使用,但不能一直使用啊,如果用多了網路給你個驗證碼,就是神仙也不行了。
B. java程序實現登陸。用戶沒有登錄但訪問非登錄的頁面,使用過濾器跳轉到登錄頁,登錄後怎麼回到訪問頁
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest)request;
if(req.getSession().getAttribute("user")==null){
req.setAttribute("error", "請你先登錄");
//非法請求才會進到這裡面,在這里保存請求的url地址,在成功登錄後再進行跳轉
req.getSession().setAttribute("goUrl", req.getRequestURL()+"?"+ req.getQueryString());
request.getRequestDispatcher("/ulogin.jsp").forward(request, response);
}
else{
chain.doFilter(request, response);
}
}
上面的代碼是過濾器中的代碼
下面的是servlet中的代碼
if(request.getSession().getAttribute("goUrl")!=null){
String url = (String)request.getSession().getAttribute("goUrl");
response.sendRedirect(url);
}
else{
response.sendRedirect("/webshopping/index.jsp");
}
C. javaweb如何實現用戶在一個頁面長時間不操作自動跳轉到登錄頁面
設置Session超時時間,超時時長內,如果用戶沒有任何操作,Session就會自動過期.你在代碼里先取Session,如果沒有取到,就跳轉到登錄頁面即可.
D. java實現注冊成功自動登錄
注冊頁面-->驗證注冊成功後跳到action,把存儲資料庫的那條數據取出來,放到session里,就已經成功登陸了,哪個頁面需要用到用戶信息,直接從session里取值