All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.api.bloxy.executor.impl.HttpClient.kt Maven / Gradle / Ivy

There is a newer version: 1.2.1
Show newest version
package io.api.bloxy.executor.impl

import io.api.bloxy.executor.IHttpClient
import java.net.URL
import java.util.stream.Collectors


/**
 * @see IHttpClient
 *
 * @author GoodforGod
 * @since 16.11.2018
 */
class HttpClient @JvmOverloads constructor(
    private val connectTimeout: Int = 8000,
    private val readTimeout: Int = 0
) : IHttpClient {

    companion object {
        private val headers: Map = hashMapOf(
            "Accept-Language" to "en",
            "Accept-Encoding" to "deflate",
            "Accept-Charset" to "UTF-8",
            "User-Agent" to "Chrome/68.0.3440.105",
            "Content-Type" to "application/x-www-form-urlencoded"
        )
    }

    override fun get(url: String): String {
        URL(url).openConnection().apply {
            readTimeout = [email protected]
            connectTimeout = [email protected]
            headers.forEach { e -> setRequestProperty(e.key, e.value) }
            getHeaderField("Location")?.let { return get(it) }
        }.getInputStream().use {
            return it.bufferedReader().lines().collect(Collectors.joining())
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy