Package api.http

Source Code of api.http.HttpRequest

package api.http;

import api.KeyValuePair;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class HttpRequest{

  /**
   * "GET" standard HTTP request method.
   */
  public static final String METHOD_GET = "GET";
  /**
   * "POST" standard HTTP request method.
   */
  public static final String METHOD_POST = "POST";
  /**
   * "PUT" standard HTTP request method.
   */
  public static final String METHOD_PUT = "PUT";
  /**
   * "DELETE" standard HTTP request method.
   */
  public static final String METHOD_DELETE = "DELETE";
  private URL url;
  private String method = HttpRequest.METHOD_GET;
  private String contentType = "application/x-www-form-urlencoded";
  private List<KeyValuePair> headers = new ArrayList<KeyValuePair>();
  private String data;

  /**
   * constructs this object with url.
   *
   * @param url a target url requests for.
   */
  public HttpRequest(URL url){
    this.url = url;
  }

  /**
   * set the standard HTTP request method. it should be "GET", "POST", "PUT"
   * or "DELETE".
   *
   * @param method "GET", "POST", "PUT", "DELETE"
   */
  public void setMethod(String method){
    method = method.toUpperCase();
    List<String> methods = new ArrayList<String>();
    methods.add(HttpRequest.METHOD_POST);
    methods.add(HttpRequest.METHOD_PUT);
    methods.add(HttpRequest.METHOD_DELETE);
    if (method.contains(method)){
      this.method = method;
    }
  }

  /**
   * adds the header of key and value into this object.
   *
   * @param key the header of key.
   * @param value the header of value.
   */
  public void addHeader(String key, String value){
    this.headers.add(new KeyValuePair(key, value));
  }

  /**
   * adds the data of bytes into this object.
   *
   * @param data data of bytes.
   */
  public void setData(String data){
    this.data = data;
  }

  /**
   * adds the content type into this object.
   *
   * @param contentType content type.
   */
  public void setContentType(String contentType){
    this.contentType = contentType;
  }

  /**
   * send a HTTP request to url.
   *
   * @return the response after the HTTP request is sent.
   * @throws IOException the url is not exist or something wrong of the server
   * of target url.
   */
  public String send() throws IOException{
    String response = null;
    HttpURLConnection connection = (HttpURLConnection)this.url.openConnection();
    connection.setDoInput(true);
    connection.setDoOutput(true);
    connection.setRequestMethod(this.method);
    connection.addRequestProperty("Content-Type", this.contentType);
    for (int i = 0; i < this.headers.size(); i++){
      KeyValuePair header = this.headers.get(i);
      connection.addRequestProperty(header.key, header.value);
    }
    if (this.data != null && this.data.length() > 0){
      OutputStream os = connection.getOutputStream();
      os.write(this.data.getBytes());
      os.flush();
      os.close();
    }
    connection.disconnect();
    InputStream is = connection.getInputStream();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte buffer[] = new byte[1024];
    for (int length; (length = is.read(buffer, 0, buffer.length)) > -1;){
      baos.write(buffer, 0, length);
    }
    is.close();
    baos.flush();
    response = baos.toString();
    baos.close();
    return response;
  }

  /**
   * returns the string represent this object.
   *
   * @return the string represent this object.
   */
  @Override
  public String toString(){
    StringBuilder sbr = new StringBuilder();
    sbr.append(String.format("url = \"%s\"\n", this.url.toString()));
    sbr.append(String.format("method = \"%s\"\n", this.method));
    sbr.append(String.format("setContentType = \"%s\"\n", this.contentType));
    sbr.append(String.format("headers = \"%s\"\n", this.headers.toString()));
    sbr.append(String.format("data = \"%s\"\n", this.data));
    return sbr.toString();
  }
}
TOP

Related Classes of api.http.HttpRequest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.