簡體   English   中英

在Java中復制Curl unix-socket命令

[英]Replicate Curl unix-socket command in Java

我想輸入類似curl時輸出的相同字符串標准:

curl --unix-socket file.sock http://path/to/rest/request

在我的Java代碼中。 我發現我需要使用AFUNIXSocket,但是我只能在連接上設置套接字文件,因此我想必須手動管理http連接。

有沒有辦法使它更容易? 如何通過我創建的套接字啟動http連接?

因此,我嘗試自己管理http連接,但它仍然有效,但我仍在尋找一種更清潔的方式來實現。 那是我做的:

AFUNIXSocket s = AFUNIXSocket.newInstance();
AFUNIXSocketAddress sockAdd = new AFUNIXSocketAddress(new File("file.sock"));

s.connect(sockAdd);

BufferedReader bf = new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter pw = new PrintWriter(s.getOutputStream());


pw.write("GET /to/rest/request HTTP/1.1\r\n");

pw.write("Host: path\r\n");
pw.write("Accept:*/*\r\n");
pw.write("\r\n");
pw.flush();
String reply = "";

Thread.sleep(500);
while (bf.ready())
   reply = reply + bf.readLine() + "\n";

像這樣進行curl調用是相同的:

curl --unix-socket file.sock http://path/to/rest/request

暫無
暫無

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

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