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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值