Search in sources :

Example 46 with GetMethod

use of org.apache.commons.httpclient.methods.GetMethod in project h2o-2 by h2oai.

the class HttpTest method get.

public Get get(String uri, Class c) {
    GetMethod get = new GetMethod("http://127.0.0.1:54321/" + uri);
    Get res = new Get();
    try {
        res._status = _client.executeMethod(get);
        if (res._status == 200) {
            Gson gson = new Gson();
            res._res = gson.fromJson(new InputStreamReader(get.getResponseBodyAsStream()), c);
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    get.releaseConnection();
    return res;
}
Also used : InputStreamReader(java.io.InputStreamReader) GetMethod(org.apache.commons.httpclient.methods.GetMethod) Gson(dontweave.gson.Gson)

Example 47 with GetMethod

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

the class ExchangeUtils method sendGetRequest.

public static String sendGetRequest(String urlAddress) throws Exception {
    HttpClient httpclient = new HttpClient();
    GetMethod getMethod = new GetMethod(urlAddress);
    TransportClientProperties tcp = TransportClientPropertiesFactory.create("http");
    if (tcp.getProxyHost().length() != 0) {
        UsernamePasswordCredentials creds = new UsernamePasswordCredentials(tcp.getProxyUser() != null ? tcp.getProxyUser() : "", tcp.getProxyPassword() != null ? tcp.getProxyUser() : "");
        httpclient.getState().setProxyCredentials(AuthScope.ANY, creds);
        HostConfiguration hcf = new HostConfiguration();
        hcf.setProxy(tcp.getProxyHost(), Integer.parseInt(tcp.getProxyPort()));
        httpclient.executeMethod(hcf, getMethod);
    } else {
        httpclient.executeMethod(getMethod);
    }
    String response = getMethod.getResponseBodyAsString();
    getMethod.releaseConnection();
    return response;
}
Also used : HostConfiguration(org.apache.commons.httpclient.HostConfiguration) HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) TransportClientProperties(org.apache.axis.components.net.TransportClientProperties) UsernamePasswordCredentials(org.apache.commons.httpclient.UsernamePasswordCredentials)

Example 48 with GetMethod

use of org.apache.commons.httpclient.methods.GetMethod 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)

Example 49 with GetMethod

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

the class WSDLLocatorImpl method getImportInputSource.

public InputSource getImportInputSource(String parentLocation, String importLocation) {
    try {
        String contextURI = parentLocation;
        URL contextURL = (contextURI != null) ? getURL(null, contextURI) : null;
        URL url = getURL(contextURL, importLocation);
        latestImportUri = url.toExternalForm();
        GetMethod get = createGedMethod(latestImportUri);
        httpClient.executeMethod(get);
        InputStream is = get.getResponseBodyAsStream();
        inputStreams.add(is);
        return new InputSource(is);
    } catch (MalformedURLException ex) {
        throw new RuntimeException(ex);
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
}
Also used : InputSource(org.xml.sax.InputSource) MalformedURLException(java.net.MalformedURLException) InputStream(java.io.InputStream) GetMethod(org.apache.commons.httpclient.methods.GetMethod) IOException(java.io.IOException) URL(java.net.URL)

Example 50 with GetMethod

use of org.apache.commons.httpclient.methods.GetMethod in project zm-mailbox by Zimbra.

the class ZMailbox method getRESTResource.

private InputStream getRESTResource(String relativePath, String startTimeArg, String endTimeArg, int msecTimeout, String alternateUrl) throws ServiceException {
    GetMethod get = null;
    URI uri = null;
    int statusCode;
    try {
        if (startTimeArg != null) {
            String encodedArg = URLEncoder.encode(startTimeArg, "UTF-8");
            if (!relativePath.contains("?")) {
                relativePath = relativePath + "?start=" + encodedArg;
            } else {
                relativePath = relativePath + "&start=" + encodedArg;
            }
        }
        if (endTimeArg != null) {
            String encodedArg = URLEncoder.encode(endTimeArg, "UTF-8");
            if (!relativePath.contains("?")) {
                relativePath = relativePath + "?end=" + encodedArg;
            } else {
                relativePath = relativePath + "&end=" + encodedArg;
            }
        }
        uri = getRestURI(relativePath, alternateUrl);
        HttpClient client = getHttpClient(uri);
        get = new GetMethod(uri.toString());
        if (msecTimeout > -1) {
            get.getParams().setSoTimeout(msecTimeout);
        }
        statusCode = HttpClientUtil.executeMethod(client, get);
        // parse the response
        if (statusCode == HttpServletResponse.SC_OK) {
            return new GetMethodInputStream(get);
        } else {
            String msg = String.format("GET from %s failed, status=%d.  %s", uri.toString(), statusCode, get.getStatusText());
            throw ServiceException.FAILURE(msg, null);
        }
    } catch (IOException e) {
        String fromUri = "";
        if (uri != null) {
            fromUri = " from " + uri.toString();
        }
        String msg = String.format("Unable to get REST resource%s: %s", fromUri, e.getMessage());
        throw ZClientException.IO_ERROR(msg, e);
    }
}
Also used : HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) IOException(java.io.IOException) URI(java.net.URI)

Aggregations

GetMethod (org.apache.commons.httpclient.methods.GetMethod)357 HttpClient (org.apache.commons.httpclient.HttpClient)216 HttpMethod (org.apache.commons.httpclient.HttpMethod)93 IOException (java.io.IOException)82 Test (org.junit.Test)70 InputStream (java.io.InputStream)65 HttpException (org.apache.commons.httpclient.HttpException)41 Map (java.util.Map)39 ArrayList (java.util.ArrayList)37 HashMap (java.util.HashMap)36 JSONParser (org.json.simple.parser.JSONParser)34 JSONObject (org.json.simple.JSONObject)31 Header (org.apache.commons.httpclient.Header)22 Element (org.w3c.dom.Element)22 PostMethod (org.apache.commons.httpclient.methods.PostMethod)21 TypeToken (com.google.gson.reflect.TypeToken)20 List (java.util.List)19 HttpState (org.apache.commons.httpclient.HttpState)18 URI (java.net.URI)17 JSONArray (org.json.simple.JSONArray)17