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

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

Introduction

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

Prototype

public HttpClient() 

Source Link

Usage

From source file:es.carebear.rightmanagement.client.RestClient.java

public User registerTest(String username, String password) throws IOException {

    HttpClient client = new HttpClient();

    PostMethod method = new PostMethod(getServiceBaseURI() + "/client/users/register/" + username);

    method.addParameter("password", password);

    System.out.println(method.getPath());

    int responseCode = client.executeMethod(method);

    String response = responseToString(method.getResponseBodyAsStream());

    System.out.println(response);

    return XMLHelper.fromXML(response, User.class);
}

From source file:net.jadler.JadlerDeprecatedDefaultsConfigurationTest.java

@Test
@SuppressWarnings("deprecation")
public void ongoingConfiguration() throws IOException {
    initJadler().that().respondsWithDefaultStatus(EXPECTED_STATUS)
            .respondsWithDefaultContentType(EXPECTED_CONTENT_TYPE)
            .respondsWithDefaultEncoding(EXPECTED_ENCODING)
            .respondsWithDefaultHeader(EXPECTED_HEADER_NAME, EXPECTED_HEADER_VALUE);

    try {/*  w  w w. j a v  a  2 s. co m*/
        onRequest().respond().withBody(STRING_WITH_DIACRITICS);

        final HttpClient client = new HttpClient();
        final GetMethod method = new GetMethod("http://localhost:" + port() + "/");
        client.executeMethod(method);

        assertThat(method.getStatusCode(), is(EXPECTED_STATUS));
        assertThat(method.getResponseHeader("Content-Type").getValue(), is(EXPECTED_CONTENT_TYPE));
        assertThat(method.getResponseHeader(EXPECTED_HEADER_NAME).getValue(), is(EXPECTED_HEADER_VALUE));
        assertThat(method.getResponseBody(), is(ISO_8859_2_REPRESENTATION));

        method.releaseConnection();
    } finally {
        closeJadler();
    }
}

From source file:ca.uvic.cs.tagsea.wizards.TagNetworkSender.java

public static synchronized void send(String xml, IProgressMonitor sendMonitor, int id)
        throws InvocationTargetException {
    sendMonitor.beginTask("Uploading Tags", 100);
    sendMonitor.subTask("Saving Temporary file...");
    File location = TagSEAPlugin.getDefault().getStateLocation().toFile();
    File temp = new File(location, "tagsea.temp.file.txt");
    if (temp.exists()) {
        String message = "Unable to send tags. Unable to create temporary file.";
        IOException ex = new IOException(message);
        throw (new InvocationTargetException(ex, message));
    }//from   w  w w  .j a  v a2s  .co  m
    try {
        FileWriter writer = new FileWriter(temp);
        writer.write(xml);
        writer.flush();
        writer.close();
    } catch (IOException e) {
        throw new InvocationTargetException(e);
    }
    sendMonitor.worked(5);
    sendMonitor.subTask("Uploading Tags...");
    String uploadScript = "http://stgild.cs.uvic.ca/cgi-bin/tagSEAUpload.cgi";
    PostMethod post = new PostMethod(uploadScript);

    String fileName = getToday() + ".txt";
    try {
        Part[] parts = { new StringPart("KIND", "tag"), new FilePart("MYLAR" + id, fileName, temp) };
        post.setRequestEntity(new MultipartRequestEntity(parts, post.getParams()));
        HttpClient client = new HttpClient();
        int status = client.executeMethod(post);
        String resp = getData(post.getResponseBodyAsStream());
        if (status != 200) {
            IOException ex = new IOException(resp);
            throw (ex);
        }
    } catch (IOException e) {
        throw new InvocationTargetException(e, e.getLocalizedMessage());
    } finally {
        sendMonitor.worked(90);
        sendMonitor.subTask("Deleting Temporary File");
        temp.delete();
        sendMonitor.done();
    }

}

From source file:com.epam.wilma.gepard.test.messagemarker.MessageMarkingSwitch.java

/**
 * Set message marking to on/off./* w  w  w. j  a v  a 2  s  .  c o m*/
 *
 * @param tc is the caller Test Case
 * @param host Wilma host:port string
 * @param state is the expected state of the switch
 * @return with response code
 * @throws Exception in case of error
 */
public String setMessageMarkingModeTo(final WilmaTestLogDecorator tc, final String host, final String state)
        throws Exception {
    String url = String.format(URL_TEMPLATE, host, state);
    tc.logStep("Switching message marking in Wilma to: " + url.split("messagemarking/")[1]);
    tc.logGetRequestEvent(url);

    HttpClient httpClient = new HttpClient();
    GetMethod httpGet = new GetMethod(url);
    int statusCode = httpClient.executeMethod(httpGet);
    String responseCode = "status code: " + statusCode + "\n";
    if (statusCode != STATUS_OK) {
        tc.naTestCase("Message marking switch failed.");
    }
    tc.logComment("Message marking is: " + url.split("messagemarking/")[1]);
    return responseCode;
}

From source file:com.epam.wilma.gepard.test.localhost.BlockLocalhostUsageSwitch.java

/**
 * Set localhost blocking to on/off./*  w w w .  j a  va 2 s .  c o m*/
 *
 * @param tc is the caller Test Case
 * @param host Wilma host:port string
 * @param state is the expected state of the switch
 * @return with response code
 * @throws Exception in case of error
 */
public String setLocalhostBlockingModeTo(final WilmaTestLogDecorator tc, final String host, final String state)
        throws Exception {
    String url = String.format(URL_TEMPLATE, host, state);
    tc.logStep("Switching localhost blocking in Wilma to: " + url.split("localhost/")[1]);
    tc.logGetRequestEvent(url);

    HttpClient httpClient = new HttpClient();
    GetMethod httpGet = new GetMethod(url);
    int statusCode = httpClient.executeMethod(httpGet);
    String responseCode = "status code: " + statusCode + "\n";
    if (statusCode != STATUS_OK) {
        tc.naTestCase("Localhost blocking switch failed.");
    }
    tc.logComment("Localhost blocking is: " + url.split("localhost/")[1]);
    return responseCode;
}

From source file:cn.newtouch.util.HttpClientUtil.java

/**
 * ?url??get/*from   ww  w  .  java 2s  .c o m*/
 * 
 * @param url
 */
public static String reqUrl(String url) {
    HttpClient client = new HttpClient();
    GetMethod method = new GetMethod(url);
    try {
        int returnCode = client.executeMethod(method);
        if (returnCode == HttpStatus.SC_NOT_IMPLEMENTED) {
            System.err.println("The Post method is not implemented by this URI");
        } else {
            return method.getResponseBodyAsString();

        }
    } catch (Exception e) {
        System.err.println(e);
    }
    return null;
}

From source file:com.predic8.membrane.core.interceptor.rewrite.RewriteInterceptorIntegrationTest.java

@Test
public void testRewriting() throws Exception {
    HttpClient client = new HttpClient();
    client.getParams().setParameter(HttpProtocolParams.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
    int status = client.executeMethod(getPostMethod());

    assertEquals(200, status);//from  w w w .  j a v a2  s  . c  o  m
}

From source file:com.manning.blogapps.chapter10.metaweblogclient.MetaWeblogResource.java

public InputStream getAsStream() throws BlogClientException {
    HttpClient httpClient = new HttpClient();
    GetMethod method = new GetMethod(url);
    try {/*w ww.j a  v a2s. c  o m*/
        httpClient.executeMethod(method);
    } catch (Exception e) {
        throw new BlogClientException("ERROR: error reading file");
    }
    if (method.getStatusCode() != 200) {
        throw new BlogClientException("ERROR HTTP status=" + method.getStatusCode());
    }
    try {
        return method.getResponseBodyAsStream();
    } catch (Exception e) {
        throw new BlogClientException("ERROR: error reading file");
    }
}

From source file:io.seldon.client.test.js.BaseJavascriptTest.java

@Before
public void setup() {
    // Conditionally run this unit:
    Assume.assumeTrue(testState.isEnabled());

    client = new HttpClient();
    objectMapper = new ObjectMapper();
}

From source file:de.maklerpoint.office.Bugzilla.BugzillaReport.java

public Map login() {
    try {//from www . j a  va  2s .c o  m
        XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
        config.setServerURL(new URL(serverURL));

        HttpClient httpClient = new HttpClient();
        rpcClient = new XmlRpcClient();
        XmlRpcCommonsTransportFactory factory = new XmlRpcCommonsTransportFactory(rpcClient);
        factory.setHttpClient(httpClient);
        rpcClient.setTransportFactory(factory);

        rpcClient.setConfig(config);

        //            ArrayList<Object> params = new ArrayList<Object>();

        Map map = new HashMap();
        map.put("login", login);
        map.put("password", password);
        map.put("rememberlogin", "Bugzilla_remember");

        //            executionData.put("password", password);
        //            executionData.put("remember", true);
        //            params.add(executionData);

        Map result = (Map) rpcClient.execute("User.login", new Object[] { map });

        Logger.getLogger(getClass()).debug("Bugreport Datenbank id: " + result);

        for (Object name : result.keySet()) {
            //                System.out.println("Name: " + name);
            //                System.out.println("Nummer: " + result.get(name));
            id = Integer.valueOf(result.get(name).toString());
        }

        return result;
    } catch (Exception e) {
        Logger.getLogger(getClass()).fatal("Fehler beim senden des Bugreports", e);
    }
    return null;
}