Skip to content

Instantly share code, notes, and snippets.

@krmahadevan
Created August 9, 2012 09:11
Show Gist options
  • Save krmahadevan/3302548 to your computer and use it in GitHub Desktop.
Save krmahadevan/3302548 to your computer and use it in GitHub Desktop.
This class shows you how to post against a grid servlet and get information.
package raw.selenium;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHttpRequest;
import org.json.JSONException;
import org.json.JSONObject;
import org.testng.Assert;
public class NodeInfoFromGrid {
static String hubHost = "localhost";
static int hubPort = 4444;
public static void main(String[] args) throws ClientProtocolException, IOException, JSONException {
URL proxyApi = new URL("http://" + hubHost + ":" + hubPort + "/grid/admin/MyConsoleServlet");
HttpClient client = new DefaultHttpClient();
BasicHttpRequest r = new BasicHttpRequest("GET", proxyApi.toExternalForm());
HttpHost host = new HttpHost(hubHost, hubPort);
HttpResponse response = client.execute(host, r);
Assert.assertEquals(200, response.getStatusLine().getStatusCode());
JSONObject o = extractObject(response);
System.out.println(o);
}
private static JSONObject extractObject(HttpResponse resp) throws IOException, JSONException {
BufferedReader rd = new BufferedReader(new InputStreamReader(resp.getEntity().getContent()));
StringBuilder s = new StringBuilder();
String line;
while ((line = rd.readLine()) != null) {
s.append(line);
}
rd.close();
return new JSONObject(s.toString());
}
}
@krmahadevan
Copy link
Author

This class can be used to post against a Servlet as shown in the gist : https://gist.github.com/3302497

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment