Example usage for org.apache.commons.httpclient HttpStatus SC_OK

List of usage examples for org.apache.commons.httpclient HttpStatus SC_OK

Introduction

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

Prototype

int SC_OK

To view the source code for org.apache.commons.httpclient HttpStatus SC_OK.

Click Source Link

Document

<tt>200 OK</tt> (HTTP/1.0 - RFC 1945)

Usage

From source file:HttpClientWrapper.java

private int doGet(GetMethod method) throws Exception {
    int statusCode = client.executeMethod(method);
    if (statusCode != HttpStatus.SC_OK) {
        System.err.println("Method failed: " + method.getStatusLine());
    }//from  w  ww. j av  a  2  s  .com
    return statusCode;
}

From source file:edu.utk.gsda.CalaisHttpClient.java

public String getAnnotatedResult(String originString) {
    try {/*from  w  w  w.  j  a  v  a  2 s .c  o  m*/
        client = new HttpClient();
        client.getParams().setParameter("http.useragent", "Calais Rest Client");

        initMethod();

        method.setRequestEntity(new StringRequestEntity(originString, null, null));
        int returnCode = client.executeMethod(method);

        if (returnCode == HttpStatus.SC_OK) {
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(method.getResponseBodyAsStream(), "UTF-8"));

            StringBuffer stringBuffer = new StringBuffer();
            String line;
            while ((line = reader.readLine()) != null) {
                stringBuffer.append(line);
            }

            reader.close();
            return stringBuffer.toString();
        } else {
            System.err.println("request failed: ");
            System.err.println("Got code: " + returnCode);
            return null;
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;

}

From source file:it.geosolutions.geonetwork.op.GNLogin.java

public static boolean login(HTTPUtils connection, String serviceURL, String username, String password) {
    Element request = new Element("request");
    request.addContent(new Element("username").setText(username));
    request.addContent(new Element("password").setText(password));

    XMLOutputter outputter = new XMLOutputter(Format.getCompactFormat());
    String xml = outputter.outputString(request);

    String loginURL = serviceURL + "/srv/en/xml.user.login";
    String out = connection.postXml(loginURL, xml);

    return connection.getLastHttpStatus() == HttpStatus.SC_OK;
}

From source file:name.chengchao.myhttpclient.version3_1.HttpClient3UtilUseManager.java

/**
 * ?url?ResponseBody,method=get//  www.j a v  a2 s  . c  o m
 * 
 * @param url exp:http://192.168.1.1:8080/dir/target.html
 * @return byte[]?
 */
public static byte[] getDataFromUrl(String url, int timeout) {
    if (StringUtils.isBlank(url)) {
        logger.error("url is blank!");
        return null;
    }
    HttpClient httpClient = new HttpClient(connectionManager);
    // 
    httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(8000);
    // ?
    httpClient.getParams().setSoTimeout(timeout);
    GetMethod method = new GetMethod(url);

    // fix???
    // method.setRequestHeader("Connection", "close");
    // ??1
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(1, false));
    try {
        int statusCode = httpClient.executeMethod(method);
        if (statusCode == HttpStatus.SC_OK) {
            return method.getResponseBody();
        } else {
            throw new RuntimeException("http request error,return code:" + statusCode + ",msg:"
                    + new String(method.getResponseBody()));
        }
    } catch (HttpException e) {
        method.abort();
        logger.error(e.getMessage());
    } catch (IOException e) {
        method.abort();
        logger.error(e.getMessage());
    } finally {
        // Release the connection.
        method.releaseConnection();
    }
    return null;
}

From source file:fr.bettinger.log4j.HttpThread.java

public void run() {
    int statusCode = 0;
    try {//from w  w w . j av a 2s  . co m
        statusCode = httpClient.executeMethod(httpMethod);
        if (statusCode != HttpStatus.SC_OK) {
            errorHandler.error("Error Server URL " + httpMethod.getHostConfiguration().getHostURL()
                    + " return status code " + statusCode);
        }
    } catch (HttpException e) {
        errorHandler.error(
                "HttpException error in sending request to server: URL: "
                        + httpMethod.getHostConfiguration().getHostURL() + " returned: " + statusCode,
                e, statusCode);
    } catch (IOException e) {
        errorHandler.error("Io error in sending request to server: URL: "
                + httpMethod.getHostConfiguration().getHostURL() + " returned: " + statusCode, e, statusCode);
    } finally {
        httpMethod.releaseConnection();
    }
}

From source file:com.android.volley.NetworkResponse.java

public NetworkResponse(byte[] data, Map<String, String> headers) {
    this(HttpStatus.SC_OK, data, headers, false, 0);
}

From source file:edu.northwestern.bioinformatics.studycalendar.security.plugin.cas.direct.DirectLoginHttpFacade.java

/**
 * Contacts the CAS server and GETs its login form, returning the content as a string.
 *//*from   w  ww .  jav a  2s  .c om*/
public String getForm() throws IOException {
    log.trace("GETting {} to obtain login ticket", loginUrl);
    GetMethod get = initMethod(new GetMethod(loginUrl));
    get.setQueryString(new NameValuePair[] { new NameValuePair("service", getServiceUrl()) });
    try {
        httpClient.executeMethod(get);
        if (get.getStatusCode() == HttpStatus.SC_OK) {
            return get.getResponseBodyAsString();
        } else {
            throw new CasDirectException("Retrieving the login form %s failed: %s", loginUrl,
                    get.getStatusLine());
        }
    } finally {
        get.releaseConnection();
    }
}

From source file:com.tw.go.plugin.material.artifactrepository.yum.exec.HttpConnectionCheckerTest.java

@Test
public void shouldNotThrowExceptionIfCheckConnectionToTheRepoPasses() throws IOException {
    HttpClient httpClient = mock(HttpClient.class);
    when(checker.getHttpClient()).thenReturn(httpClient);
    GetMethod getMethod = mock(GetMethod.class);
    when(checker.getGetMethod(Matchers.<String>any())).thenReturn(getMethod);
    when(httpClient.executeMethod(getMethod)).thenReturn(HttpStatus.SC_OK);

    checker.checkConnection("url", new Credentials(null, null));

    verify(httpClient).executeMethod(getMethod);
    verify(getMethod, never()).getStatusLine();
}

From source file:com.sappenin.eaut.consumer.mappingservice.MappingServiceClientImpl.java

/**
 * This function communicates with an EAUT Mapping service via an HTTP get,
 * provides the service an email address, and return the resulting URL, if
 * found./*from  w ww.  j a  va2 s .co m*/
 * 
 * @param pae
 * @return The URL that the specified Mapping Service indicates is the URL
 *         of the specified email address.
 * @throws EAUTException
 */
@Override
public String queryMappingService(ParsedEmailAddress pae, String getUrl) throws EAUTException {
    HttpClient client = new HttpClient();
    GetMethod get = new GetMethod(getUrl);
    get.setFollowRedirects(true);

    if (log.isDebugEnabled())
        log.debug("Performing HTTP GET on: " + getUrl + " ...");

    int statusCode;
    try {
        statusCode = client.executeMethod(get);
        if (statusCode != HttpStatus.SC_OK) {
            if (statusCode == HttpStatus.SC_BAD_REQUEST)
                throw new EAUTException("The Email Identifier supplied by '" + getUrl
                        + "' is not properly formatted per the EAUT spec and/or RFC2822 (Http Error 400)");
            else if (statusCode == HttpStatus.SC_INTERNAL_SERVER_ERROR)
                throw new EAUTException(
                        "The EAUT Mapping Service was unable to complete a mapping request for URL: '" + getUrl
                                + "'.  Please try this request again at a later time.  (Http Error 500)");
            else
                throw new EAUTException("GET failed on " + getUrl + "(Http Error " + statusCode + ")");
        } else {
            String redirectLocation = get.getURI().toString();
            if (redirectLocation != null) {
                return redirectLocation;
            } else {
                // The response is invalid and did not provide the new
                // location
                // for the resource. Report an error or possibly handle the
                // response like a 404 Not Found error.
                throw new EAUTException("GET failed on " + getUrl + "(Http Error " + statusCode + ")");
            }

        }
    } catch (EAUTException ee) {
        throw ee;
    } catch (HttpException he) {
        throw new EAUTException(he);
    } catch (IOException ioe) {
        throw new EAUTException(ioe);
    }
}

From source file:com.thoughtworks.go.config.update.ConfigUpdateAjaxResponseTest.java

@Test
public void shouldGetJsonRepresentationForSuccess() throws Exception {
    ConfigUpdateAjaxResponse response = ConfigUpdateAjaxResponse.success("id", HttpStatus.SC_OK,
            "saved successful");
    String jsonString = response.toJson();
    assertThat(jsonString, is(//from   w ww .ja  v a  2 s  .c om
            "{\"fieldErrors\":{},\"globalErrors\":[],\"message\":\"saved successful\",\"isSuccessful\":true,\"subjectIdentifier\":\"id\"}"));
}