get Page by encoding - Java Network

Java examples for Network:Http

Description

get Page by encoding

Demo Code


//package com.java2s;

import java.io.*;
import java.net.*;

public class Main {
    public static String getPage(String url, String encoding)
            throws IOException {
        return (getPage(url, encoding, null));
    }//from  w  w w  . ja v a  2s . c  o  m

    public static String getPage(String url, String encoding, String cookies)
            throws IOException {
        URL u = new URL(url);
        HttpURLConnection conn = (HttpURLConnection) u.openConnection();
        if (cookies != null)
            conn.setRequestProperty("Cookie", cookies);
        if (conn.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND)
            return (null);
        BufferedReader stream = new BufferedReader(new InputStreamReader(
                conn.getInputStream(), encoding));

        String string = "", line;

        while ((line = stream.readLine()) != null)
            string += line;

        stream.close();
        return (string);
    }
}

Related Tutorials