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

org.yelong.http.response.HttpResponseBuilder Maven / Gradle / Ivy

Go to download

简单封装java对http的请求,实现便捷的发送http请求(可以发送携带文件的请求)

The newest version!
/**
 * 
 */
package org.yelong.http.response;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Collectors;

import org.yelong.http.request.HttpRequest;
import org.yelong.http.utils.WebUtils;

/**
 * @since 1.0
 */
public final class HttpResponseBuilder {

	private HttpResponseBuilder() {
	}

	public static HttpResponse build(HttpURLConnection conn) throws IOException {
		return build(conn, null);
	}

	public static HttpResponse build(HttpURLConnection conn, HttpRequest request) throws IOException {
		// 响应消息体头
		Map headers = new HashMap<>();
		Map> headerFields = conn.getHeaderFields();
		for (Entry> entry : headerFields.entrySet()) {
			String value = entry.getValue().stream().collect(Collectors.joining(","));
			headers.put(entry.getKey(), value);
		}
		byte[] content = WebUtils.getResponseContent(conn);
		String charset = WebUtils.getCharset(conn);
		int responseCode = conn.getResponseCode();
		return new DefaultHttpResponse(request, headers, content, charset, responseCode);
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy