get Http URL Connection - Android Network

Android examples for Network:HTTP Get

Description

get Http URL Connection

Demo Code


//package com.java2s;

import java.io.IOException;

import java.net.HttpURLConnection;
import java.net.URL;

public class Main {
    /**//from  w w  w.j av  a2 s  . co  m
     * 
     * @param url
     * @param charsetName
     * @param connectTimeout
     * @param readTimeout
     * @return HttpURLConnection
     * @throws IOException
     */
    public static HttpURLConnection getHttpURLConnection(String url,
            String charsetName, int connectTimeout, int readTimeout)
            throws IOException {

        HttpURLConnection httpConnection = (HttpURLConnection) new URL(url)
                .openConnection();
        httpConnection.setConnectTimeout(connectTimeout);
        httpConnection.setDoInput(true);
        httpConnection.setDoOutput(true);
        httpConnection.setInstanceFollowRedirects(false);
        httpConnection.setReadTimeout(readTimeout);
        httpConnection.setRequestMethod("POST");
        httpConnection.setRequestProperty("Accept-Encoding", "gzip");
        httpConnection.setRequestProperty("Charset", charsetName);
        httpConnection.setRequestProperty("Connection", "close");
        httpConnection.setRequestProperty("User-Agent", "SDK");
        httpConnection.setUseCaches(false);

        return httpConnection;
    }
}

Related Tutorials