日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > java >内容正文

java

curl命令java_从Java调用curl命令

發(fā)布時(shí)間:2024/1/1 java 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 curl命令java_从Java调用curl命令 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

curl -k -XPOST 'https://localhost:9200/myweb/myrep/**input_string**/_update' -d '{"doc":{"status":"Disconnected"}}'

在上面調(diào)用以獲取XML文件中的input_string列表

選項(xiàng)1:

編寫(xiě)一個(gè)bash腳本以完成上述任務(wù),然后從Java代碼中調(diào)用此腳本

選項(xiàng)2:

RunTime.exec()在for循環(huán)中調(diào)用curl命令

像這樣:curl command in java

還有其他更好的方法嗎?

這將是我執(zhí)行各種其他事情的整個(gè)Java程序中的重要步驟之一.這就是我在尋找將其與Java代碼很好地集成的方法的原因,而不是提供作為單獨(dú)的CLI腳本在上面運(yùn)行的選項(xiàng).

解決方法:

這是通過(guò)HTTPURLConnection進(jìn)行POST和GET的實(shí)現(xiàn).

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();

}

}

下面是一個(gè)Nodejs服務(wù)器,可以通過(guò)以下方式對(duì)此進(jìn)行測(cè)試

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客戶(hù)端輸出

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

節(jié)點(diǎn)服務(wù)器輸出

Listening at http://127.0.0.1:3000

POST

Partial body: {"doc":{"status":"Disconnected"}}

Body: {"doc":{"status":"Disconnected"}}

GET

params : test=hello

標(biāo)簽:bash,rest,curl,java

來(lái)源: https://codeday.me/bug/20191119/2034548.html

總結(jié)

以上是生活随笔為你收集整理的curl命令java_从Java调用curl命令的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。