use of lib.data.HttpTransaction in project LibTest by liudao01.
the class ChuckInterceptor method intercept.
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
RequestBody requestBody = request.body();
boolean hasRequestBody = requestBody != null;
HttpTransaction transaction = new HttpTransaction();
HttpTransaction transactionView = new HttpTransaction();
// 时间
transaction.setRequestDate(new Date());
// 请求方法
transaction.setMethod(request.method());
// 请求url
transaction.setUrl(request.url().toString());
// 请求头
transaction.setRequestHeaders(request.headers());
if (hasRequestBody) {
if (requestBody.contentType() != null) {
transaction.setRequestContentType(requestBody.contentType().toString());
}
if (requestBody.contentLength() != -1) {
transaction.setRequestContentLength(requestBody.contentLength());
}
}
// 请求体是否明文
transaction.setRequestBodyIsPlainText(!bodyHasUnsupportedEncoding(request.headers()));
if (hasRequestBody && transaction.responseBodyIsPlainText()) {
BufferedSource source = getNativeSource(new Buffer(), bodyGzipped(request.headers()));
Buffer buffer = source.buffer();
requestBody.writeTo(buffer);
Charset charset = UTF8;
MediaType contentType = requestBody.contentType();
if (contentType != null) {
charset = contentType.charset(UTF8);
}
if (isPlaintext(buffer)) {
if (!contentType.toString().contains("multipart/form-data")) {
String s = readFromBuffer(buffer, charset);
String requestStr;
try {
requestStr = decode(s);
} catch (Exception e) {
requestStr = s;
}
String string = converStr(requestStr);
try {
// requestStrLast = string;
requestStrLast = JsonFormatUtil.formatJson(string);
} catch (Exception e) {
requestStrLast = string;
}
} else {
requestStrLast = "";
return null;
}
transaction.setRequestBody(readFromBuffer(buffer, charset));
} else {
transaction.setResponseBodyIsPlainText(false);
}
}
// Uri transactionUri = create(transaction);
long startNs = System.nanoTime();
Response response;
try {
response = chain.proceed(request);
} catch (Exception e) {
transaction.setError(e.toString());
try {
transactionView = transaction;
transactionView.setRequestBody(requestStrLast);
TestLibUtil.getInstance().sendmessage(transactionView);
} catch (Exception e1) {
e1.printStackTrace();
TestLibUtil.getInstance().sendmessage(transactionView);
}
// TestLibUtil.getInstance().sendmessage(transactionView);
throw e;
}
long tookMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNs);
ResponseBody responseBody = response.body();
// includes headers added later in the chain
transaction.setRequestHeaders(response.request().headers());
transaction.setResponseDate(new Date());
transaction.setTookMs(tookMs);
transaction.setProtocol(response.protocol().toString());
transaction.setResponseCode(response.code());
transaction.setResponseMessage(response.message());
transaction.setResponseContentLength(responseBody.contentLength());
if (responseBody.contentType() != null) {
transaction.setResponseContentType(responseBody.contentType().toString());
}
transaction.setResponseHeaders(response.headers());
transaction.setResponseBodyIsPlainText(!bodyHasUnsupportedEncoding(response.headers()));
if (HttpHeaders.hasBody(response) && transaction.responseBodyIsPlainText()) {
BufferedSource source = getNativeSource(response);
source.request(Long.MAX_VALUE);
Buffer buffer = source.buffer();
Charset charset = UTF8;
MediaType contentType = responseBody.contentType();
if (contentType != null) {
try {
charset = contentType.charset(UTF8);
} catch (UnsupportedCharsetException e) {
// update(transaction, transactionUri);
try {
transactionView = transaction;
transactionView.setRequestBody(requestStrLast);
TestLibUtil.getInstance().sendmessage(transactionView);
} catch (Exception e1) {
e1.printStackTrace();
}
return response;
}
}
if (isPlaintext(buffer)) {
try {
responseStrLast = JsonFormatUtil.formatJson(readFromBuffer(buffer.clone(), charset));
} catch (Exception e) {
responseStrLast = readFromBuffer(buffer.clone(), charset);
}
transaction.setResponseBody(readFromBuffer(buffer.clone(), charset));
} else {
transaction.setResponseBodyIsPlainText(false);
}
transaction.setResponseContentLength(buffer.size());
}
try {
transactionView = transaction;
transactionView.setRequestBody(requestStrLast);
transactionView.setResponseBody(responseStrLast);
TestLibUtil.getInstance().sendmessage(transactionView);
} catch (Exception e) {
e.printStackTrace();
}
return response;
}
use of lib.data.HttpTransaction in project LibTest by liudao01.
the class NetHoverMenuScreen method onItemLongClick.
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
HttpTransaction httpTransaction = (HttpTransaction) TestLibUtil.httpMoudleList.get(position);
Utils.copy(mContext, "请求方式: " + httpTransaction.getMethod() + "\n" + "请求地址: " + httpTransaction.getUrl() + "\n" + "请求参数: " + httpTransaction.getRequestBody());
return true;
}
use of lib.data.HttpTransaction in project LibTest by liudao01.
the class HttpNavigatorContent method onEventMainThread.
public void onEventMainThread(@NonNull HoverTheme newTheme) {
Log.d(TAG, "onEventMainThread: ");
if (TestLibUtil.httpMoudleList != null) {
if (listHttpAdapter != null) {
List<HttpTransaction> httpMoudleList = new ArrayList<>();
httpMoudleList.addAll(TestLibUtil.httpMoudleList);
if (httpMoudleList != null && httpMoudleList.size() > 0) {
// 倒序刚发的在最前面
Collections.reverse(httpMoudleList);
}
Message message = Message.obtain();
message.obj = httpMoudleList;
handler.sendMessage(message);
}
}
}
use of lib.data.HttpTransaction in project LibTest by liudao01.
the class MainActivity method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = this;
// TestLibUtil.getInstance().startUtil(getApplication());
setContentView(R.layout.activity_main);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!Settings.canDrawOverlays(context)) {
Intent drawOverlaysSettingsIntent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
drawOverlaysSettingsIntent.setData(Uri.parse("package:" + context.getPackageName()));
context.startActivity(drawOverlaysSettingsIntent);
}
}
btStart = (Button) findViewById(R.id.bt_start);
tvIpList = (TextView) findViewById(R.id.tv_ip_list);
btCrash = (Button) findViewById(R.id.bt_crash);
btStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
doHttpActivity();
}
});
btCrash.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int x = 1 / 0;
}
});
// 网络测试
final String url = "www.baidu.com";
final String json = "{\n" + " \"result\": \"success\",\n" + " \"arr\": [\n" + " {\n" + " \"examid\": 11,\n" + " \"class\": \"情感测试\",\n" + " \"subclass\": \"家庭关系\",\n" + " \"title\": \"家庭情感表达测试\",\n" + " \"updatetime\": \"2013-12-04 17:14:17\"\n" + " },\n" + " {\n" + " \"examid\": 12,\n" + " \"class\": \"情感测试\",\n" + " \"subclass\": \"亲子关系\",\n" + " \"title\": \"亲子关系测试\",\n" + " \"updatetime\": \"2013-12-04 17:14:17\"\n" + " }\n" + " ]\n" + "}";
btStartOne = findViewById(R.id.bt_start_one);
HttpTransaction httpTransaction = new HttpTransaction();
httpTransaction.setUrl(url);
httpTransaction.setResponseBody(json);
TestLibUtil.getInstance().sendmessage(httpTransaction);
btStartOne.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
HttpTransaction httpTransaction = new HttpTransaction();
httpTransaction.setUrl("www.google.com__" + (count++));
httpTransaction.setResponseBody(json);
TestLibUtil.getInstance().sendmessage(httpTransaction);
}
});
// tvIpList.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View v) {
// TestLibUtil.getInstance().sendmessage("", url, json);
// }
// });
// ip 测试
IPinit();
List<IpConfigBeen> ipList = TestLibUtil.getInstance().getIpList();
String list = "";
for (IpConfigBeen ipConfigBeen : ipList) {
list = list + ipConfigBeen.toString() + "\n";
}
tvIpList.setText(list);
}
use of lib.data.HttpTransaction in project LibTest by liudao01.
the class TestLibUtil method sendmessage.
/**
* 发送网路哦请求
*
* @param header
* @param url
* @param json
*/
public void sendmessage(String header, String url, String json) {
Intent intent1 = new Intent();
// 不能发送大量数据
// BroadcastUtil.send(context, intent1, BroadcastUtil.windows);
// 直接操作静态变量
Logger.d("打印数据 url= \n" + url);
HttpTransaction been = new HttpTransaction();
been.setUrl(url);
been.setRequestBody("header = " + header);
been.setResponseBody(json);
// 最大条数 0条避免数量过多溢出
if (httpMoudleList != null) {
if (httpMoudleList.size() > 30) {
httpMoudleList.remove(30);
httpMoudleList.add(been);
} else {
httpMoudleList.add(been);
}
HttpNavigatorContent.setList();
}
}
Aggregations