private void dispatchSomePendingEvents(boolean flag)
     throws IOException, ParseException, HttpException
 {
     if (GoogleAnalyticsTracker.getInstance().getDebug() && flag)
     {
         Log.v("GoogleAnalyticsTracker", "dispatching events in dry run mode");
     }
     int i = 0;
     while (i < events.size() && i < maxEventsPerRequest) 
     {
         Event event = (Event)events.get(i);
         String s;
         BasicHttpRequest basichttprequest;
         if ("__##GOOGLEPAGEVIEW##__".equals(event.category))
         {
             s = NetworkRequestUtil.constructPageviewRequestPath(event, referrer);
         } else
         if ("__##GOOGLETRANSACTION##__".equals(event.category))
         {
             s = NetworkRequestUtil.constructTransactionRequestPath(event, referrer);
         } else
         if ("__##GOOGLEITEM##__".equals(event.category))
         {
             s = NetworkRequestUtil.constructItemRequestPath(event, referrer);
         } else
         {
             s = NetworkRequestUtil.constructEventRequestPath(event, referrer);
         }
         basichttprequest = new BasicHttpRequest("GET", s);
         basichttprequest.addHeader("Host", NetworkDispatcher.access$200().getHostName());
         basichttprequest.addHeader("User-Agent", userAgent);
         if (GoogleAnalyticsTracker.getInstance().getDebug())
         {
             Log.i("GoogleAnalyticsTracker", basichttprequest.getRequestLine().toString());
         }
         if (flag)
         {
             requesterCallBacks.requestSent();
         } else
         {
             pipelinedRequester.addRequest(basichttprequest);
         }
         i++;
     }
     if (!flag)
     {
         pipelinedRequester.sendRequests();
     }
 }
コード例 #2
0
ファイル: HttpProtocol.java プロジェクト: tianyl1984/myWebApp
  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);
      }
    }
  }
コード例 #3
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();
    }
  }