Example usage for org.apache.commons.httpclient HttpClient getHttpConnectionManager

List of usage examples for org.apache.commons.httpclient HttpClient getHttpConnectionManager

Introduction

In this page you can find the example usage for org.apache.commons.httpclient HttpClient getHttpConnectionManager.

Prototype

public HttpConnectionManager getHttpConnectionManager()

Source Link

Usage

From source file:com.cognifide.maven.plugins.crx.CrxPackageAbstractMojo.java

/**
 * Get the http client/*from ww  w  .j a va 2  s .  c  o m*/
 */
protected HttpClient getHttpClient() {
    final HttpClient client = new HttpClient();
    client.getHttpConnectionManager().getParams().setConnectionTimeout(HTTP_CONNECTION_TIMEOUT);

    // authentication stuff
    client.getParams().setAuthenticationPreemptive(true);
    Credentials defaultcreds = new UsernamePasswordCredentials(user, password);
    client.getState().setCredentials(AuthScope.ANY, defaultcreds);

    return client;
}

From source file:com.zb.app.external.wechat.service.WeixinService.java

public void upload(File file, String type) {
    StringBuilder sb = new StringBuilder(400);
    sb.append("http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=");
    sb.append(getAccessToken());/*from  w ww . j  av  a2s .co m*/
    sb.append("&type=").append(type);

    PostMethod postMethod = new PostMethod(sb.toString());
    try {
        // FilePart?
        FilePart fp = new FilePart("filedata", file);
        Part[] parts = { fp };
        // MIMEhttpclientMulitPartRequestEntity
        MultipartRequestEntity mre = new MultipartRequestEntity(parts, postMethod.getParams());
        postMethod.setRequestEntity(mre);
        HttpClient client = new HttpClient();
        client.getHttpConnectionManager().getParams().setConnectionTimeout(50000);// 
        int status = client.executeMethod(postMethod);
        if (status == HttpStatus.SC_OK) {
            logger.error(postMethod.getResponseBodyAsString());
        } else {
            logger.error("fail");
        }
        byte[] responseBody = postMethod.getResponseBody();
        String result = new String(responseBody, "utf-8");
        logger.error("result : " + result);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        // 
        postMethod.releaseConnection();
    }
}

From source file:com.google.apphosting.vmruntime.jetty9.VmRuntimeJettySessionTest.java

/**
 * Test that sessions are persisted in the datastore and memcache, and that
 * any data stored is available to subsequent requests.
 *
 * @throws Exception//from w  w w . j a  v a 2 s .c o  m
 */
public void testSessions() throws Exception {
    URL url = createUrl("/count?type=session");

    FakeableVmApiProxyDelegate fakeApiProxy = new FakeableVmApiProxyDelegate();
    ApiProxy.setDelegate(fakeApiProxy);

    // Add responses for session create.
    fakeApiProxy.addApiResponse(createDatastorePutResponse());
    fakeApiProxy.addApiResponse(MemcacheSetResponse.newBuilder().addSetStatus(SetStatusCode.STORED).build());
    // Add responses for session save.
    fakeApiProxy.addApiResponse(createDatastorePutResponse());
    fakeApiProxy.addApiResponse(MemcacheSetResponse.newBuilder().addSetStatus(SetStatusCode.STORED).build());

    // Make a call to count and save the session cookie.
    HttpClient httpClient = new HttpClient();
    httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(30000);
    GetMethod firstGet = new GetMethod(url.toString());
    int returnCode = httpClient.executeMethod(firstGet);
    assertEquals(HttpServletResponse.SC_OK, returnCode);
    // Check the count, should be 1 for the first request.
    assertEquals("1", firstGet.getResponseBodyAsString().trim());

    // Parse the memcache put request so we can respond to the next get request.
    MemcacheSetRequest setRequest = MemcacheSetRequest
            .parseFrom(fakeApiProxy.getLastRequest().getRequestData());
    assertEquals(1, setRequest.getItemCount());
    Item responsePayload = Item.newBuilder().setKey(setRequest.getItem(0).getKey())
            .setValue(setRequest.getItem(0).getValue()).build();
    MemcacheGetResponse getResponse = MemcacheGetResponse.newBuilder().addItem(responsePayload).build();
    fakeApiProxy.addApiResponse(getResponse);

    // Add responses for session save.
    fakeApiProxy.addApiResponse(createDatastorePutResponse());
    fakeApiProxy.addApiResponse(MemcacheSetResponse.newBuilder().addSetStatus(SetStatusCode.STORED).build());

    // Make a call to count with the session cookie.
    GetMethod secondGet = new GetMethod(url.toString());
    returnCode = httpClient.executeMethod(secondGet);
    assertEquals(HttpServletResponse.SC_OK, returnCode);
    // Check the count, should be "2" for the second request.
    assertEquals("2", secondGet.getResponseBodyAsString().trim());
}

From source file:com.atlassian.jira.web.action.setup.SetupProductBundleHelper.java

private HttpClient prepareClient() {
    final HttpClient httpClient = new HttpClient();

    httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(10000);
    httpClient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
    httpClient.getState().addCookies(getCookies());

    return httpClient;
}

From source file:com.google.apphosting.vmruntime.jetty9.VmRuntimeJettyKitchenSinkTest.java

public void testWithUntrustedInboundIp() throws Exception {
    HttpClient httpClient = new HttpClient();
    httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(30000);
    GetMethod get = new GetMethod(createUrlForHostIP("/test-ssl").toString());
    int httpCode = httpClient.executeMethod(get);
    assertEquals(200, httpCode);/*from   www  .j a v  a2  s. c  o m*/
}

From source file:com.google.apphosting.vmruntime.jetty9.VmRuntimeJettyKitchenSinkTest.java

public void testSsl_NoSSL() throws Exception {
    HttpClient httpClient = new HttpClient();
    httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(30000);
    GetMethod get = new GetMethod(createUrl("/test-ssl").toString());
    int httpCode = httpClient.executeMethod(get);
    assertEquals(200, httpCode);//  w ww .  j  a v a 2s  .com
    String expected = "false:http:http://localhost:" + port + "/test-ssl";
    assertEquals(expected, get.getResponseBodyAsString());
}

From source file:com.google.apphosting.vmruntime.jetty9.VmRuntimeJettyKitchenSinkTest.java

public void testSsl_WithSSL() throws Exception {
    HttpClient httpClient = new HttpClient();
    httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(30000);
    GetMethod get = new GetMethod(createUrl("/test-ssl").toString());
    get.addRequestHeader(VmApiProxyEnvironment.HTTPS_HEADER, "on");
    int httpCode = httpClient.executeMethod(get);
    assertEquals(200, httpCode);// w  w  w  . j  a va  2  s  .co  m
    assertEquals("true:https:https://localhost/test-ssl", get.getResponseBodyAsString());
}

From source file:com.taobao.ad.easyschedule.action.schedule.ScheduleAction.java

private SchedulerDTO getSchedulerInfo(String instanceName) {
    SchedulerDTO result = null;/*from  www  .j  a va 2 s. com*/
    try {
        HttpClient client = new HttpClient();
        int connTimeout = 1000;
        client.getHttpConnectionManager().getParams().setConnectionTimeout(connTimeout);
        GetMethod getMethod = null;
        String url = configBO.getStringValue(ConfigDO.CONFIG_KEY_INSTANCE_CONTROL_INTERFACE);
        url = url.replaceAll("#instance#", instanceName);
        url = url + "getInstanceAjax";
        getMethod = new GetMethod(url);
        getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
        int statusCode = client.executeMethod(getMethod);
        if (statusCode == HttpStatus.SC_OK) {
            result = JSONObject.parseObject(getMethod.getResponseBodyAsString(), SchedulerDTO.class);
        }
    } catch (Exception e) {
    }
    return result;
}

From source file:at.ac.tuwien.auto.sewoa.http.HttpRetrieverImpl.java

public byte[] retrieveData(String url) {
    byte[] result = new byte[] {};
    HttpClient httpClient = httpClientFactory.createClient();
    GetMethod getMethod = new GetMethod(url);

    HttpConnectionManagerParams params = httpClient.getHttpConnectionManager().getParams();
    params.setConnectionTimeout((int) TimeUnit.SECONDS.toMillis(CONNECTION_TO));
    params.setSoTimeout((int) TimeUnit.SECONDS.toMillis(SOCKET_TO));
    getMethod.addRequestHeader("Content-Type", "application/xml");
    getMethod.addRequestHeader("Accept", "application/xml");

    try {//from w w w .ja va2 s  . co  m
        result = send(httpClient, getMethod);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

From source file:com.google.apphosting.vmruntime.jetty9.VmRuntimeJettyKitchenSinkTest.java

public void testAsyncRequests_WaitUntilDone() throws Exception {
    long sleepTime = 2000;
    FakeableVmApiProxyDelegate fakeApiProxy = new FakeableVmApiProxyDelegate();
    ApiProxy.setDelegate(fakeApiProxy);//from  ww w .ja  va 2  s. c  om
    HttpClient httpClient = new HttpClient();
    httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
    GetMethod get = new GetMethod(createUrl("/sleep").toString());
    get.addRequestHeader("Use-Async-Sleep-Api", "true");
    get.addRequestHeader("Sleep-Time", Long.toString(sleepTime));
    long startTime = System.currentTimeMillis();
    int httpCode = httpClient.executeMethod(get);
    assertEquals(200, httpCode);
    Header vmApiWaitTime = get.getResponseHeader(VmRuntimeUtils.ASYNC_API_WAIT_HEADER);
    assertNotNull(vmApiWaitTime);
    assertTrue(Integer.parseInt(vmApiWaitTime.getValue()) > 0);
    long elapsed = System.currentTimeMillis() - startTime;
    assertTrue(elapsed >= sleepTime);
}