我想用Java“应用程序”获得一个"动态网页"(例如:*.php)的内容
但是这个"动态网页",需要获取并验证用户"Cookie"。虽然可以用Socket的手动的编写Http协议并发送Cookie但是采用这样的方式,确实有些麻烦。。请问有没有现成的Java类或项目,能实现这个功能?

解决方案 »

  1.   

    从Servlet 中获取Cookie 信息的例子。 ShowCookies.java 的源代码如下: import javax.servlet.*; 
    import javax.servlet.http.*; /** 
    * <p>This is a simple servlet that displays all of the 
    * Cookies present in the request 
    */ public class ShowCookies extends HttpServlet 

    public void doGet(HttpServletRequest req, HttpServletResponse resp) 
    throws ServletException, java.io.IOException 
    { // Set the content type of the response 
    resp.setContentType("text/html;charset=gb2312"); // Get the PrintWriter to write the response 
    java.io.PrintWriter out = resp.getWriter(); // Get an array containing all of the cookies 
    Cookie cookies[] = req.getCookies(); // Write the page header 
    out.println("<html>"); 
    out.println("<head>"); 
    out.println("<title>Servlet Cookie Information</title>"); 
    out.println("</head>"); 
    out.println("<body>"); if ((cookies == null) || (cookies.length == 0)) { 
    out.println("没有 cookies "); 

    else { 
    out.println("<center><h1>响应消息中的Cookies 信息 </h1>"); 
    // Display a table with all of the info 
    out.println("<table border>"); 
    out.println("<tr><th>Name</th><th>Value</th>" + "<th>Comment</th><th>Max Age</th></tr>"); 
    for (int i = 0; i < cookies.length; i++) { 
    Cookie c = cookies[i]; 
    out.println("<tr><td>" + c.getName() + "</td><td>" + 
    c.getValue() + "</td><td>" + c.getComment() + "</td><td>" + c.getMaxAge() + "</td></tr>"); 

    out.println("</table></center>"); 

    // Wrap up 
    out.println("</body>"); 
    out.println("</html>"); 
    out.flush(); 
    } /** 
    * <p>Initialize the servlet. This is called once when the 
    * servlet is loaded. It is guaranteed to complete before any 
    * requests are made to the servlet 
    * @param cfg Servlet configuration information 
    */ public void init(ServletConfig cfg) 
    throws ServletException 

    super.init(cfg); 
    } /** 
    * <p>Destroy the servlet. This is called once when the servlet 
    * is unloaded. 
    */ public void destroy() 

    super.destroy(); 


    看看可行不?好像开源的项目中还没有能实现这个功能的
      

  2.   

    对,你可以用httpclient来实现,我上次写了一个提取网页内容的程序,虽然没有用到象你说的需要cookie才能够实现,但是在httpclient中有类似的方法去解决
      

  3.   

    恩~~非常感谢 "jinxfei" 和 "xtbzqw" 程序写完了用的就是HttpClient 这个东西做 应用程序访问WEB 确实很实用HttpClient很不错哈~~