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

com.xiongyingqi.http.HttpBuilder Maven / Gradle / Ivy

package com.xiongyingqi.http;

import com.xiongyingqi.util.EntityHelper;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.*;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.junit.Test;

import java.io.IOException;
import java.net.URI;
import java.nio.charset.Charset;
import java.nio.charset.UnsupportedCharsetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Created by 瑛琪xiongyingqi.com on 2014/4/18 0018.
 */
public class HttpBuilder {
    private String method = "GET";
    private String host = "";
    private int port = -1;
    private String url = "";
    private String uri = "";
    private Collection
headers = new ArrayList
(); private List nameValuePairs = new ArrayList(); private Charset charset = Charset.forName("UTF-8"); private String protocol = "http"; public static HttpBuilder newBuilder() { return new HttpBuilder(); } public HttpBuilder host(String host) { this.host = host; return this; } public HttpBuilder port(int port) { this.port = port; return this; } public HttpBuilder url(String url) { this.url = url; return this; } public HttpBuilder uri(String uri) { this.uri = uri; return this; } public HttpBuilder header(String name, String value) { Header header = new BasicHeader(name, value); this.headers.add(header); return this; } public HttpBuilder param(String name, String value) { NameValuePair nameValuePair = new BasicNameValuePair(name, value); this.nameValuePairs.add(nameValuePair); return this; } public HttpBuilder http() { this.protocol = "http"; return this; } public HttpBuilder https() { this.protocol = "https"; return this; } public HttpBuilder post() { this.method = "POST"; return this; } public HttpBuilder get() { this.method = "GET"; return this; } public HttpBuilder delete() { this.method = "DELETE"; return this; } public HttpBuilder put() { this.method = "PUT"; return this; } public HttpBuilder charset(String charset) { Charset charsetVar = null; try { charsetVar = Charset.forName(charset); } catch (UnsupportedCharsetException e) { charsetVar = Charset.forName("UTF-8"); } this.charset = charsetVar; return this; } private String buildUrl() { if (this.url != null && !"".equals(this.url.trim())) { if (!this.url.startsWith("http://") && !this.url.startsWith("https://")) { this.url = protocol + "://" + this.url; } return this.url; } String url = ""; if (!this.url.startsWith(protocol + "://")) { url = protocol + "://"; } url += host; if (port <= 0) { url += port; } if (!url.endsWith("/") && !url.endsWith("\\")) { url += "/"; } url += uri; EntityHelper.print(url); return url; } private HttpRequestBase buildMethod() { HttpRequestBase httpRequestBase = null; if (method.equals("GET")) { httpRequestBase = new HttpGet(); } else if (method.equals("POST")) { httpRequestBase = new HttpPost(); } else if (method.equals("PUT")) { httpRequestBase = new HttpPut(); } else if (method.equals("DELETE")) { httpRequestBase = new HttpDelete(); } return httpRequestBase; } public HttpRequestBase build() { String url = buildUrl(); HttpRequestBase requestBase = buildMethod(); for (Header header : headers) { requestBase.addHeader(header); } requestBase.setURI(URI.create(url)); if (nameValuePairs != null && nameValuePairs.size() > 0 && requestBase instanceof HttpPost) { HttpEntity requestEntity = new UrlEncodedFormEntity(nameValuePairs, charset); ((HttpPost) requestBase).setEntity(requestEntity); } return requestBase; } @Test public void test() { try { String rs = HttpAccess.execute(HttpAccess.getClient(), HttpBuilder.newBuilder().url("http://www.baidu.com").get().build()); EntityHelper.print(rs); } catch (IOException e) { e.printStackTrace(); } } public List getNameValuePairs() { return nameValuePairs; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy