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

org.dsa.iot.dslink.util.http.HttpClient Maven / Gradle / Ivy

There is a newer version: 0.24.2
Show newest version
package org.dsa.iot.dslink.util.http;

import org.dsa.iot.dslink.provider.HttpProvider;
import org.dsa.iot.dslink.util.URLInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Samuel Grenier
 */
public class HttpClient {

    private final URLInfo url;

    public HttpClient(URLInfo url) {
        if (url == null) {
            throw new NullPointerException("url");
        }
        this.url = url;
    }

    public HttpResp post(String uri, byte[] content) {
        HttpProvider provider = HttpProvider.getProvider();
        Map headers = new HashMap<>();
        {
            headers.put("connection", "close");
            headers.put("accept-encoding", "text/plain");
            headers.put("host", url.host);
        }

        URLInfo tmp = createUrlFromUri(url, uri);
        return provider.post(tmp, content, headers);
    }

    private static URLInfo createUrlFromUri(URLInfo url, String uri) {
        if (uri == null || uri.isEmpty()) {
            uri = "/";
        }
        return new URLInfo(url.protocol, url.host, url.port, uri, url.secure);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy