Advertisement
Guest User

Untitled

a guest
Jun 28th, 2013
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.97 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.io.UnsupportedEncodingException;
  3.  
  4. import org.apache.http.HttpEntity;
  5. import org.apache.http.HttpResponse;
  6. import org.apache.http.client.methods.HttpPost;
  7. import org.apache.http.entity.StringEntity;
  8. import org.apache.http.impl.client.DefaultHttpClient;
  9. import org.apache.http.util.EntityUtils;
  10.  
  11. import com.google.gson.JsonElement;
  12. import com.google.gson.JsonObject;
  13. import com.google.gson.JsonParser;
  14.  
  15. public class TestHttpClient {
  16.    
  17.     public static String parse(String jsonLine) {
  18.         JsonElement jelement = new JsonParser().parse(jsonLine);
  19.         JsonObject  jobject = jelement.getAsJsonObject();
  20.         String result = jobject.get("result").toString();
  21.        
  22.         return result;
  23.     }
  24.    
  25.    
  26.     public static void main(String[] args) throws UnsupportedEncodingException {
  27.       DefaultHttpClient client = new DefaultHttpClient();          
  28.          
  29.       HttpPost post = new HttpPost("http://PATH_OF_YOUR_SERVER/index.php/admin/remotecontrol");
  30.       post.setHeader("Content-type", "application/json");
  31.       post.setEntity( new StringEntity("{\"method\": \"get_session_key\", \"params\": {\"username\": \"YOUR_USERNAME\", \"password\": \"YOUR_PASSWORD\" }, \"id\": 1}"));
  32.       try {
  33.         HttpResponse response = client.execute(post);
  34.         if(response.getStatusLine().getStatusCode() == 200){
  35.             HttpEntity entity = response.getEntity();
  36.             String sessionKey = parse(EntityUtils.toString(entity));
  37.             post.setEntity( new StringEntity("{\"method\": \"list_groups\", \"params\": {\"sSessionKey \": "+sessionKey+", \"iSurveyID \": \"ID_SURVEY\" }, \"id\": 1}"));
  38.             response = client.execute(post);
  39.             if(response.getStatusLine().getStatusCode() == 200){
  40.                 entity = response.getEntity();
  41.                 System.out.println(EntityUtils.toString(entity));
  42.                 }
  43.            }
  44.        
  45.        
  46.       } catch (IOException e) {
  47.         e.printStackTrace();
  48.       }
  49.     }}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement