首页 > 编程知识 正文

curl命令get请求(java调用linux命令)

时间:2023-05-05 05:51:00 阅读:75676 作者:4435

curl-k-xpost ' https://localhost :9200/myweb/myrep/bbdylinput _ stringbbdyl/_ update '-d ' { ' doc ' 3360 }

在上调用以获取XML文件的input_string列表

编写bash脚本以完成上述任务,然后从Java代码中调用此脚本

RunTime.exec ()在for循环中调用curl命令

还有其他更好的方法吗?

这将是整个Java程序中我执行各种其他事情的重要步骤之一。 这就是为什么我们正在寻找与Java代码成功集成的方法,而不是提供作为单独的CLI脚本运行的选项。

解决方法:

这是通过HTTPURLConnection实现POST和GET。

import Java.io.bytearray inputstream;

import Java.io.bytearray output stream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.io.tzdgz;

import Java.net.httpurl connection;

import java.net.URL;

import java.util.HashMap;

import java.util.Map;

公共类postget

publicstaticvoidmain (stringxsdbbzargs ) throws tzdgz {

Map headerMap=new HashMap (;

stringbodystr=' {' doc' : {' status' :' disconnected' };

post(http://127.0.0.1:3000 ),headerMap,body );

get(http://127.0.0.1:3000? test=hello ',headerMap;

}

publicstaticvoidpost (字符串目标,映射头映射,

try {

http=(httpurlconnection ) newurl (targeturl ).openConnection );

HTP.setrequestmethod('post );

http.setdooutput(true;

for (map.entry header : header map.entryset ) )

http.setrequestproperty (header.getkey )、header.getValue );

}

out.write(readinput ) body );

out.close (;

string response=new string (readinput (in )、' UTF-8 ';

}catch(ioexceptione ) {

e .打印堆栈跟踪(;

}

}

publicstaticvoidget (字符串目标,映射头映射)。

try {

http=(httpurlconnection ) newurl (targeturl ).openConnection );

HTTP.setrequestmethod('get );

for (map.entry header : header map.entryset ) )

http.setrequestproperty (header.getkey )、header.getValue );

}

string response=new string (readinput (in )、' UTF-8 ';

}catch(ioexceptione ) {

e .打印堆栈跟踪(;

}

}

publicstaticbytexsdbbzreadinput {

bytearrayoutputstreambais=newbytearrayoutputstream (;

bytexsdbbz buffer=new byte[1024];

int readLen=-1;

try {

while((readlen=in.read ) ) buffer!=-1 ()

bais.write(buffer,0,readLen );

}

in.close (;

}catch(ioexceptione ) {

e .打印堆栈跟踪(;

}

return bais.toByteArray (;

}

}

可以通过以下方法测试以下Nodejs服务

http=请求(http );

if(req.method=='post ' )。

console.log('post );

var body=' ';

req.on('data ',function ) data )

主体=数据;

控制台. log (partial body : ) body;

);

req.on('end ',function ) )。

控制台. log (body : ) body;

);

RES.writehead(200,{ ' content-type ' : ' text/html ' };

RES.end (后接收);

}elseif(req.method==='get ' ) ) )。

控制台. 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;

}

);

端口=3000;

主机=' 127.0.0.1 ';

控制台. log (listening at http://' host ' : ' port );

Java客户端xhdsl

发送开机自检

响应代码: 200

响应头: { transfer-encoding=[ chunked ],null=[HTTP/1.1 200 OK],Connection=[keep-alive],Date=

响应接收器:开机自检接收

发送获取

响应代码: 200

响应头: { transfer-encoding=[ chunked ],null=[HTTP/1.1 200 OK],Connection=[keep-alive],Date=

响应来自服务器:

test=hello节点服务器xhdsl

listening at http://127.0.0.1:3000

信箱

partial body : { ' doc ' : { ' status ' : ' disconnected ' }

body : { ' doc ' : { ' status ' : ' disconnected ' }

获取

params : test=hello

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。