簡體   English   中英

在java中運行外部程序會產生與terminal命令不同的結果

[英]Running external program within java produces different results from terminal command

嘿我正在嘗試在java運行命令:

String curlCommand=String.format("curl --max-time %d --socks5 %s -A \"%s\"  -i %s",maxWait,proxy ,getAgent(), myurl);
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(curlCommand);

由於某種原因,執行此行將導致cookies被拒絕。

但是,當我從終端運行卷曲程序時,我沒有任何問題,一切正常

這是我運行程序Java

Date: Tue, 03 Mar 2015 19:20:38 GMT
X-Li-Pop: prod-lva1
X-LI-UUID: 8C548z0TyBMwY002tCoAAA==
Set-Cookie: denial-reason-code=3,2,8; Max-Age=5
Set-Cookie: denial-client-ip=xxxxxxx; Max-Age=5
Content-Length: 1852
Content-Type: text/html

如果我在terminal使用curl運行完全命令,結果如下:

HTTP/1.1 200 OK
Server: Play
X-SSR-Engine-Init: <xxxxxxx>
X-FS-UUID: 0e69d33f4913c813b05f1aefb42a0000
X-Page-Speed: 1
Date: Tue, 03 Mar 2015 19:21:27 GMT
Content-Length: 31113
Content-Type: text/html; charset=utf-8
X-Frame-Options: sameorigin
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
X-Li-Fabric: prod-lva1
Strict-Transport-Security: max-age=0
Set-Cookie: JSESSIONID="ajax:4773794645302404176"; Path=/; Domain=<xxxxxx>; HTTPOnly
Set-Cookie: bcookie="v=2&da2801ec-e7e2-4b04-8244-401d6ac0e7c3"; domain=xxxxx; Path=/; Expires=Fri, 03-Mar-2017 06:58:59 GMT
Set-Cookie: bscookie="v=1&2015030319212713ed4940-eb5d-4860-8c30-2eec4819157aAQHMGWOb0Fiv_feTewpb6Rw3TGGAGQ_Q"; domain=<xxxxxx>; Path=/; Secure; Expires=Fri, 03-Mar-2017 06:58:59 GMT; HttpOnly
Pragma: no-cache
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Cache-Control: no-cache, no-store
Connection: keep-alive
X-Li-Pop: prod-lva1
X-LI-UUID: DmnTP0kTyBOwXxrvtCoAAA==
Set-Cookie: lidc="b=VB97:g=149:u=1:i=1425410487:t=1425496887:s=AQHAOhdkFxryw00Wo64LkfjsYb8Q1hqZ"; Expires=Wed, 04 Mar 2015 19:21:27 GMT; domain=<xxxxx>; Path=/

所以我的問題是為什么它應該是完全不同的,我在這里遺漏了什么?

好吧,我終於解決了它。

原因是Runtime runtime = Runtime.getRuntime();

不應該使用它。 因為它只是弄亂了參數(雖然命令打印出來看起來很好,但它只是沒有向OS發送正確的命令)而是我應該使用ProcessBuilder class 它就像一個魅力。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM