Http.get(String, Http.Conf), post(String, Http.Conf) =>

Http.get(Http.Conf), Http.post(Http.Conf)
master
mjkhan21 2 years ago
parent e19abaee6d
commit 5c47d8c195

@ -17,8 +17,8 @@ import cokr.xit.foundation.AbstractComponent;
/**Http
* <p> .
* <pre><code> new Http().get(
* "https://...",
* new Http.Conf()
* .url("https://...")
* // .header("Content-Type", "application/x-www-form-urlencoded")
* .param("param0", "value0")
* .param("param1", "value1")
@ -27,6 +27,8 @@ import cokr.xit.foundation.AbstractComponent;
* @author mjkhan
*/
public class Http extends AbstractComponent {
public static final String FORM = "application/x-www-form-urlencoded";
public static final String JSON = "application/json";
/**http
* @author mjkhan
*/
@ -43,12 +45,12 @@ public class Http extends AbstractComponent {
/** .
* @return
*/
protected String charset() {
private String charset() {
return ifEmpty(charset, () -> "UTF-8");
}
/** .
* @param charset
* @param charset . UTF-8.
* @return Conf
*/
public Conf charset(String charset) {
@ -60,21 +62,30 @@ public class Http extends AbstractComponent {
* @return URL
* @throws Exception
*/
protected URL url() throws Exception {
return new URL(url);
private URL url() throws Exception {
return new URL(notEmpty(url, "url"));
}
/**http . "GET"
* @return http
/**http url .
* @param url url
* @return Conf
*/
public Conf url(String url) {
this.url = url;
return this;
}
/**http .
* @return http . "GET"
*/
protected String method() {
private String method() {
return ifEmpty(method, "GET");
}
/** .
* @return
*/
protected String params() {
private String params() {
return !isEmpty(params) ? params.entrySet().stream().map(this::encode).collect(Collectors.joining("&")) : "";
}
@ -82,7 +93,7 @@ public class Http extends AbstractComponent {
* @param entry
* @return
*/
protected String encode(Map.Entry<String, String> entry) {
private String encode(Map.Entry<String, String> entry) {
try {
return URLEncoder.encode(entry.getKey(), charset()) + "=" + URLEncoder.encode(entry.getValue(), charset());
} catch (Exception e) {
@ -117,7 +128,7 @@ public class Http extends AbstractComponent {
/** http .
* @param http HttpURLConnection
*/
protected void setHeaders(HttpURLConnection http) {
private void setHeaders(HttpURLConnection http) {
if (isEmpty(headers)) return;
headers.forEach(http::setRequestProperty);
@ -126,7 +137,7 @@ public class Http extends AbstractComponent {
/**http . 10000
* @return http
*/
protected int timeout() {
private int timeout() {
return Math.max(timeout, 10000);
}
@ -187,35 +198,32 @@ public class Http extends AbstractComponent {
}
/** url GET .
* @param url url
* @param conf
*/
public void get(String url, Conf conf) {
public void get(Conf conf) {
if (conf == null)
conf = new Conf();
conf.method = "GET";
request(url, conf);
request(conf);
}
/** url POST .
* @param url url
* @param conf
*/
public void post(String url, Conf conf) {
public void post(Conf conf) {
if (conf == null)
conf = new Conf();
conf.method = "POST";
request(url, conf);
request(conf);
}
/** url , conf .
* @param url url
* @param conf
*/
protected void request(String url, Conf conf) {
private void request(Conf conf) {
HttpURLConnection http = null;
try {
conf.url = url;
http = (HttpURLConnection)conf.url().openConnection();
http.setRequestMethod(conf.method());
http.setUseCaches(false);
@ -243,23 +251,23 @@ public class Http extends AbstractComponent {
* @param http HttpURLConnection
* @throws Exception
*/
protected void setParams(Conf conf, HttpURLConnection http) throws Exception {
private void setParams(Conf conf, HttpURLConnection http) throws Exception {
String params = conf.params();
if (!isEmpty(params)) {
if (isEmpty(params)) return;
http.setDoOutput(true);
DataOutputStream out = new DataOutputStream(http.getOutputStream());
out.writeBytes(params);
out.flush();
out.close();
}
}
/**http .
* @param http HttpURLConnection
* @return http
* @throws Exception
*/
protected Response getResponse(HttpURLConnection http) throws Exception {
private Response getResponse(HttpURLConnection http) throws Exception {
Response resp = new Response();
resp.status = http.getResponseCode();
@ -284,7 +292,7 @@ public class Http extends AbstractComponent {
* @param t Throwable
* @return http
*/
protected Response getResponse(Throwable t) {
private Response getResponse(Throwable t) {
Response resp = new Response();
resp.status = 500;
resp.response = rootCause(t).getMessage();

Loading…
Cancel
Save