rest接口调用

/**
* 心得:一定要关闭流,inStream
* 那也就是用变量接收文件中的值
* @author Administrator
*
*/
public class HttpClientDemo {

private String trustStore = "证书名";
private String keyStorePwd = "pwd";

public void start(){
InputStream inStream = null;

try {
//加载配置文件
Properties prop = new Properties();
FileInputStream fis = new FileInputStream("路径/rest.properties");
inStream = new BufferedInputStream(fis);
prop.load(inStream);

//加载安全证书
KeyStore ks = KeyStore.getInstance("JKS");
ks.load(ClassLoader.getSystemResourceAsStream(trustStore),keyStorePwd.toCharArray());

//SSL:一种保证网络上的恋歌节点进行安全通信的协议
//连接采用tls还是ssl的安全方式
SSLContext sslContext = SSLContext.getInstance("TLS");

X509TrustManager tm = new X509TrustManager(){

@Override
public void checkClientTrusted(X509Certificate[] arg0,
String arg1) throws CertificateException {}

@Override
public void checkServerTrusted(X509Certificate[] arg0,
String arg1) throws CertificateException {}

@Override
public X509Certificate[] getAcceptedIssuers() {
return null;
}

};

KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
kmf.init(ks,"pwd".toCharArray());

sslContext.init(kmf.getKeyManagers(), new TrustManager[]{tm}, new SecureRandom());

SSLSocketFactory socketFactory = new SSLSocketFactory(sslContext,SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

String url = String.format("https://%s:%s%s", prop.getProperty("ip"),
prop.getProperty("port"),
prop.getProperty("url"));

JSONObject json = new JSONObject();
json.put("param",prop.getProperty("param"));

HttpClient httpClient = new DefaultHttpClient();
//443 https的默认端口号
httpClient.getConnectionManager().getSchemeRegistry().registry().register(new Scheme("https",443,socketFactory));

HttpPost hp = new HttpPost(url);
hp.setHeader("Context-Type","application/json;charset=UTF-8");
hp.setHeader("Accept","application/json;charset=UTF-8");
hp.setEntity(new StringEntity(json.toString()));

HttpResponse httpResponse = httpClient.execute(hp);
HttpEntity entity = hp.getEntity();
httpResponse.getStatusLine().getStatusCode();
EntityUtils.toString(entity,"UTF-8");

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (KeyStoreException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (CertificateException e) {
e.printStackTrace();
} catch (UnrecoverableKeyException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
}
finally{
if(null != inStream){
inStream.close();
inStream = null;
}
}

}



}
get,put,delete,post等方法,使用同上

代码中用的证书下次再写,怎么查看证书,内容
Kettle 是一款开源的数据集成工具,支持通过 REST 接口进行调用。它提供了一组 RESTful API,可以用于执行转换(Transformation)和作业(Job)。 要使用 Kettle 的 REST 接口进行调用,您需要了解以下几个关键概念: 1. 转换(Transformation):表示数据处理的流程。它由一系列的步骤(Steps)组成,每个步骤执行特定的数据处理操作。 2. 作业(Job):表示一组转换和其他控制逻辑的组合,用于实现更复杂的工作流程。 3. 转换和作业的定义文件:转换使用 .ktr 文件进行定义,作业使用 .kjb 文件进行定义。 下面是使用 Kettle REST API 进行转换和作业调用的一般步骤: 1. 构建请求:根据 API 文档,构建包含必要参数和请求体的 HTTP 请求。 2. 发送请求:使用 HTTP 客户端库发送请求到 Kettle 的 REST 接口。 3. 处理响应:解析服务器返回的响应,获取需要的信息。 以下是一些常见的 Kettle REST API 调用示例: - 执行转换:使用 POST 请求发送转换定义文件(.ktr 文件)到 `/kettle/executeTrans` 接口。 - 执行作业:使用 POST 请求发送作业定义文件(.kjb 文件)到 `/kettle/executeJob` 接口。 - 获取转换或作业的状态:使用 GET 请求访问 `/kettle/status` 接口,并提供转换或作业的 ID。 - 获取转换或作业的日志:使用 GET 请求访问 `/kettle/log` 接口,并提供转换或作业的 ID。 请注意,具体的 API 调用方式和参数取决于您使用的 Kettle 版本和配置。建议查阅 Kettle 的官方文档或相关资源,以获取更详细的信息和示例代码。 希望这些信息对您有所帮助!如果您有任何进一步的问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值