use of info.xiancloud.core.util.http.Request in project xian by happyyangyuan.
the class HttpUnit method execute.
@Override
public UnitResponse execute(UnitRequest msg) {
ObjectInputStream ois = null;
try {
String reqBase64 = msg.get("req", String.class);
ois = new ObjectInputStream(new ByteArrayInputStream(Base64.getDecoder().decode(reqBase64)));
Request request = (Request) ois.readObject();
Response response;
try {
response = request.executeLocal();
} catch (ConnectException e) {
return UnitResponse.error(ISocketGroup.CODE_CONNECT_TIMEOUT, null, "Connect timeout: " + request.getUrl());
} catch (SocketTimeoutException e) {
return UnitResponse.error(ISocketGroup.CODE_SOCKET_TIMEOUT, null, "Read timeout: " + request.getUrl());
} catch (Throwable e) {
return UnitResponse.exception(e);
}
JSONObject retJson = new JSONObject();
retJson.put("status", response.getStatus());
retJson.put("headers", response.getHeaders());
retJson.put("entity", response.string());
return UnitResponse.success(retJson);
} catch (Throwable e) {
return UnitResponse.exception(e);
} finally {
}
}
use of info.xiancloud.core.util.http.Request in project xian by happyyangyuan.
the class QCloudBaseUnit method execute.
@Override
public UnitResponse execute(UnitRequest msg) {
// 填充请求参数
TreeMap<String, String> params = fillRequestArgs(msg);
// 生成签名
String sign = createSignature(params);
params.put("Signature", sign);
LOG.info(String.format("腾讯云API调用,请求参数:%s", JSON.toJSONString(params)));
try {
Request request = HttpKit.get("https://" + getAPIHost() + HHTP_APIURL);
for (Entry<String, String> param : params.entrySet()) {
request.addParam(param.getKey(), param.getValue());
}
request.setSSL(null, null);
String result = request.executeLocal().string();
return UnitResponse.success(convert(result));
} catch (ConnectException e) {
LOG.error("调用腾讯云API连接超时", e);
throw new RuntimeException("调用腾讯云API连接超时");
} catch (SocketTimeoutException e) {
LOG.error("调用腾讯云API响应超时", e);
throw new RuntimeException("调用腾讯云API响应超时");
} catch (Exception e) {
LOG.error("调用腾讯云API出错", e);
throw new RuntimeException("调用腾讯云API出错");
}
}
Aggregations