Ejemplo n.º 1
0
  public void run() {
    try {
      Socket socket = new Socket(host.getHostName(), host.getPort());
      conn.bind(socket, params);

      // build request
      BasicHttpRequest request;

      if (!this.getRequest().getBody().isEmpty()) {
        request =
            new BasicHttpEntityEnclosingRequest(
                this.getRequest().getMethod(), this.getRequest().getPath());
      } else {
        request = new BasicHttpRequest(this.getRequest().getMethod(), this.getRequest().getPath());
      }

      // add headers
      Map<String, String> headers = this.getRequest().getHeaders();
      Iterator<Map.Entry<String, String>> it = headers.entrySet().iterator();

      while (it.hasNext()) {
        Map.Entry<String, String> pairs = it.next();

        request.addHeader(pairs.getKey(), pairs.getValue());
      }

      // set body
      if (request instanceof BasicHttpEntityEnclosingRequest) {
        StringEntity body = new StringEntity(this.getRequest().getBody());

        ((BasicHttpEntityEnclosingRequest) request).setEntity(body);
      }

      logger.info("> " + request.getRequestLine().getUri());

      // request
      request.setParams(params);
      httpexecutor.preProcess(request, httpproc, context);

      HttpResponse response = httpexecutor.execute(request, conn, context);
      response.setParams(params);
      httpexecutor.postProcess(response, httpproc, context);

      logger.info("< " + response.getStatusLine());

      // set all request headers
      Header[] allHeaders = request.getAllHeaders();

      for (int i = 0; i < allHeaders.length; i++) {
        this.getRequest().setHeader(allHeaders[i].getName(), allHeaders[i].getValue());
      }

      // create response
      String content = "";
      HttpEntity entity = response.getEntity();

      if (entity != null) {
        content = EntityUtils.toString(entity);
      }

      this.response = new Response(response, content);

      // call callback
      callback.onResponse(this.request, this.response);
    } catch (Exception e) {
      Aletheia.handleException(e);
    } finally {
      try {
        conn.close();
      } catch (Exception e) {
        Aletheia.handleException(e);
      }
    }
  }
Ejemplo n.º 2
0
  public static void main(String[] args) throws Exception {

    HttpParams params = new SyncBasicHttpParams();
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(params, "UTF-8");
    HttpProtocolParams.setUserAgent(params, "HttpComponents/1.1");
    HttpProtocolParams.setUseExpectContinue(params, true);
    HttpProcessor httpproc =
        new ImmutableHttpProcessor(
            new HttpRequestInterceptor[] {
              new RequestContent(),
              new RequestTargetHost(),
              new RequestConnControl(),
              new RequestUserAgent(),
              new RequestExpectContinue()
            });
    HttpRequestExecutor httpexecutor = new HttpRequestExecutor();
    HttpContext context = new BasicHttpContext(null);
    HttpHost host = new HttpHost("localhost", 8080);
    DefaultHttpClientConnection conn = new DefaultHttpClientConnection();
    ConnectionReuseStrategy connStrategy = new DefaultConnectionReuseStrategy();
    context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn);
    context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, host);
    try {

      String[] targets = {"/t.html"};

      for (int i = 0; i < targets.length; i++) {

        if (!conn.isOpen()) {

          Socket socket = new Socket(host.getHostName(), host.getPort());

          conn.bind(socket, params);
        }

        BasicHttpRequest request = new BasicHttpRequest("GET", targets[i]);

        System.out.println(">> Request URI: " + request.getRequestLine().getUri());

        request.setParams(params);

        httpexecutor.preProcess(request, httpproc, context);

        HttpResponse response = httpexecutor.execute(request, conn, context);

        response.setParams(params);

        httpexecutor.postProcess(response, httpproc, context);

        System.out.println("<< Response: " + response.getStatusLine());

        System.out.println(EntityUtils.toString(response.getEntity()));

        System.out.println("==============");

        if (!connStrategy.keepAlive(response, context)) {

          conn.close();

        } else {

          System.out.println("Connection kept alive...");
        }
      }

    } finally {

      conn.close();
    }
  }