Java Code Examples for org.apache.commons.httpclient.methods.GetMethod#getResponseBodyAsString()

The following examples show how to use org.apache.commons.httpclient.methods.GetMethod#getResponseBodyAsString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HttpUtil.java    From OfficeAutomatic-System with Apache License 2.0 6 votes vote down vote up
public static String sendGet(String urlParam) throws HttpException, IOException {
    // 创建httpClient实例对象
    HttpClient httpClient = new HttpClient();
    // 设置httpClient连接主机服务器超时时间:15000毫秒
    httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(15000);
    // 创建GET请求方法实例对象
    GetMethod getMethod = new GetMethod(urlParam);
    // 设置post请求超时时间
    getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 60000);
    getMethod.addRequestHeader("Content-Type", "application/json");

    httpClient.executeMethod(getMethod);

    String result = getMethod.getResponseBodyAsString();
    getMethod.releaseConnection();
    return result;
}
 
Example 2
Source File: YouTubeCaptionsScraper.java    From Babler with Apache License 2.0 6 votes vote down vote up
/**
 * Fetches captions/transcript for a given video
 * @param videoID to fetch
 * @param lang this captions should be in
 * @throws IOException
 */
public void getAndSaveTranscript(String videoID, String lang) throws IOException {

    lang = LanguageCode.convertIso2toIso1(lang);

    String url = captionEndPoint+"lang="+lang+"&v="+videoID;
    GetMethod get = new GetMethod(url);
    this.client.executeMethod(get);
    String xmlData = get.getResponseBodyAsString();

    //parse XML
    Document doc = Jsoup.parse(xmlData, "", Parser.xmlParser());
    String allCaps = "";
    for (Element e : doc.select("text")) {
        allCaps += e.text();
    }

    FileSaver file = new FileSaver(allCaps, lang, "youtube_caps", url, videoID);
    file.save(logDb);

}
 
Example 3
Source File: EndpointIT.java    From microprofile-sandbox with Apache License 2.0 6 votes vote down vote up
@Test
public void testServlet() throws Exception {
    HttpClient client = new HttpClient();

    GetMethod method = new GetMethod(URL);

    try {
        int statusCode = client.executeMethod(method);

        assertEquals("HTTP GET failed", HttpStatus.SC_OK, statusCode);

        String response = method.getResponseBodyAsString(10000);

        assertTrue("Unexpected response body",
                response.contains("Hello World From Your Friends at Liberty Boost EE!"));
    } finally {
        method.releaseConnection();
    }
}
 
Example 4
Source File: NetUtils.java    From ApprovalTests.Java with Apache License 2.0 6 votes vote down vote up
public static String loadWebPage(String url, String parameters)
{
  try
  {
    HttpClient client = new HttpClient();
    GetMethod method = new GetMethod(url);
    if (parameters != null)
    {
      method.setQueryString(parameters);
    }
    client.executeMethod(method);
    String html = method.getResponseBodyAsString();
    return html;
  }
  catch (Exception e)
  {
    throw ObjectUtils.throwAsError(e);
  }
}
 
Example 5
Source File: EndpointIT.java    From boost with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void testServlet() throws Exception {
    HttpClient client = new HttpClient();

    GetMethod method = new GetMethod(URL + "/hello");

    try {
        int statusCode = client.executeMethod(method);

        assertEquals("HTTP GET failed", HttpStatus.SC_OK, statusCode);

        String response = method.getResponseBodyAsString(10000);

        assertTrue("Unexpected response body",
                response.contains("Hello World From Your Friends at Liberty Boost EE!"));
    } finally {
        method.releaseConnection();
    }
}
 
Example 6
Source File: EndpointIT.java    From boost with Eclipse Public License 1.0 6 votes vote down vote up
@Test
public void testServlet() throws Exception {
    HttpClient client = new HttpClient();

    GetMethod method = new GetMethod(URL);

    try {
        int statusCode = client.executeMethod(method);

        assertEquals("HTTP GET failed", HttpStatus.SC_OK, statusCode);

        String response = method.getResponseBodyAsString(10000);

        assertTrue("Unexpected response body", response.contains("The population of NYC is 8550405"));
    } finally {
        method.releaseConnection();
    }
}
 
Example 7
Source File: ServletTest.java    From ci.maven with Apache License 2.0 6 votes vote down vote up
@Test
public void testServlet() throws Exception {
    HttpClient client = new HttpClient();

    GetMethod method = new GetMethod(URL);

    try {
        int statusCode = client.executeMethod(method);

        assertEquals("HTTP GET failed", HttpStatus.SC_OK, statusCode);

        String response = method.getResponseBodyAsString();

        assertTrue("Unexpected response body", response.contains("Simple Servlet ran successfully"));
    } finally {
        method.releaseConnection();
    }  
}
 
Example 8
Source File: CatalogITCase.java    From olat with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetOwner() throws IOException {
    final HttpClient c = loginWithCookie("administrator", "olat");

    // admin is owner
    URI uri = UriBuilder.fromUri(getContextURI()).path("catalog").path(entry1.getKey().toString()).path("owners").path(admin.getKey().toString()).build();
    GetMethod method = createGet(uri, MediaType.APPLICATION_JSON, true);

    int code = c.executeMethod(method);
    assertEquals(200, code);
    final String body = method.getResponseBodyAsString();
    method.releaseConnection();
    final UserVO vo = parse(body, UserVO.class);
    assertNotNull(vo);

    // id1 is not owner
    uri = UriBuilder.fromUri(getContextURI()).path("catalog").path(entry1.getKey().toString()).path("owners").path(id1.getKey().toString()).build();
    method = createGet(uri, MediaType.APPLICATION_JSON, true);

    code = c.executeMethod(method);
    assertEquals(404, code);
}
 
Example 9
Source File: GroupMgmtITCase.java    From olat with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetGroupInfos2() throws IOException {
    final HttpClient c = loginWithCookie("administrator", "olat");

    final String request = "/groups/" + g2.getKey() + "/infos";
    final GetMethod method = createGet(request, MediaType.APPLICATION_JSON, true);
    final int code = c.executeMethod(method);
    assertEquals(code, 200);
    final String body = method.getResponseBodyAsString();
    method.releaseConnection();
    final GroupInfoVO vo = parse(body, GroupInfoVO.class);
    assertNotNull(vo);
    assertEquals(Boolean.FALSE, vo.getHasWiki());
    assertNull(vo.getNews());
    assertNotNull(vo.getForumKey());
}
 
Example 10
Source File: RepositoryEntriesITCase.java    From olat with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetEntry() throws HttpException, IOException {
    final RepositoryEntry re = createRepository("Test GET repo entry", 83911l);

    final HttpClient c = loginWithCookie("administrator", "olat");

    final GetMethod method = createGet("repo/entries/" + re.getKey(), MediaType.APPLICATION_JSON, true);
    final int code = c.executeMethod(method);
    assertEquals(200, code);
    final String body = method.getResponseBodyAsString();
    method.releaseConnection();

    final RepositoryEntryVO entryVo = parse(body, RepositoryEntryVO.class);
    assertNotNull(entryVo);
}
 
Example 11
Source File: OlatJerseyTestCase.java    From olat with Apache License 2.0 5 votes vote down vote up
/**
 * Login the HttpClient based on the session cookie
 * 
 * @param username
 * @param password
 * @param c
 * @throws HttpException
 * @throws IOException
 */
public String loginWithToken(final String username, final String password, final HttpClient c) throws HttpException, IOException {
    final URI uri = UriBuilder.fromUri(getContextURI()).path("auth").path(username).queryParam("password", password).build();
    final GetMethod method = new GetMethod(uri.toString());
    final int response = c.executeMethod(method);
    final String body = method.getResponseBodyAsString();
    method.releaseConnection();
    assertTrue(response == 200);
    assertTrue(body != null && body.length() > "<hello></hello>".length());
    final Header securityToken = method.getResponseHeader(RestSecurityHelper.SEC_TOKEN);
    assertTrue(securityToken != null && StringHelper.containsNonWhitespace(securityToken.getValue()));
    return securityToken == null ? null : securityToken.getValue();
}
 
Example 12
Source File: KafkaAvroSchemaRegistry.java    From incubator-gobblin with Apache License 2.0 5 votes vote down vote up
/**
 * Fetch schema by key.
 */
@Override
protected Schema fetchSchemaByKey(String key) throws SchemaRegistryException {
  String schemaUrl = KafkaAvroSchemaRegistry.this.url + GET_RESOURCE_BY_ID + key;

  GetMethod get = new GetMethod(schemaUrl);

  int statusCode;
  String schemaString;
  HttpClient httpClient = this.borrowClient();
  try {
    statusCode = httpClient.executeMethod(get);
    schemaString = get.getResponseBodyAsString();
  } catch (IOException e) {
    throw new SchemaRegistryException(e);
  } finally {
    get.releaseConnection();
    this.httpClientPool.returnObject(httpClient);
  }

  if (statusCode != HttpStatus.SC_OK) {
    throw new SchemaRegistryException(
        String.format("Schema with key %s cannot be retrieved, statusCode = %d", key, statusCode));
  }

  Schema schema;
  try {
    schema = new Schema.Parser().parse(schemaString);
  } catch (Throwable t) {
    throw new SchemaRegistryException(String.format("Schema with ID = %s cannot be parsed", key), t);
  }

  return schema;
}
 
Example 13
Source File: HttpClientUtil.java    From Gather-Platform with GNU General Public License v3.0 5 votes vote down vote up
public String getWithRealHeader(String url) throws IOException {
//        clearCookies();
        GetMethod g = new GetMethod(url);
        ////////////////////////
        g.addRequestHeader("Accept", "text/html,application/xhtml+xml,application/xml;");
        g.addRequestHeader("Accept-Language", "zh-cn");
        g.addRequestHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.0.3) Gecko/2008092417 Firefox/3.0.3");
        g.addRequestHeader("Keep-Alive", "300");
        g.addRequestHeader("Connection", "Keep-Alive");
        g.addRequestHeader("Cache-Control", "no-cache");
        ///////////////////////
        hc.executeMethod(g);
        return g.getResponseBodyAsString();
    }
 
Example 14
Source File: GroupMgmtITCase.java    From olat with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetMessages() throws IOException {
    final HttpClient c = loginWithCookie("rest-one", "A6B7C8");

    final String request = "/groups/" + g1.getKey() + "/forum/posts/" + m1.getKey();
    final GetMethod method = createGet(request, MediaType.APPLICATION_JSON, true);
    final int code = c.executeMethod(method);
    assertEquals(code, 200);
    final String body = method.getResponseBodyAsString();
    method.releaseConnection();
    final List<MessageVO> messages = parseMessageArray(body);

    assertNotNull(messages);
    assertEquals(4, messages.size());
}
 
Example 15
Source File: CoursesResourcesFoldersITCase.java    From olat with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetFilesDeeper() throws IOException {
    final HttpClient c = loginWithCookie("administrator", "olat");
    final URI uri = UriBuilder.fromUri(getCourseFolderURI()).path("SubDir").path("SubSubDir").path("SubSubSubDir").build();
    final GetMethod method = createGet(uri, MediaType.APPLICATION_JSON, true);
    final int code = c.executeMethod(method);
    assertEquals(code, 200);

    final String body = method.getResponseBodyAsString();
    final List<LinkVO> links = parseLinkArray(body);
    assertNotNull(links);
    assertEquals(1, links.size());
    assertEquals("3_singlepage.html", links.get(0).getTitle());
}
 
Example 16
Source File: RepositoryEntriesITCase.java    From olat with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetEntry() throws HttpException, IOException {
    final RepositoryEntry re = createRepository("Test GET repo entry", 83911l);

    final HttpClient c = loginWithCookie("administrator", "olat");

    final GetMethod method = createGet("repo/entries/" + re.getKey(), MediaType.APPLICATION_JSON, true);
    final int code = c.executeMethod(method);
    assertEquals(200, code);
    final String body = method.getResponseBodyAsString();
    method.releaseConnection();

    final RepositoryEntryVO entryVo = parse(body, RepositoryEntryVO.class);
    assertNotNull(entryVo);
}
 
Example 17
Source File: UserMgmtITCase.java    From olat with Apache License 2.0 5 votes vote down vote up
@Test
public void testUserGroup() throws IOException {
    final HttpClient c = loginWithCookie("administrator", "olat");

    // retrieve all groups
    final String request = "/users/" + id1.getKey() + "/groups";
    final GetMethod method = createGet(request, MediaType.APPLICATION_JSON, true);
    final int code = c.executeMethod(method);
    assertEquals(code, 200);

    final String body = method.getResponseBodyAsString();
    final List<GroupVO> groups = parseGroupArray(body);
    assertNotNull(groups);
    assertTrue(groups.size() >= 4);
}
 
Example 18
Source File: CrucibleSessionImpl.java    From Crucible4IDEA with MIT License 5 votes vote down vote up
private String doDownloadFile(String relativeUrl) {
  String url = getHostUrl() + relativeUrl;
  final GetMethod method = new GetMethod(url);
  try {
    executeHttpMethod(method);
    return method.getResponseBodyAsString();
  }
  catch (IOException e) {
    throw new RuntimeException(e);
  }
}
 
Example 19
Source File: GroupMgmtITCase.java    From olat with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetGroupAdmin() throws IOException {
    final HttpClient c = loginWithCookie("administrator", "olat");

    final String request = "/groups/" + g1.getKey();
    final GetMethod method = createGet(request, MediaType.APPLICATION_JSON, true);
    final int code = c.executeMethod(method);
    assertEquals(code, 200);
    final String body = method.getResponseBodyAsString();
    method.releaseConnection();
    final GroupVO vo = parse(body, GroupVO.class);
    assertNotNull(vo);
    assertEquals(vo.getKey(), g1.getKey());
}
 
Example 20
Source File: HttpClientUtil.java    From Gather-Platform with GNU General Public License v3.0 5 votes vote down vote up
public String get(String url, String cookies) throws
            IOException {
//        clearCookies();
        GetMethod g = new GetMethod(url);
        g.setFollowRedirects(false);
        if (StringUtils.isNotEmpty(cookies)) {
            g.addRequestHeader("cookie", cookies);
        }
        hc.executeMethod(g);
        return g.getResponseBodyAsString();
    }