解析客户端的cookie

时间:2015-09-23 13:34:34

标签: java parsing servlets cookies

尝试访问我的DownloadDemo类中的cookie(在csv文件中下载网站上的信息),但似乎无法找到正确的方法。我的代码:

import java.io.*;
import java.net.URL;
import javax.servlet.*; 
import javax.servlet.http.*; 

public class DownloadDemo extends CookieTest
{


    public static void main(String[] args)
    {


        StringBuilder contents = new StringBuilder(4096);
        BufferedReader br = null;

        try
        { //goes to the given URL
            String downloadSite = ((args.length > 0) ? args[0] : "google.com");




            // file saved in your workspace
            String outputFile = ((args.length > 1) ? args[1] : "test.csv");
            URL url = new URL(downloadSite);
            InputStream is = url.openConnection().getInputStream();
            br = new BufferedReader(new InputStreamReader(is));
            PrintStream ps = new PrintStream(new FileOutputStream(outputFile));
            String line;
            String newline = System.getProperty("line.separator");
            while ((line = br.readLine()) != null)
            {
                contents.append(line).append(newline);
            }
            ps.println(contents.toString());
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            try { if (br != null) br.close(); } catch(IOException e) { e.printStackTrace(); }
        }
    }
}

Cookies课程:

import java.io.*;
import java.net.CookieHandler;
import java.net.CookieManager;
import java.net.CookiePolicy;
import java.net.CookieStore;
import java.net.HttpCookie;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;

import javax.servlet.*; 
import javax.servlet.http.*; 

public class CookieTest extends HttpServlet 
{     
    public void doGet(HttpServletRequest req, HttpServletResponse res) 
    throws ServletException, IOException 
        { 
            res.setContentType("text/html"); 
            PrintWriter out = res.getWriter(); 

            //Get the current session ID by searching the received cookies. 
            String cookieid = null; 
            Cookie[] cookies = req.getCookies(); 

            if (cookies != null) 
            { 
                for (int i = 0; i < cookies.length; i++) 
                { 
                    if (cookies[i].getName().equals("REMOTE_USER")) 
                    { 
                         cookieid = cookies[i].getValue(); 
                         break; 
                    } 
                } 
            } 
            System.out.println("Cookie Id--"+cookieid); 

            //If the session ID wasn't sent, generate one. 
            //Then be sure to send it to the client with the response. 

        } 


    //Gets the cookie

    public void getCookieUsingCookieHandler() { 
        try {       
            // Instantiate CookieManager;
            // make sure to set CookiePolicy
            CookieManager manager = new CookieManager();
            manager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
            CookieHandler.setDefault(manager);

            // get content from URLConnection;
            // cookies are set by web site
            URL url = new URL("https://google.com");
            URLConnection connection = url.openConnection();
            connection.getContent();

            // get cookies from underlying
            // CookieStore
            CookieStore cookieJar =  manager.getCookieStore();
            List <HttpCookie> cookies =
                cookieJar.getCookies();
            for (HttpCookie cookie: cookies) {
              System.out.println("CookieHandler retrieved cookie: " + cookie);
            }
        } catch(Exception e) {
            System.out.println("Unable to get cookie using CookieHandler");
            e.printStackTrace();
        }
    }  


    public void setCookieUsingCookieHandler() {
        try {
            // instantiate CookieManager
            CookieManager manager = new CookieManager();
            CookieHandler.setDefault(manager);
            CookieStore cookieJar =  manager.getCookieStore();

            // create cookie
            HttpCookie cookie = new HttpCookie("UserName", "John H");

            // add cookie to CookieStore for a
            // particular URL
            URL url = new URL("https://google.com");
            cookieJar.add(url.toURI(), cookie);
            System.out.println("Added cookie using cookie handler");
        } catch(Exception e) {
            System.out.println("Unable to set cookie using CookieHandler");
            e.printStackTrace();
        }
    }





}

我使用Cookie的原因是我需要访问用户凭据才能下载信息。

非常感谢提前。

0 个答案:

没有答案