Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.IOException;
- import java.io.UnsupportedEncodingException;
- import org.apache.http.HttpEntity;
- import org.apache.http.HttpResponse;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.entity.StringEntity;
- import org.apache.http.impl.client.DefaultHttpClient;
- import org.apache.http.util.EntityUtils;
- import com.google.gson.JsonElement;
- import com.google.gson.JsonObject;
- import com.google.gson.JsonParser;
- public class TestHttpClient {
- public static String parse(String jsonLine) {
- JsonElement jelement = new JsonParser().parse(jsonLine);
- JsonObject jobject = jelement.getAsJsonObject();
- String result = jobject.get("result").toString();
- return result;
- }
- public static void main(String[] args) throws UnsupportedEncodingException {
- DefaultHttpClient client = new DefaultHttpClient();
- HttpPost post = new HttpPost("http://PATH_OF_YOUR_SERVER/index.php/admin/remotecontrol");
- post.setHeader("Content-type", "application/json");
- post.setEntity( new StringEntity("{\"method\": \"get_session_key\", \"params\": {\"username\": \"YOUR_USERNAME\", \"password\": \"YOUR_PASSWORD\" }, \"id\": 1}"));
- try {
- HttpResponse response = client.execute(post);
- if(response.getStatusLine().getStatusCode() == 200){
- HttpEntity entity = response.getEntity();
- String sessionKey = parse(EntityUtils.toString(entity));
- post.setEntity( new StringEntity("{\"method\": \"list_groups\", \"params\": {\"sSessionKey \": "+sessionKey+", \"iSurveyID \": \"ID_SURVEY\" }, \"id\": 1}"));
- response = client.execute(post);
- if(response.getStatusLine().getStatusCode() == 200){
- entity = response.getEntity();
- System.out.println(EntityUtils.toString(entity));
- }
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement