xworker.httpclient.actions.HttpTest Maven / Gradle / Ivy
package xworker.httpclient.actions;
import java.io.IOException;
import java.io.InputStream;
import ognl.OgnlException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.util.EntityUtils;
import org.xmeta.ActionContext;
import org.xmeta.Thing;
import org.xmeta.World;
import org.xmeta.util.UtilData;
import xworker.httpclient.HttpClientManager;
import xworker.util.StringUtils;
import freemarker.template.TemplateException;
public class HttpTest {
/**
* 执行Http测试。
*
* @param actionContext
* @return
* @throws OgnlException
* @throws TemplateException
* @throws IOException
*/
public static HttpTestInfo run(ActionContext actionContext) throws OgnlException, IOException, TemplateException{
World world = World.getInstance();
Thing self = (Thing) actionContext.get("self");
//URI
String uri = StringUtils.getString(self, "url", actionContext);
//log.info(uri);
//HttpGet
HttpGet httpGet = new HttpGet(uri);
/*
//headers
if(self.headers != null && self.headers != ""){
def headers = Ognl.getValue(self.headers, actionContext);
if(headers instanceof Header[]){
httpGet.setHeaders(headers);
}
}
//params
if(self.params != null && self.params != ""){
def params = Ognl.getValue(self.params, actionContext);
if(params instanceof HttpParams){
httpGet.setParams(params);
}
}*/
//httpClient
HttpClient httpclient = null;
if(self.getStringBlankAsNull("httpclient") != null){
Object obj = UtilData.getData(self, "httpclient", actionContext);
if(obj instanceof HttpClient){
httpclient = (HttpClient) obj;
}else if(obj instanceof String){
Thing httpclientThing = world.getThing((String) obj);
if(httpclientThing != null){
httpclient = (HttpClient) httpclientThing.doAction("getHttpClient", actionContext);
}
}
}
if(httpclient == null){
httpclient = HttpClientManager.getDefaultHttpClient();
//throw new XMetaException("no httpclient");
}
HttpResponse response = null;
HttpEntity entity = null;
long start = System.currentTimeMillis();
HttpTestInfo info = new HttpTestInfo();
info.url = uri;
try{
response = httpclient.execute(httpGet);
entity = response.getEntity();
info.statusCode = response.getStatusLine().getStatusCode();
info.contentLength = entity.getContentLength();
info.contentType = entity.getContentType().getValue();
InputStream in = entity.getContent();
byte[] bytes = new byte[2048];
while(in.read(bytes) != -1){
}
info.success = true;
long totalTime = System.currentTimeMillis() - start;
info.totalTime = totalTime;
if(info.contentLength > 0){
info.speed = (int) (info.contentLength * 1000 / (totalTime));
info.speedLabel = UtilData.getSizeInfo(info.speed);
}
}catch(Exception e){
info.success = false;
long totalTime = System.currentTimeMillis() - start;
info.totalTime = totalTime;
info.exception = e;
}finally{
if(entity != null){
try {
EntityUtils.consume(entity);
} catch (IOException e) {
e.printStackTrace();
}
}
}
return info;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy