Example usage for org.apache.commons.httpclient HttpMethod setDoAuthentication

List of usage examples for org.apache.commons.httpclient HttpMethod setDoAuthentication

Introduction

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

Prototype

public abstract void setDoAuthentication(boolean paramBoolean);

Source Link

Usage

From source file:Correct.java

public static void main(String[] args) {

    String URLL = "";
    HttpClient client = new HttpClient();
    HttpMethod method = new PostMethod(URLL);
    method.setDoAuthentication(true);
    HostConfiguration hostConfig = client.getHostConfiguration();
    hostConfig.setHost("172.29.38.8");
    hostConfig.setProxy("172.29.90.4", 8);
    //   NTCredentials proxyCredentials = new NTCredentials("", "", "", "");
    client.getState().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials("", ""));
    ////        try {
    ////            URL url = new URL("");
    ////            HttpURLConnection urls = (HttpURLConnection) url.openConnection();
    ////            
    ////           
    ////        } catch (MalformedURLException ex) {
    ////            Logger.getLogger(Correct.class.getName()).log(Level.SEVERE, null, ex);
    ////        } catch (IOException ex) {
    ////            Logger.getLogger(Correct.class.getName()).log(Level.SEVERE, null, ex);
    ////        }
    try {//from  www  .jav  a2  s  . c  o  m
        // send the transaction
        client.executeMethod(hostConfig, method);
        StatusLine status = method.getStatusLine();

        if (status != null && method.getStatusCode() == 200) {

            System.out.println(method.getResponseBodyAsString() + "\n Status code : " + status);

        } else {

            System.err.println(method.getStatusLine() + "\n: Posting Failed !");
        }

    } catch (IOException ioe) {

        ioe.printStackTrace();

    }
    method.releaseConnection();

}

From source file:demo.jaxrs.client.Client.java

private static void setMethodHeaders(HttpMethod httpMethod, String name, String password) {
    if (httpMethod instanceof PostMethod || httpMethod instanceof PutMethod) {
        httpMethod.setRequestHeader("Content-Type", "application/xml");
    }/*  ww w .j  av a  2s .c  om*/
    httpMethod.setDoAuthentication(false);
    httpMethod.setRequestHeader("Accept", "application/xml");
    httpMethod.setRequestHeader("Authorization", "Basic " + base64Encode(name + ":" + password));

}

From source file:lucee.commons.net.http.httpclient3.HttpMethodCloner.java

/**
* Clones a HttpMethod. &ltbr>// w w w  .  j  av a2s  .co m
* &ltb&gtAttention:</b> You have to clone a method before it has
* been executed, because the URI can change if followRedirects
* is set to true.
*
* @param m the HttpMethod to clone
*
* @return the cloned HttpMethod, null if the HttpMethod could
* not be instantiated
*
* @throws java.io.IOException if the request body couldn't be read
*/
public static HttpMethod clone(HttpMethod m) {
    HttpMethod copy = null;
    try {
        copy = m.getClass().newInstance();
    } catch (InstantiationException iEx) {
    } catch (IllegalAccessException iaEx) {
    }
    if (copy == null) {
        return null;
    }
    copy.setDoAuthentication(m.getDoAuthentication());
    copy.setFollowRedirects(m.getFollowRedirects());
    copy.setPath(m.getPath());
    copy.setQueryString(m.getQueryString());

    Header[] h = m.getRequestHeaders();
    int size = (h == null) ? 0 : h.length;

    for (int i = 0; i < size; i++) {
        copy.setRequestHeader(new Header(h[i].getName(), h[i].getValue()));
    }
    copy.setStrictMode(m.isStrictMode());
    if (m instanceof HttpMethodBase) {
        copyHttpMethodBase((HttpMethodBase) m, (HttpMethodBase) copy);
    }
    if (m instanceof EntityEnclosingMethod) {
        copyEntityEnclosingMethod((EntityEnclosingMethod) m, (EntityEnclosingMethod) copy);
    }
    return copy;
}

From source file:com.eviware.soapui.impl.wsdl.submit.filters.HttpAuthenticationRequestFilter.java

public static void initRequestCredentials(SubmitContext context, String username, Settings settings,
        String password, String domain) {
    HttpClient httpClient = (HttpClient) context.getProperty(BaseHttpRequestTransport.HTTP_CLIENT);
    HttpMethod httpMethod = (HttpMethod) context.getProperty(BaseHttpRequestTransport.HTTP_METHOD);

    if (StringUtils.isNullOrEmpty(username) && StringUtils.isNullOrEmpty(password)) {
        httpClient.getParams().setAuthenticationPreemptive(false);
        httpMethod.setDoAuthentication(false);
    } else {// w w w.java  2 s  .  co  m
        // set preemptive authentication
        if (settings.getBoolean(HttpSettings.AUTHENTICATE_PREEMPTIVELY)) {
            httpClient.getParams().setAuthenticationPreemptive(true);
            HttpState state = (HttpState) context.getProperty(SubmitContext.HTTP_STATE_PROPERTY);

            if (state != null) {
                Credentials defaultcreds = new UsernamePasswordCredentials(username,
                        password == null ? "" : password);
                state.setCredentials(AuthScope.ANY, defaultcreds);
            }
        } else {
            httpClient.getParams().setAuthenticationPreemptive(false);
        }

        httpMethod.getParams().setParameter(CredentialsProvider.PROVIDER,
                new UPDCredentialsProvider(username, password, domain));

        httpMethod.setDoAuthentication(true);
    }
}

From source file:eu.eco2clouds.api.bonfire.client.rest.RestClient.java

private static Response executeMethod(HttpMethod httpMethod, String type, String username, String password,
        String url) {//from www .  j  av a 2s.  c o m
    HttpClient client = getClient(url, username, password);
    Response response = null;

    // We set the Heathers
    httpMethod.setDoAuthentication(true);
    httpMethod.addRequestHeader(BONFIRE_ASSERTED_ID_HEADER, username);
    //httpMethod.addRequestHeader(BONFIRE_ASSERTED_ID_HEADER, groups);
    httpMethod.addRequestHeader("Accept", type);
    httpMethod.addRequestHeader("Content-Type", type);

    try {
        int statusCode = client.executeMethod(httpMethod);
        String responsePayload = httpMethod.getResponseBodyAsString();
        response = new Response(statusCode, responsePayload);
    } catch (IOException iOException) {
        System.out.println("ERROR TRYING TO PERFORM GET TO: " + httpMethod.getPath());
        System.out.println("ERROR: " + iOException.getMessage());
        System.out.println("ERROR: " + iOException.getStackTrace());
    } finally {
        httpMethod.releaseConnection();
    }

    return response;
}

From source file:lucee.commons.net.http.httpclient3.HTTPEngine3Impl.java

private static void setCredentials(HttpClient client, HttpMethod httpMethod, String username, String password) {
    // set Username and Password
    if (username != null) {
        if (password == null)
            password = "";
        client.getState().setCredentials(null, null, new UsernamePasswordCredentials(username, password));
        httpMethod.setDoAuthentication(true);
    }/*from   www.  ja va 2 s  . co  m*/
}

From source file:com.feilong.tools.net.httpclient3.HttpClientUtil.java

/**
 *  authentication./*from  ww  w .jav a 2 s .co m*/
 * 
 * @param httpMethod
 *            the http method
 * @param httpClientConfig
 *            the http client config
 * @param httpClient
 *            the http client
 */
private static void setAuthentication(HttpMethod httpMethod, HttpClientConfig httpClientConfig,
        HttpClient httpClient) {
    UsernamePasswordCredentials usernamePasswordCredentials = httpClientConfig.getUsernamePasswordCredentials();
    // ?
    if (Validator.isNotNullOrEmpty(usernamePasswordCredentials)) {
        httpMethod.setDoAuthentication(true);

        AuthScope authScope = AuthScope.ANY;
        Credentials credentials = usernamePasswordCredentials;

        httpClient.getState().setCredentials(authScope, credentials);

        // 1.1?(Preemptive Authentication)
        // ??HttpClientbasic?????????
        // ???

        HttpClientParams httpClientParams = new HttpClientParams();
        httpClientParams.setAuthenticationPreemptive(true);
        httpClient.setParams(httpClientParams);
    }
}

From source file:davmail.http.DavGatewayHttpClientFacade.java

/**
 * Get Http Status code for the given URL
 *
 * @param httpClient httpClient instance
 * @param url        url string//  ww w.  j  a v  a2s  .  c o m
 * @return HttpStatus code
 */
public static int getHttpStatus(HttpClient httpClient, String url) {
    int status = 0;
    HttpMethod testMethod = new GetMethod(url);
    testMethod.setDoAuthentication(false);
    try {
        status = httpClient.executeMethod(testMethod);
    } catch (IOException e) {
        LOGGER.warn(e.getMessage(), e);
    } finally {
        testMethod.releaseConnection();
    }
    return status;
}

From source file:com.bt.aloha.batchtest.v2.RemoteTomcatStackRunner.java

private String getResponse(String uri) {
    HttpClient httpClient = new HttpClient();
    httpClient.getState().setCredentials(AuthScope.ANY,
            new UsernamePasswordCredentials(getUsername(), getPassword()));
    HttpMethod getMethod = new GetMethod(uri);
    getMethod.setDoAuthentication(true);

    try {//  ww w.  j  a  v  a  2s  .  c om
        int rc = httpClient.executeMethod(getMethod);
        LOG.debug(rc);
        if (rc != 200)
            throw new RuntimeException(String.format("bad http response from Tomcat: %d", rc));
        return getMethod.getResponseBodyAsString();
    } catch (HttpException e) {
        LOG.warn(e);
        throw new RuntimeException(e);
    } catch (IOException e) {
        LOG.warn(e);
        throw new RuntimeException(e);
    }
}

From source file:fedora.test.integration.TestLargeDatastreams.java

private long exportAPIALite(String dsId) throws Exception {
    String url = apia.describeRepository().getRepositoryBaseURL() + "/get/" + pid + "/" + dsId;
    HttpMethod httpMethod = new GetMethod(url);
    httpMethod.setDoAuthentication(true);
    httpMethod.getParams().setParameter("Connection", "Keep-Alive");

    HttpClient client = fedoraClient.getHttpClient();
    client.executeMethod(httpMethod);/*www.j  ava 2  s  .com*/
    BufferedInputStream dataStream = new BufferedInputStream(httpMethod.getResponseBodyAsStream());

    long bytesRead = 0;
    while (dataStream.read() >= 0) {
        ++bytesRead;
    }

    return bytesRead;
}