Examples of HttpGet


Examples of org.apache.http.client.methods.HttpGet

//  }
 
  @Test
  public void _450KBEntityTest() throws ClientProtocolException, IOException {
    DefaultHttpClient httpclient = new DefaultHttpClient();
    HttpGet httpget = new HttpGet("http://localhost:" + PORT + "/450kb_body");
    HttpResponse response = httpclient.execute(httpget);

    assertNotNull(response);
    assertEquals(200, response.getStatusLine().getStatusCode());
    assertEquals(new ProtocolVersion("HTTP", 1, 1), response.getStatusLine().getProtocolVersion());
View Full Code Here

Examples of org.apache.http.client.methods.HttpGet

  }
 
  @Test
  public void authenticatedRequestHandlerTest() throws ClientProtocolException, IOException {
    DefaultHttpClient httpclient = new DefaultHttpClient();
    HttpGet httpget = new HttpGet("http://localhost:" + PORT + "/authenticated");
    httpget.setHeader("user", "Roger Schildmeijer");
    HttpResponse response = httpclient.execute(httpget);

    assertNotNull(response);
    assertEquals(200, response.getStatusLine().getStatusCode());
    assertEquals(new ProtocolVersion("HTTP", 1, 1), response.getStatusLine().getProtocolVersion());
View Full Code Here

Examples of org.apache.http.client.methods.HttpGet

  }
 
  @Test
  public void notAuthenticatedRequestHandlerTest() throws ClientProtocolException, IOException {
    DefaultHttpClient httpclient = new DefaultHttpClient();
    HttpGet httpget = new HttpGet("http://localhost:" + PORT + "/authenticated");
    httpget.setHeader("wrong_header", "Roger Schildmeijer");
    HttpResponse response = httpclient.execute(httpget);

    assertNotNull(response);
    assertEquals(403, response.getStatusLine().getStatusCode());
    assertEquals(new ProtocolVersion("HTTP", 1, 1), response.getStatusLine().getProtocolVersion());
View Full Code Here

Examples of org.apache.http.client.methods.HttpGet

  }
 
  @Test
  public void queryParamsTest() throws ClientProtocolException, IOException {
    HttpClient httpclient = new DefaultHttpClient();
    HttpGet httpget = new HttpGet("http://localhost:" + PORT + "/query_params?key1=value1&key2=value2");
    HttpResponse response = httpclient.execute(httpget);
    List<String> expectedHeaders = Arrays.asList(new String[] {"Server", "Date", "Content-Length", "Etag", "Connection"});

    assertEquals(200, response.getStatusLine().getStatusCode());
    assertEquals(new ProtocolVersion("HTTP", 1, 1), response.getStatusLine().getProtocolVersion());
View Full Code Here

Examples of org.apache.http.client.methods.HttpGet

    }
  }

  public static int fetch(Page page, boolean ignoreIfBinary) {
    String toFetchURL = page.getWebURL().getURL();
    HttpGet get = null;
    HttpEntity entity = null;
    try {
      get = new HttpGet(toFetchURL);
      synchronized (mutex) {
        long now = (new Date()).getTime();
        if (now - startOfPeriod > 10000) {
          logger.info("Number of pages fetched per second: " + processedCount
              / ((now - startOfPeriod) / 1000));
          processedCount = 0;
          startOfPeriod = now;
        }
        processedCount++;

        if (now - lastFetchTime < politenessDelay) {
          Thread.sleep(politenessDelay - (now - lastFetchTime));
        }
        lastFetchTime = (new Date()).getTime();
      }
      HttpResponse response = httpclient.execute(get);
      entity = response.getEntity();

      int statusCode = response.getStatusLine().getStatusCode();
      if (statusCode != HttpStatus.SC_OK) {
        if (statusCode != HttpStatus.SC_NOT_FOUND) {
          if (statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY) {
            Header header = response.getFirstHeader("Location");
            if (header != null) {
              String movedToUrl = header.getValue();
              page.getWebURL().setURL(movedToUrl);
            } else {
              page.getWebURL().setURL(null);
            }
            return PageFetchStatus.Moved;
          }
          logger.info("Failed: " + response.getStatusLine().toString() + ", while fetching " + toFetchURL);
        } else if (show404Pages) {
          logger.info("Not Found: " + toFetchURL + " (Link found in doc#: "
              + page.getWebURL().getParentDocid() + ")");
        }
        return response.getStatusLine().getStatusCode();
      }

      String uri = get.getURI().toString();
      if (!uri.equals(toFetchURL)) {
        if (!URLCanonicalizer.getCanonicalURL(uri).equals(toFetchURL)) {
          int newdocid = DocIDServer.getDocID(uri);
          if (newdocid != -1) {
            if (newdocid > 0) {
              return PageFetchStatus.RedirectedPageIsSeen;
            }
            WebURL webURL = new WebURL();
            webURL.setURL(uri);
            webURL.setDocid(DocIDServer.getNewDocID(uri));
            page.setWebURL(webURL);
          }
        }
      }

      if (entity != null) {
        long size = entity.getContentLength();
        if (size == -1) {
          Header length = response.getLastHeader("Content-Length");
          if (length == null) {
            length = response.getLastHeader("Content-length");
          }
          if (length != null) {
            size = Integer.parseInt(length.getValue());
          } else {
            size = -1;
          }
        }
        if (size > MAX_DOWNLOAD_SIZE) {
          entity.consumeContent();
          return PageFetchStatus.PageTooBig;
        }

        boolean isBinary = false;

        Header type = entity.getContentType();
        if (type != null) {
          String typeStr = type.getValue().toLowerCase();
          if (typeStr.contains("image") || typeStr.contains("audio") || typeStr.contains("video")) {
            isBinary = true;
            if (ignoreIfBinary) {
              return PageFetchStatus.PageIsBinary;
            }
          }
        }

        if (page.load(entity.getContent(), (int) size, isBinary)) {
          return PageFetchStatus.OK;
        } else {
          return PageFetchStatus.PageLoadError;
        }
      } else {
        get.abort();
      }
    } catch (IOException e) {
      logger.error("Fatal transport error: " + e.getMessage() + " while fetching " + toFetchURL
          + " (link found in doc #" + page.getWebURL().getParentDocid() + ")");
      return PageFetchStatus.FatalTransportError;
    } catch (IllegalStateException e) {
      // ignoring exceptions that occur because of not registering https
      // and other schemes
    } catch (Exception e) {
      if (e.getMessage() == null) {
        logger.error("Error while fetching " + page.getWebURL().getURL());
      } else {
        logger.error(e.getMessage() + " while fetching " + page.getWebURL().getURL());
      }
    } finally {
      try {
        if (entity != null) {
          entity.consumeContent();
        } else if (get != null) {
          get.abort();
        }
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
View Full Code Here

Examples of org.apache.http.client.methods.HttpGet

    public ImageResult call() throws Exception {
      log.debug("Fetching image: {}", result.getRasterImage().getUrl());
      int triesLeft = retries;
      while (true) {
        try {
          HttpGet get = new HttpGet(result.getRasterImage().getUrl());
          HttpResponse response = httpClient.execute(get);
          ByteArrayOutputStream outputStream = new ByteArrayOutputStream(1024);
          response.getEntity().writeTo(outputStream);
          result.setImage(outputStream.toByteArray());
          return result;
View Full Code Here

Examples of org.apache.http.client.methods.HttpGet

         final List<NameValuePair> params = new ArrayList<NameValuePair>();
         params.add(new BasicNameValuePair("jsp", PATH_JSP));
         params.add(new BasicNameValuePair("echo", echoValue));
         final URI uri = URIUtils.createURI("http", "localhost", 8080,
               appName + SEPARATOR + servletClass.getSimpleName(), URLEncodedUtils.format(params, "UTF-8"), null);
         final HttpGet request = new HttpGet(uri);

         // Execute the request
         log.info("Executing request to: " + request.getURI());
         final HttpResponse response = client.execute(request);
         final HttpEntity entity = response.getEntity();
         if (entity == null)
         {
            TestCase.fail("Request returned no entity");
View Full Code Here

Examples of org.apache.http.client.methods.HttpGet

         final HttpClient client = new DefaultHttpClient();

         // Make an HTTP Request
         final URI uri = URIUtils.createURI("http", "localhost", 8080,
               appName + SEPARATOR + servletClass.getSimpleName(), null, null);
         final HttpGet request = new HttpGet(uri);

         // Execute the request
         log.info("Executing request to: " + request.getURI());
         final HttpResponse response = client.execute(request);
         final HttpEntity entity = response.getEntity();
         if (entity == null)
         {
            TestCase.fail("Request returned no entity");
View Full Code Here

Examples of org.apache.http.client.methods.HttpGet

   private HttpRequestBase createHttpMethod(String url, String restVerb)
   {
      if ("GET".equals(restVerb))
      {
         return new HttpGet(url);
      }
      else if ("POST".equals(restVerb))
      {
         return new HttpPost(url);
      }
View Full Code Here

Examples of org.apache.http.client.methods.HttpGet

     * @throws IOException
     */
    public byte[] GETbytes(final MultiProtocolURI url, final long maxBytes) throws IOException {
        final boolean localhost = url.getHost().equals("localhost");
        final String urix = url.toNormalform(true, false, !localhost, false);
        final HttpGet httpGet = new HttpGet(urix);
        if (!localhost) setHost(url.getHost()); // overwrite resolved IP, needed for shared web hosting DO NOT REMOVE, see http://en.wikipedia.org/wiki/Shared_web_hosting_service
        return getContentBytes(httpGet, maxBytes);
    }
View Full Code Here
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.