service.StatisticsServerTest.java Source code

Java tutorial

Introduction

Here is the source code for service.StatisticsServerTest.java

Source

/*
* $Id
*
* (C) Copyright 1997 i-Teco, CJSK. All Rights reserved.
* i-Teco PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*
* ?  1997 i-Teco, ?.
*  ?    ???   
*   ?  i-Teco.          
*/
package service;

import junit.framework.TestCase;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.log4j.Logger;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

/**
 * $Id
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Author: g.alexeev (g.alexeev@i-teco.ru)</p>
 * <p>Date: 04.02.13</p>
 *
 * @version 1.0
 */
public class StatisticsServerTest extends TestCase {
    private final Logger logger = Logger.getLogger(StatisticsServerTest.class);

    public void testApp() throws UnsupportedEncodingException {
        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost("http://194.28.132.219:8080/statsserver_archive/rest/");
        StringEntity entity = new StringEntity("{\"age\":\"38\",\"sex\":\"Male\",\"country\":\"Russia\"}");
        entity.setContentType("application/json");
        post.setEntity(entity);
        try {
            HttpResponse response = client.execute(post);
            BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
            String line = "";
            while ((line = rd.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            logger.error(e.getMessage());
        }
    }
}