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:function.SettingCountryGeo.java

private void setCountry(String location, Photo photo) throws IOException, JSONException {

    String url = "http://api.geonames.org/searchJSON?q=" + location + "&maxRows=1&username=jelena_tabas";

    setRequest(url);/*w  w  w  .  j  av  a  2  s  .com*/
    setMethod(new GetMethod(getRequest()));

    setStatusCode(getClient().executeMethod(getMethod()));

    if (getStatusCode() != HttpStatus.SC_OK) {
        System.err.println("Method failed: " + getMethod().getStatusLine());
    }
    setRstream(null);
    setRstream(getMethod().getResponseBodyAsStream());

    String jstr = toString(getRstream());
    JSONObject jobj = new JSONObject(jstr);
    JSONArray geonames = jobj.getJSONArray("geonames");

    for (int i = 0; i < geonames.length(); i++) {
        JSONObject jphoto = geonames.getJSONObject(i);
        photo.setLocation(jphoto.getString("countryName"));
        photo.setLon(jphoto.getDouble("lng"));
        photo.setLat(jphoto.getDouble("lat"));
    }

}

From source file:ClientPost.java

public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, java.io.IOException {

    HttpClient httpClient = new HttpClient();
    PostMethod postMethod = new PostMethod("http://localhost:8080/home/viewPost.jsp");
    NameValuePair[] postData = { new NameValuePair("username", "devgal"),
            new NameValuePair("department", "development"), new NameValuePair("email", "devgal@yahoo.com") };
    //the 2.0 beta1 version has a
    // PostMethod.setRequestBody(NameValuePair[])
    //method, as addParameters is deprecated
    postMethod.addParameters(postData);// www.  j a v  a 2 s.co  m
    httpClient.executeMethod(postMethod);
    //display the response to the POST method
    response.setContentType("text/html");
    java.io.PrintWriter out = response.getWriter();
    //A "200 OK" HTTP Status Code
    if (postMethod.getStatusCode() == HttpStatus.SC_OK) {
        out.println(postMethod.getResponseBodyAsString());
    } else {
        out.println("The POST action raised an error: " + postMethod.getStatusLine());
    }
    //release the connection used by the method
    postMethod.releaseConnection();

}

From source file:idea.clientRequests.httpClientRequester.java

public boolean doHttpget(String csUrl) {
    boolean bStatus = false;
    m_responseBody = null;/* w  ww . j a  v  a  2s  .c  o m*/
    HttpClient client = new HttpClient();
    GetMethod method = new GetMethod(csUrl);

    // Provide custom retry handler is necessary
    DefaultHttpMethodRetryHandler handler = new DefaultHttpMethodRetryHandler(3, false);
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, handler);
    try {
        // Execute the method.
        int statusCode = client.executeMethod(method);
        if (statusCode != HttpStatus.SC_OK) {
            System.err.println("Method failed: " + method.getStatusLine());
        }
        // Read the response body.
        m_responseBody = method.getResponseBody();

        bStatus = true;
    } catch (HttpException e) {
        System.err.println("Fatal protocol violation: " + e.getMessage());
        e.printStackTrace();
    } catch (IOException e) {
        System.err.println("Fatal transport error: " + e.getMessage());
        e.printStackTrace();
    } finally {
        // Release the connection.
        method.releaseConnection();
    }
    return bStatus;
}

From source file:function.SearchPhotos.java

public List<Photo> searchPhoto(String text, int page)
        throws IOException, ParserConfigurationException, SAXException, JSONException {

    setRequest(getData().getRequestMethod() + getData().getMethodSearchPhotos() + "&text=" + text
            + "&sort=relevance" + "&page=" + page + "&api_key=" + getData().getKey() + "&format=json");
    System.out.println("GET search request: " + getRequest());

    setMethod(new GetMethod(getRequest()));
    setStatusCode(getClient().executeMethod(getMethod()));

    if (getStatusCode() != HttpStatus.SC_OK) {
        System.err.println("Method failed: " + getMethod().getStatusLine());
    }//from  w w  w  .  ja  va2s .  co m
    setRstream(null);
    setRstream(getMethod().getResponseBodyAsStream());

    String jstr = toString(getRstream());
    jstr = jstr.substring("jsonFlickrApi(".length(), jstr.length() - 1);

    JSONObject jobj = new JSONObject(jstr);
    JSONArray photos = jobj.getJSONObject("photos").getJSONArray("photo");
    for (int i = 0; i < photos.length(); i++) {
        JSONObject jphoto = photos.getJSONObject(i);
        setPhoto(new Photo());
        getPhoto().setId(jphoto.getString("id"));
        getPhoto().setTitle(jphoto.getString("title"));
        getPhoto().setUserId(jphoto.getString("owner"));
        getPhoto().setSecret(jphoto.getString("secret"));
        getPhoto().setServer(jphoto.getString("server"));

        getListPhotos().add(getPhoto());
    }
    return getListPhotos();
}

From source file:com.thoughtworks.twist.mingle.core.MingleClientTest.java

public void testValidatesWhenHttpStatusIs200() throws Exception {
    MingleClient mingleClient = new TestMingleClient("http://localhost:30001/projects/foobar", "ketan", "ketan",
            HttpStatus.SC_OK);
    assertTrue(mingleClient.validate());
}

From source file:datasoul.util.OnlineUpdateCheck.java

@Override
public void run() {

    HttpClient client = new HttpClient();
    HttpMethod method = new GetMethod(ONLINE_BASE_URL + "latest-version");
    try {/*from   w  w  w .ja  va  2 s  . c  om*/
        // Execute the method.
        int statusCode = client.executeMethod(method);

        if (statusCode != HttpStatus.SC_OK) {
            return;
        }

        // Read the response body.
        byte[] responseBody = method.getResponseBody();

        // Deal with the response.
        // Use caution: ensure correct character encoding and is not binary data
        String resp = new String(responseBody);

        // Expected format is: <NUMBER>;<NEW VERSION>;<URL>
        String toks[] = resp.split(";");

        if (toks.length != 3) {
            return;
        }

        int latestversion = Integer.parseInt(toks[0]);

        if (latestversion > VERSION) {
            String msg = java.util.ResourceBundle.getBundle("datasoul/internationalize")
                    .getString("NEW DATASOUL VERSION") + " " + toks[1] + " "
                    + java.util.ResourceBundle.getBundle("datasoul/internationalize")
                            .getString("IS AVAILABLE AT")
                    + " " + toks[2];

            ObjectManager.getInstance().getDatasoulMainForm().setInfoText(msg);
        }

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        // Release the connection.
        method.releaseConnection();
    }

}

From source file:cn.comgroup.tzmedia.server.util.mail.SendCloudMail.java

/**
* Send email using SMTP server.//from  w  w  w  .j  av  a 2 s  .  co  m
*
* @param recipientEmail TO recipient
* @param title title of the message
* @param message message to be sent
* connected state or if the message is not a MimeMessage
*/
public static void send(String recipientEmail, String title, String message)
        throws UnsupportedEncodingException, IOException {
    String url = "http://sendcloud.sohu.com/webapi/mail.send.json";
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httpost = new HttpPost(url);

    List nvps = new ArrayList();
    // ??SendCloud?????????????????
    nvps.add(new BasicNameValuePair("api_user", "commobile_test_IxiZE1"));
    nvps.add(new BasicNameValuePair("api_key", "0tkHZ5vDdScYzRbn"));
    nvps.add(new BasicNameValuePair("from", "suport@tzzjmedia.net"));
    nvps.add(new BasicNameValuePair("to", recipientEmail));
    nvps.add(new BasicNameValuePair("subject", title));
    nvps.add(new BasicNameValuePair("html", message));

    httpost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
    // 
    HttpResponse response = httpclient.execute(httpost);
    // ??
    if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { // 
        // ?xml
        String result = EntityUtils.toString(response.getEntity());
        Logger.getLogger(SendCloudMail.class.getName()).log(Level.INFO, result);
    } else {
        System.err.println("error");
    }
}

From source file:net.sourceforge.jcctray.model.DashboardXmlParser.java

public static DashBoardProjects getProjects(String url, HttpClient client)
        throws HttpException, IOException, SAXException {
    GetMethod method = new GetMethod(url);
    method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(3, false));
    try {/*from   w  ww  .j  a  v a2s .co  m*/
        int statusCode = client.executeMethod(method);
        if (statusCode != HttpStatus.SC_OK) {
            throw new RuntimeException(
                    ("Could not connect to " + url + ". The server returned a " + statusCode + " status code"));
        }
        return getProjects(method.getResponseBodyAsStream());
    } finally {
        method.releaseConnection();
    }
}

From source file:atg.taglib.json.Helper.java

/**
 * Get a response from the server for a test
 *
 * @param pTestUrl The test url, relative to the tests context root
 * @return the <code>ResponseData</code> object for this test
 * @throws IOException /* w  ww  .j  a v a2  s .c o m*/
 * @throws HttpException 
 * @throws JSONException 
 */
public static ResponseData getData(String pType, int pTestNum)
        throws HttpException, IOException, JSONException {
    // Setup HTTP client
    HttpClient client = new HttpClient();
    HttpMethod method = new GetMethod(getTestUrl(pType, pTestNum));

    // Execute the GET request, capturing response status
    int statusCode = client.executeMethod(method);
    String responseBody = method.getResponseBodyAsString();
    method.releaseConnection();

    // Create response data object
    ResponseData data = new ResponseData();
    data.body = responseBody.trim();
    data.statusCode = statusCode;
    if (statusCode == HttpStatus.SC_OK) {
        // Parse the JSON response
        data.json = parseJsonTextToObject(responseBody);
    }

    // Get the expected result
    method = new GetMethod(getStatus200ResultUrl(pType, pTestNum));
    data.expectedStatusCode = HttpStatus.SC_OK;

    // Execute the GET request, capturing response status
    statusCode = client.executeMethod(method);

    if (statusCode == HttpStatus.SC_NOT_FOUND) {
        // Test result wasn't found - we must be expecting an error for this test
        method.releaseConnection();
        method = new GetMethod(getStatus500ResultUrl(pType, pTestNum));
        statusCode = client.executeMethod(method);
        data.expectedStatusCode = HttpStatus.SC_INTERNAL_SERVER_ERROR;
    }

    responseBody = method.getResponseBodyAsString().trim();
    method.releaseConnection();

    // Parse the expected data   
    if (data.expectedStatusCode == HttpStatus.SC_OK) {
        // Parse body into JSON object
        data.expectedJson = parseJsonTextToObject(responseBody);
    } else {
        // Exception is expected, set the expected key
        data.expectedMsgKey = responseBody.trim();
    }

    return data;
}

From source file:com.foglyn.fogbugz.ResponseProcessor.java

boolean checkHttpStatus(int httpCode) {
    return httpCode == HttpStatus.SC_OK;
}