close
Java 客戶端
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
public class NewClass {
public static void main(String[] args) throws Exception {
URL url = new URL("http://127.0.0.1/CookieTest");
URLConnection conn = url.openConnection();
// 寫入 Cookie
conn.setRequestProperty("Cookie", "loli=Moe;who=God;");
// 連線
conn.setDoInput(true);
conn.setDoOutput(true);
conn.connect();
//
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write("e_mail=my@loli.com&password=yayaya"); // 輸入信箱與密碼
wr.flush();
//-----------------------------------------------------
System.out.println("------------- Server 回傳標頭 -------------------------------");
//get all headers
Map<String, List<String>> map = conn.getHeaderFields();
for (Map.Entry<String, List<String>> entry : map.entrySet()) {
System.out.println("Key : " + entry.getKey() + " ,Value : " + entry.getValue());
}
System.out.println("------------- 伺服器種類 ---------------");
//get header by 'key'
String server = conn.getHeaderField("Server");
System.out.println(server);
System.out.println("------------- 兩種 Cookie ---------------");
HttpURLConnection httpConn = (HttpURLConnection) conn; // 回傳使用
String cookie = conn.getRequestProperty("Cookie"); // 客戶端原先的 Cookie
String requestCookie = conn.getHeaderField("Set-Cookie"); // 伺服器要求新寫入的
System.out.println("Cookies : " + cookie);
System.out.println("Request Cookies : " + requestCookie);
System.out.println("------------- Server 回傳訊息 ---------------");
//-----------------------------------------------------
try (InputStreamReader in = new InputStreamReader(httpConn.getInputStream());
BufferedReader br = new BufferedReader(in);) {
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
}
}
}
//-----------------------------------------------------------------------------------------------------
Web 伺服器 Servlet ( 例如 : Tomcat )
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/CookieTest")
public class CookieTest extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/plain;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
String e_mail = request.getParameter("e_mail");
String password = request.getParameter("password");
if (e_mail.equals("my@loli.com") && password.equals("yayaya")) {
int cookieAge = 24 * 60 * 60; // 1 Day
Cookie status = new Cookie("status", "You_Already_To_Sign");
status.setMaxAge(cookieAge);
response.addCookie(status);
out.println("登入成功 !");
out.println();
} else {
out.println("登入失敗 ! 請檢查信箱與祕法是否輸入錯誤 !?");
out.println();
}
for (Cookie c : request.getCookies()) {
out.println("Name : " + c.getName());
out.println("Value : " + c.getValue());
out.println();
}
}
}
}
//-----------------------------------------------------------------------------
Java 客戶端執行結果
------------- Server 回傳標頭 -------------------------------
Key : null ,Value : [HTTP/1.1 200 OK]
Key : Server ,Value : [Apache-Coyote/1.1]
Key : Set-Cookie ,Value : [status=You_Already_To_Sign; Expires=Tue, 27-Oct-2015 18:02:37 GMT]
Key : Content-Length ,Value : [66]
Key : Date ,Value : [Mon, 26 Oct 2015 18:02:37 GMT]
Key : Content-Type ,Value : [text/plain;charset=UTF-8]
------------- 伺服器種類 ---------------
Apache-Coyote/1.1
------------- 兩種 Cookie ---------------
Cookies : loli=Moe;who=God;
Request Cookies : status=You_Already_To_Sign; Expires=Tue, 27-Oct-2015 18:02:37 GMT
------------- Server 回傳訊息 ---------------
登入成功 !
Name : loli
Value : Moe
Name : who
Value : God
全站熱搜
留言列表