導航:首頁 > 編程語言 > java自動登錄網站

java自動登錄網站

發布時間:2024-11-10 20:04:35

A. 如何用java實現登陸網站--不需打開瀏覽器

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里取值

閱讀全文

與java自動登錄網站相關的資料

熱點內容
vhd文件有什麼用 瀏覽:482
編程小朋友看什麼書 瀏覽:623
經營如何讓數據說話 瀏覽:258
如何在手機上升級opop 瀏覽:614
coreldrawx5免費視頻教程 瀏覽:725
網站引導頁面源碼 瀏覽:234
個人簡歷範文word 瀏覽:220
uc下載的視頻怎樣提取到文件 瀏覽:499
英雄下載下載最新版本2015下載安裝 瀏覽:433
NX深孔鑽編程替換面如何操作 瀏覽:725
手機怎麼刪除pdf文件 瀏覽:256
蘋果手機沒有efs文件夾怎麼辦 瀏覽:723
metro軟體在哪個文件夾 瀏覽:69
怎麼用手機登錄編程貓 瀏覽:400
文本md204顯示器如何編程 瀏覽:705
如何將表中重復數據標記 瀏覽:859
中級資料庫系統工程師應用技術考什麼 瀏覽:404
博途編程如何設置停止鍵 瀏覽:409
python3刪除文件內容 瀏覽:754
如何優化seo數據分析 瀏覽:132

友情鏈接