Package org.apache.http

Examples of org.apache.http.HttpResponse


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

    assertEquals(200, response.getStatusLine().getStatusCode());
    assertEquals(new ProtocolVersion("HTTP", 1, 1), response.getStatusLine().getProtocolVersion());
    assertEquals("OK", response.getStatusLine().getReasonPhrase());

    assertEquals(expectedHeaders.size(), response.getAllHeaders().length);

    for (String header : expectedHeaders) {
      assertTrue(response.getFirstHeader(header) != null);
    }

    assertEquals(expectedPayload, convertStreamToString(response.getEntity().getContent()).trim());
    assertEquals(expectedPayload.length()+"", response.getFirstHeader("Content-Length").getValue());
  }
View Full Code Here


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

    assertNotNull(response);
    assertEquals(200, response.getStatusLine().getStatusCode());
    assertEquals(new ProtocolVersion("HTTP", 1, 1), response.getStatusLine().getProtocolVersion());
    assertEquals("OK", response.getStatusLine().getReasonPhrase());
    assertEquals(4, response.getAllHeaders().length);
    assertEquals("8", response.getFirstHeader("Content-Length").getValue());
  }
View Full Code Here

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

    assertNotNull(response);
    assertEquals(200, response.getStatusLine().getStatusCode());
    assertEquals(new ProtocolVersion("HTTP", 1, 1), response.getStatusLine().getProtocolVersion());
    assertEquals("OK", response.getStatusLine().getReasonPhrase());
    assertEquals(5, response.getAllHeaders().length);
    assertEquals("7", response.getFirstHeader("Content-Length").getValue());
    assertEquals("kickass", convertStreamToString(response.getEntity().getContent()).trim());
  }
View Full Code Here

 
  @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());
    assertEquals("OK", response.getStatusLine().getReasonPhrase());
    assertEquals(5, response.getAllHeaders().length);
    //assertEquals(450*1024, Integer.parseInt(response.getFirstHeader("Content-Length").getValue())/8);
    //assertEquals(450*1024, _450KBResponseEntityRequestHandlr.entity.getBytes(Charsets.UTF_8).length);
    String payLoad = convertStreamToString(response.getEntity().getContent()).trim();
    assertEquals(_450KBResponseEntityRequestHandler.entity, payLoad);
  }
View Full Code Here

    final String body = "Räger Schildmäijår";
   
    DefaultHttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://localhost:" + PORT + "/echo");
    httppost.setEntity(new StringEntity(body))// HTTP 1.1 says that the default charset is ISO-8859-1
    HttpResponse response = httpclient.execute(httppost)
   
    assertNotNull(response);
    assertEquals(200, response.getStatusLine().getStatusCode());
    assertEquals(new ProtocolVersion("HTTP", 1, 1), response.getStatusLine().getProtocolVersion());
    assertEquals("OK", response.getStatusLine().getReasonPhrase());
    assertEquals(5, response.getAllHeaders().length);
    String payLoad = convertStreamToString(response.getEntity().getContent()).trim();
    assertEquals(body, payLoad);
  }
View Full Code Here

  @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());
    assertEquals("OK", response.getStatusLine().getReasonPhrase());
    assertEquals(5, response.getAllHeaders().length);
    String payLoad = convertStreamToString(response.getEntity().getContent()).trim();
    assertEquals("Roger Schildmeijer", payLoad);
  }
View Full Code Here

  @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());
    assertEquals("Forbidden", response.getStatusLine().getReasonPhrase());
    assertEquals(5, response.getAllHeaders().length);
    String payLoad = convertStreamToString(response.getEntity().getContent()).trim();
    assertEquals("Authentication failed", payLoad);
  }
View Full Code Here

 
  @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());
    assertEquals("OK", response.getStatusLine().getReasonPhrase());
   
    assertEquals(expectedHeaders.size(), response.getAllHeaders().length);

    for (String header : expectedHeaders) {
      assertTrue(response.getFirstHeader(header) != null);
    }

    final String expected = "value1 value2";
    assertEquals(expected, convertStreamToString(response.getEntity().getContent()).trim());
    assertEquals(expected.length()+"", response.getFirstHeader("Content-Length").getValue());
  }
View Full Code Here

        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;
View Full Code Here

      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;
        } catch (Exception e) {
          if (log.isDebugEnabled()) {
            log.error("Fetching image: error loading " + result.getRasterImage().getUrl(), e);
View Full Code Here

TOP

Related Classes of org.apache.http.HttpResponse

Copyright © 2018 www.massapicom. 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.