1. 程式人生 > >linux通過curl方式呼叫介面

linux通過curl方式呼叫介面

今天遇到個需求,linux上將txt檔案匯入資料庫,需要監測txt中的資料是否完全匯入了資料庫,即txt中的行數是否和資料庫中的行數是否一致的問題。問題是獲取txt行數需要執行linux指令,需要java程式碼知道IP 埠 使用者名稱 密碼;線上安全問題,根本不允許java應用有這麼大的許可權。

經過tutor指點,可以反過來,讓linux上使用指令呼叫我的java應用即可,即使用curl指令呼叫java介面。

1、呼叫get請求
$ curl http://www.linuxidc.com/login.cgi?user=test001&password=123456

2、呼叫post請求
$ curl -d "user=nickwolfe&password=12345" http://www.linuxidc.com/login.cgi

3、xml格式post請求

方式一:傳送磁碟上面的xml檔案(推薦)
root [ /apps ]$ curl -X POST -H 'content-type: application/xml'  -d @/apps/myxmlfile.txt http://172.19.219.xx:8081/csp/faq/actDiaUserInfo.action
ps:其中myxmlfile.txt為磁碟上面的xml檔案,後面為請求路徑

方式二:在命令列直接傳送xml結構資料

root [ /apps ]$ curl -H 'content-type: application/xml' -X POST -d '<?xml version="1.0" encoding="UTF-8"?><userinfoReq><subsNumber>13814528620</subsNumber><type>3</type></userinfoReq>' http://172.19.219.xx:8081/csp/faq/actDiaUserInfo.action
或者

root [ /apps ]$ echo '<?xml version="1.0" encoding="UTF-8"?><userinfoReq><subsNumber>13814528620</subsNumber><type>3</type></userinfoReq>'|curl -X POST -H'Content-type:text/xm' -d @- http://172.19.xx.xx:8081/csp/faq/actDiaUserInfo.action
ps:其中<?xml version...>就是要post的xml 檔案,後面是請求路徑,linux上雙引號或單引號之間巢狀需要使用反斜槓 \ 進行轉義

響應訊息:

<?xml version="1.0" encoding="utf-8"?><result><result_code>0</result_code><result_text>success</result_text><getUserinfoRsp><userInfo><identityID>1117384802</identityID><phone>13814528620</phone><email>[email protected]</email><accountID></accountID><province>江蘇</province><city>南京</city><passId>7775637869243</passId></userInfo></getUserinfoRsp></result>
 

4、json格式post請求

方式一:傳送磁碟上面的JSON檔案(推薦)
root [ /apps ]$ curl -X POST -H 'content-type: application/json'  -d @/apps/myjsonfile.txt http://192.168.129.xx/AntiRushServer/api/ActivityAntiRush
ps:其中myjsonfile.txt為磁碟上面的JSON檔案,後面為請求路徑

方式二:在命令列直接傳送JSON結構資料
root [ ~ ]$ curl -H 'content-type: application/json' -X POST -d '{"accountType":"4","channel":"1","channelId":"YW_MMY","uid":"13154897541","phoneNumber":"13154897541","loginSource":"3","loginType":"1","userIp":"192.168.2.3","postTime":"14633fffffffffff81286","userAgent":"Windows NT","imei":"352600051025733","macAddress":"40:92:d4:cb:46:43","serialNumber":"123"}' http://192.168.129.xx/AntiRushServer/api/ActivityAntiRush
響應訊息:
{"code":"4000","message":"引數錯誤:time的值不是UInt"}

方式一:傳送磁碟上面的請求報文檔案(推薦)
root [ /apps ]$ curl -H 'Content-Type: text/xml;charset=UTF-8;SOAPAction:""' -d @/apps/mysoapfile.xml http://172.18.173.xx:8085/csp-magent-client/madapterservices/madapter/lmCountAccessor
ps:其中myjsonfile.txt為磁碟上面的請求報文檔案,後面為請求路徑

方式二:在命令列直接傳送xml結構資料
[plain] view plain copy
<code class="language-plain">root [ /apps ]$ curl -H 'content-type: application/xml' -d '<?xml version="1.0" encoding="utf-8"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://service.accessor.madapter.csp.huawei.com"><soapenv:Header /><soapenv:Body><ser:leaveMessageCount><ser:in0><![CDATA[20161011160516XdznbN]]></ser:in0><ser:in1><![CDATA[1600106496388382726]]></ser:in1><ser:in2><![CDATA[14]]></ser:in2><ser:in3><![CDATA[<extendParams><channelid>1600</channelid><servicetype></servicetype><appid></appid><usertype>10</usertype><userid>6496388382726</userid><msisdn>13814528620</msisdn><email></email><account></account><nickname></nickname><questionType></questionType></extendParams>]]></ser:in3></ser:leaveMessageCount></soapenv:Body></soapenv:Envelope>' http://172.18.173.xx:8085/csp-magent-client/madapterservices/madapter/lmCountAccessor</code>  
響應訊息
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soap:Body><ns1:leaveMessageCountResponse xmlns:ns1="http://service.accessor.madapter.csp.huawei.com"><ns1:out>8</ns1:out></ns1:leaveMessageCountResponse></soap:Body></soap:Envelope>

借鑑:https://blog.csdn.net/russ44/article/details/53308838