JPHamlett
JPHamlett

Reputation: 365

How do I send a cookie while trying to grab a sites source?

I am trying to grab a site's source code using this code

private static String getUrlSource(String url) throws IOException {
        URL url = new URL(url);
        URLConnection urlConn = url.openConnection();
        BufferedReader in = new BufferedReader(new InputStreamReader(
                urlConn.getInputStream(), "UTF-8"));
        String inputLine;
        StringBuilder a = new StringBuilder();
        while ((inputLine = in.readLine()) != null)
            a.append(inputLine);
        in.close();

        return a.toString();
    }

When I do grab the site code this way I get an error about needing to allow cookies. Is there anyway to allow cookies in a java application just so I can grab some source code? I do have the cookie my browser uses to log me in if that helps.

Thanks John

Upvotes: 1

Views: 23

Answers (1)

Jigar Joshi
Jigar Joshi

Reputation: 240860

This way you would have to deal with raw request data, Go with apache http client that gives you abstraction and some methods to allow to set headers in request

Upvotes: 1

Related Questions