java rest client
I write a java client which connect to the scanner scan and get the status of scanner using rest api. But when the client run
several hours, the server become very very slow. Please help me check the code and tell me what's the problem.
StringBuilder data = new StringBuilder();
data.append("{ \"url\": \"");
data.append(url);
data.append("\",\"checks\": [\"*\"]");
data.append("}");
DefaultHttpClient client = null;
try {
client = new DefaultHttpClient();
client.getParams().setParameter("Content-Type", "UTF-8");
HttpPost addPost = new HttpPost(serverurl);
addPost.setEntity( new StringEntity(data.toString()));
HttpResponse response = client.execute(addPost);
if (response.getStatusLine().getStatusCode() == 200) {
StringBuilder sb = new StringBuilder();
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));
String s ;
while ((s = br.readLine()) != null) {
sb.append(s);
}
}
catch (IOException e) {
if (br != null) { br.close(); }
}
if (sb.length() > 0) {
String taskid = null;
Matcher match = pattern.matcher(sb.toString());
while (match.find()) {
String s = match.group().replaceAll("\"", "");
if ("id".equals(s)) continue ;
taskid = s;
}
if (taskid == null) {
throw new Exception("taskid is null");
}
String status = null;
while (true) {
HttpGet get = new HttpGet(serverurl+"/"+taskid);
HttpResponse queryResponse = client.execute(get);
if (queryResponse.getStatusLine().getStatusCode() == 200) {
sb.setLength(0);
br = new BufferedReader(new InputStreamReader((queryResponse.getEntity().getContent()), "UTF-8"));
String json;
while ((json = br.readLine()) != null) {
sb.append(json);
}
HashMap<String, Object> obj;
try {
obj = (HashMap<String, Object>) JSONUtil.deserialize(sb.toString());
}
catch (Exception e) {
System.out.println(sb.toString());
throw e ;
}
status = (String) obj.get("status");
System.out.println(sdf.format(new Date())+", taskid=" + taskid + ", status="+ status);
if ("done".equals(status)) {
break ;
}
Thread.sleep(cycle * 1000);
}
}
if ("done".equals(status)) {
HttpResponse reportResponse = client.execute(new HttpGet(serverurl+"/"+taskid+"/report"));
if (reportResponse.getStatusLine().getStatusCode() == 200) {
sb.setLength(0);
br = new BufferedReader(new InputStreamReader((reportResponse.getEntity().getContent())));
String json;
while ((json = br.readLine()) != null) {
sb.append(json);
}
// System.out.println(sb.toString());
}
}
Keyboard shortcuts
Generic
? | Show this help |
---|---|
ESC | Blurs the current field |
Comment Form
r | Focus the comment reply box |
---|---|
^ + ↩ | Submit the comment |
You can use Command ⌘
instead of Control ^
on Mac
Support Staff 1 Posted by Tasos Laskos on 27 Jun, 2017 02:08 PM
When you say "server" you mean the REST server or the web application's server
2 Posted by liuyong on 28 Jun, 2017 01:01 AM
The server is REST server.
Support Staff 3 Posted by Tasos Laskos on 28 Jun, 2017 02:48 PM
That depends on the available system resources, if you're running many scans or if the machine is underpowered then the entire system can become sluggish.