You are not logged in.

#1 2013-01-23 14:05:46

iduine
Member
From: Rennes - France
Registered: 2012-11-18
Posts: 6

[SOLVED] JAVA, servlet and Cookies

Hi everyone! I've a little issue with Cookies... I'm not sur how to use it with JAVA.

I explain :

I've to read an math equation on a website (for a challenge), and use it to return the solution on an other page.
But the website is requesting athentication, with a cookie (i know witch one, and where it is.)

I've already done a part of the code, just to read a web page :

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

public class Main {
    public static String getHTML() {
        URL url;
        HttpURLConnection conn;
        BufferedReader rd;
        String line;
        String result = "";
        try {
            url = new URL("http://.......................YourURLHere......................");
            conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            while ((line = rd.readLine()) != null) {
                result += line;
            }
            rd.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    public static void main(String[] args) {
        System.out.println(getHTML());
    }
}

I know there's method like

void AddCookie(Cookie myCookie)

But i'm not sure that's the one i should use, because apparently it must be use with the object HttpServletResponse.

So if anyone could help me, i keep searching on the web, thanks!


PS : sorry if my english isn't so good! >.<


EDIT : Okay so that was the setRequestProperty(String name, String key) method. I let it there, could be usefull!

Last edited by iduine (2013-01-23 14:24:25)

Offline

Board footer

Powered by FluxBB