Search in sources :

Example 41 with HttpClient

use of org.apache.commons.httpclient.HttpClient in project pinpoint by naver.

the class HttpClientIT method test.

@Test
public void test() throws Exception {
    HttpClient client = new HttpClient();
    client.getParams().setConnectionManagerTimeout(CONNECTION_TIMEOUT);
    client.getParams().setSoTimeout(SO_TIMEOUT);
    GetMethod method = new GetMethod("http://google.com");
    // Provide custom retry handler is necessary
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
    method.setQueryString(new NameValuePair[] { new NameValuePair("key2", "value2") });
    try {
        // Execute the method.
        client.executeMethod(method);
    } catch (Exception ignored) {
    } finally {
        method.releaseConnection();
    }
    PluginTestVerifier verifier = PluginTestVerifierHolder.getInstance();
    verifier.printCache();
}
Also used : NameValuePair(org.apache.commons.httpclient.NameValuePair) HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) DefaultHttpMethodRetryHandler(org.apache.commons.httpclient.DefaultHttpMethodRetryHandler) PluginTestVerifier(com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier) Test(org.junit.Test)

Example 42 with HttpClient

use of org.apache.commons.httpclient.HttpClient in project openhab1-addons by openhab.

the class KM200Comm method sendDataToService.

/**
     * This function does the SEND http communication to the device
     *
     */
public Integer sendDataToService(String service, byte[] data) {
    // Create an instance of HttpClient.
    Integer rCode = null;
    if (client == null) {
        client = new HttpClient();
    }
    synchronized (client) {
        // Create a method instance.
        PostMethod method = new PostMethod("http://" + device.getIP4Address() + service);
        // Provide custom retry handler is necessary
        method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false));
        // Set the right header
        method.setRequestHeader("Accept", "application/json");
        method.addRequestHeader("User-Agent", "TeleHeater/2.2.3");
        method.setRequestEntity(new ByteArrayRequestEntity(data));
        try {
            rCode = client.executeMethod(method);
        } catch (Exception e) {
            logger.error("Failed to send data {}", e);
        } finally {
            // Release the connection.
            method.releaseConnection();
        }
        return rCode;
    }
}
Also used : PostMethod(org.apache.commons.httpclient.methods.PostMethod) HttpClient(org.apache.commons.httpclient.HttpClient) DefaultHttpMethodRetryHandler(org.apache.commons.httpclient.DefaultHttpMethodRetryHandler) JSONException(org.json.JSONException) GeneralSecurityException(java.security.GeneralSecurityException) HttpException(org.apache.commons.httpclient.HttpException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ByteArrayRequestEntity(org.apache.commons.httpclient.methods.ByteArrayRequestEntity)

Example 43 with HttpClient

use of org.apache.commons.httpclient.HttpClient in project tdi-studio-se by Talend.

the class WSDLLocatorImpl method createHttpClient.

private HttpClient createHttpClient() {
    HttpClient httpClient = new HttpClient();
    if (configuration.getProxyServer() != null) {
        HostConfiguration hostConfiguration = new HostConfiguration();
        hostConfiguration.setProxy(configuration.getProxyServer(), configuration.getProxyPort());
        httpClient.setHostConfiguration(hostConfiguration);
    }
    if (configuration.getUsername() != null) {
        Credentials credentials = new UsernamePasswordCredentials(configuration.getUsername(), configuration.getPassword());
        httpClient.getState().setCredentials(AuthScope.ANY, credentials);
    }
    if (configuration.getProxyUsername() != null) {
        Credentials credentials = new UsernamePasswordCredentials(configuration.getProxyUsername(), configuration.getProxyPassword());
        httpClient.getState().setProxyCredentials(AuthScope.ANY, credentials);
        httpClient.getHostConfiguration().setProxy(configuration.getProxyServer(), configuration.getProxyPort());
    }
    return httpClient;
}
Also used : HostConfiguration(org.apache.commons.httpclient.HostConfiguration) HttpClient(org.apache.commons.httpclient.HttpClient) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials) Credentials(org.apache.commons.httpclient.Credentials) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials)

Example 44 with HttpClient

use of org.apache.commons.httpclient.HttpClient in project tdi-studio-se by Talend.

the class SpagoBITalendEngineClient_0_5_0 method deployJob.

/*
     * (non-Javadoc)
     * 
     * @see
     * it.eng.spagobi.engines.talend.client.ISpagoBITalendEngineClient#deployJob(it.eng.spagobi.engines.talend.client
     * .JobDeploymentDescriptor, java.io.File)
     */
public boolean deployJob(JobDeploymentDescriptor jobDeploymentDescriptor, File executableJobFiles) throws EngineUnavailableException, AuthenticationFailedException, ServiceInvocationFailedException {
    HttpClient client;
    PostMethod method;
    File deploymentDescriptorFile;
    boolean result = false;
    client = new HttpClient();
    method = new PostMethod(getServiceUrl(JOB_UPLOAD_SERVICE));
    deploymentDescriptorFile = null;
    try {
        //$NON-NLS-1$ //$NON-NLS-2$
        deploymentDescriptorFile = File.createTempFile("deploymentDescriptor", ".xml");
        FileWriter writer = new FileWriter(deploymentDescriptorFile);
        writer.write(jobDeploymentDescriptor.toXml());
        writer.flush();
        writer.close();
        Part[] parts = { new FilePart(executableJobFiles.getName(), executableJobFiles), //$NON-NLS-1$
        new FilePart("deploymentDescriptor", deploymentDescriptorFile) };
        method.setRequestEntity(new MultipartRequestEntity(parts, method.getParams()));
        client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
        int status = client.executeMethod(method);
        if (status == HttpStatus.SC_OK) {
            if (//$NON-NLS-1$
            method.getResponseBodyAsString().equalsIgnoreCase("OK"))
                result = true;
        } else {
            throw new ServiceInvocationFailedException(Messages.getString("SpagoBITalendEngineClient_0_5_0.serviceExcFailed") + JOB_UPLOAD_SERVICE, //$NON-NLS-1$
            method.getStatusLine().toString(), method.getResponseBodyAsString());
        }
    } catch (HttpException e) {
        //$NON-NLS-1$
        throw new EngineUnavailableException(Messages.getString("SpagoBITalendEngineClient_0_5_0.protocolViolation") + e.getMessage());
    } catch (IOException e) {
        //$NON-NLS-1$
        throw new EngineUnavailableException(Messages.getString("SpagoBITalendEngineClient_0_5_0.transportError") + e.getMessage());
    } finally {
        method.releaseConnection();
        if (deploymentDescriptorFile != null)
            deploymentDescriptorFile.delete();
    }
    return result;
}
Also used : ServiceInvocationFailedException(it.eng.spagobi.engines.talend.client.exception.ServiceInvocationFailedException) PostMethod(org.apache.commons.httpclient.methods.PostMethod) FileWriter(java.io.FileWriter) IOException(java.io.IOException) MultipartRequestEntity(org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity) FilePart(org.apache.commons.httpclient.methods.multipart.FilePart) EngineUnavailableException(it.eng.spagobi.engines.talend.client.exception.EngineUnavailableException) FilePart(org.apache.commons.httpclient.methods.multipart.FilePart) Part(org.apache.commons.httpclient.methods.multipart.Part) HttpClient(org.apache.commons.httpclient.HttpClient) HttpException(org.apache.commons.httpclient.HttpException) File(java.io.File)

Example 45 with HttpClient

use of org.apache.commons.httpclient.HttpClient in project tdi-studio-se by Talend.

the class MDMTransactionClient method getSessionID.

public static String getSessionID(String url, String username, String password) throws IOException {
    HttpClient client = new HttpClient();
    client.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
    client.getParams().setAuthenticationPreemptive(true);
    GetMethod get = new GetMethod(url);
    get.setDoAuthentication(true);
    String sessionID;
    try {
        client.executeMethod(get);
        sessionID = parseSessionID(get);
    } catch (HttpException e) {
        throw e;
    } catch (IOException e) {
        throw e;
    } finally {
        get.releaseConnection();
    }
    return sessionID;
}
Also used : HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) HttpException(org.apache.commons.httpclient.HttpException) IOException(java.io.IOException) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials)

Aggregations

HttpClient (org.apache.commons.httpclient.HttpClient)399 GetMethod (org.apache.commons.httpclient.methods.GetMethod)221 PostMethod (org.apache.commons.httpclient.methods.PostMethod)120 HttpMethod (org.apache.commons.httpclient.HttpMethod)98 Test (org.junit.Test)87 IOException (java.io.IOException)82 InputStream (java.io.InputStream)70 HttpException (org.apache.commons.httpclient.HttpException)44 JSONParser (org.json.simple.parser.JSONParser)44 JSONObject (org.json.simple.JSONObject)40 Map (java.util.Map)35 ArrayList (java.util.ArrayList)27 HashMap (java.util.HashMap)25 HttpState (org.apache.commons.httpclient.HttpState)25 ZMailbox (com.zimbra.client.ZMailbox)23 Account (com.zimbra.cs.account.Account)22 StringRequestEntity (org.apache.commons.httpclient.methods.StringRequestEntity)22 ServiceException (com.zimbra.common.service.ServiceException)21 JSONArray (org.json.simple.JSONArray)21 ByteArrayRequestEntity (org.apache.commons.httpclient.methods.ByteArrayRequestEntity)20