curl命令java_从Java调用curl命令
curl -k -XPOST 'https://localhost:9200/myweb/myrep/**input_string**/_update' -d '{"doc":{"status":"Disconnected"}}'
在上面调用以获取XML文件中的input_string列表
选项1:
编写一个bash脚本以完成上述任务,然后从Java代码中调用此脚本
选项2:
RunTime.exec()在for循环中调用curl命令
像这样:curl command in java
还有其他更好的方法吗?
这将是我执行各种其他事情的整个Java程序中的重要步骤之一.这就是我在寻找将其与Java代码很好地集成的方法的原因,而不是提供作为单独的CLI脚本在上面运行的选项.
解决方法:
这是通过HTTPURLConnection进行POST和GET的实现.
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
public class PostGET {
public static void main(String[] args) throws UnsupportedEncodingException {
Map headerMap = new HashMap();
String bodyStr = "{\"doc\":{\"status\":\"Disconnected\"}}";
InputStream body = new ByteArrayInputStream(bodyStr.getBytes("UTF-8"));
System.out.println("Sending POST");
post("http://127.0.0.1:3000", headerMap, body);
System.out.println("");
System.out.println("Sending GET");
get("http://127.0.0.1:3000?test=hello", headerMap);
}
public static void post(String targetUrl, Map headerMap,
InputStream body) {
HttpURLConnection http = null;
try {
http = (HttpURLConnection) new URL(targetUrl).openConnection();
http.setRequestMethod("POST");
http.setDoOutput(true);
for (Map.Entry header : headerMap.entrySet()) {
http.setRequestProperty(header.getKey(), header.getValue());
}
OutputStream out = http.getOutputStream();
out.write(readInput(body));
out.close();
InputStream in = http.getInputStream();
String response = new String(readInput(in), "UTF-8");
System.out.println("Response code: " + http.getResponseCode());
System.out.println("Response headers : " + http.getHeaderFields());
System.out.println("response from server: " + response);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void get(String targetUrl, Map headerMap) {
HttpURLConnection http = null;
try {
http = (HttpURLConnection) new URL(targetUrl).openConnection();
http.setRequestMethod("GET");
for (Map.Entry header : headerMap.entrySet()) {
http.setRequestProperty(header.getKey(), header.getValue());
}
InputStream in = http.getInputStream();
String response = new String(readInput(in), "UTF-8");
System.out.println("Response code: " + http.getResponseCode());
System.out.println("Response headers : " + http.getHeaderFields());
System.out.println("response from server: " + response);
} catch (IOException e) {
e.printStackTrace();
}
}
public static byte[] readInput(InputStream in) {
ByteArrayOutputStream bais = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int readLen = -1;
try {
while ((readLen = in.read(buffer)) != -1) {
bais.write(buffer, 0, readLen);
}
in.close();
} catch (IOException e) {
e.printStackTrace();
}
return bais.toByteArray();
}
}
下面是一个Nodejs服务器,可以通过以下方式对此进行测试
http = require('http');
server = http.createServer( function(req, res) {
if (req.method == 'POST') {
console.log("POST");
var body = '';
req.on('data', function (data) {
body += data;
console.log("Partial body: " + body);
});
req.on('end', function () {
console.log("Body: " + body);
});
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('post received');
} else if(req.method === 'GET') {
console.log("GET");
var params = req.url.split('?');
params = params.length > 1 ? params[1] : "";
console.log('params : ' + params);
var html = '
' + params+ '
';
res.writeHead(200, {'Content-Type': 'text/html'});
res.end(html);
}
});
port = 3000;
host = '127.0.0.1';
server.listen(port, host);
console.log('Listening at http://' + host + ':' + port);
Java客户端输出
Sending POST
Response code: 200
Response headers : {Transfer-Encoding=[chunked], null=[HTTP/1.1 200 OK], Connection=[keep-alive], Date=[Thu, 31 Dec 2015 19:40:05 GMT], Content-Type=[text/html]}
response from server: post received
Sending GET
Response code: 200
Response headers : {Transfer-Encoding=[chunked], null=[HTTP/1.1 200 OK], Connection=[keep-alive], Date=[Thu, 31 Dec 2015 19:40:05 GMT], Content-Type=[text/html]}
response from server:
test=hello
节点服务器输出
Listening at http://127.0.0.1:3000
POST
Partial body: {"doc":{"status":"Disconnected"}}
Body: {"doc":{"status":"Disconnected"}}
GET
params : test=hello
标签:bash,rest,curl,java
来源: https://codeday.me/bug/20191119/2034548.html
curl命令java_从Java调用curl命令相关推荐
- java kill 进程命令_Windows使用Java调用taskkill命令结束进程
最近开发的系统需要进行打开Word程序操作,但是有时会出现慕名奇妙的问题导致Word不能正常退出,进而卡死系统的进行,所以研究了些强行杀死Word的方法. 在windows 下,最常使用的结束进程的方 ...
- linux下java命令行参数_Java调用Linux命令行
Java调用Linux命令行 Java语言以其跨平台性和简易性而著称,在Java里面的lang包里(java.lang.Runtime)提供了一个允许Java程序与该程序所运行的环境交互的接口,这就是 ...
- java调用exe_Windows系统中Java调用cmd命令及执行exe程序的方法
Java调用cmd命令,并输出显示信息: package com.anxin.cmd.test; import java.io.BufferedReader; import java.io.Input ...
- android java调用_关于Android中Java调用外部命令的三种方式
此所谓三种方式,只是个人认为.本人还是菜鸟初涉,所以有所错误,请指正. 个人认为,Java调用外部命令.无非三种情况: 一.是只执行命令,不考虑返回值. 二.是执行命令的同时,还需要得到返回值. 三. ...
- java调用外部命令实例
一 java 调用外部命令的方式 一般调用的方式如下: Process pid = null; String[] cmd = { "/bin/sh", "-c" ...
- java调用shell命令并获取执行结果
java调用shell命令并获取执行结果 使用到Process和Runtime两个类,返回值通过Process类的getInputStream()方法获取 [plain] view plain cop ...
- java调用cmd命令执行mysql命令
java调用cmd命令在linux执行mysql数据恢复命令 Statement statement = con.createStatement(); ResultSet resultsql = st ...
- java基础/java调用shell命令和脚本
一.项目需求: 从某一机构获取证书,证书机构提供小工具,执行.sh脚本即可启动服务,本地调用该服务即可获取证书. 问题:linux服务器启动该服务,不能关闭.一旦关闭,服务即停止. 解决方案:java ...
- Java - 调用 Shell 命令和脚本
介绍 有时候我们在Linux中运行Java程序时,需要调用一些Shell命令和脚本.而Runtime.getRuntime().exec()方法给我们提供了这个功能,而且Runtime.getRunt ...
最新文章
- 那个写同龄人正在抛弃你的作者,道理都懂,怎么你还是loser
- 第8天:我用AI算法造了一些“网红”
- Spring Cloud Sidecar –节点初始化
- tf.contrib在tf2中无法使用
- SPSS统计功能与模块对照表
- linux oracle11g开机,Linux Oracle11g 设置自动启动
- 机器人带陀螺仪走钢丝_走直线很难吗?陀螺仪表示,少了它机器人连直线都走不了...
- 安装时间大于30秒_高送转第一股秒板,封单金额近百亿!最新高送转潜力股名单曝光...
- 基于EEMD和樽海鞘算法优化SVM的滚动轴承故障诊断python
- 【那些年学过的计算机基础】--计算机发展史(图片版)
- 万维网联盟将于2014年推出HTML5标准(四)
- MySQL有几部分_数据库系统有哪几部分组成?
- 互联网+O2O 的商业模式
- 【Docker学习】【问题解决】Client.Timeout exceeded while awaiting headers
- WinAPI入门: 第一个标准Win32窗口程序 [改进详细注释版]
- centos 7 查看oracle,Centos7下oracle配置(详细)
- 谷歌五笔输入法电脑版_新手学拼音还是学五笔打字(看完你就明白)
- 使用宝塔搭建环境,以及把自己本地的Web项目通过宝塔发布到远程云服务器上
- [创业-6]:创业中,好领导者需要做的最重要的9件事
- Matlab中定义接口超类