[英]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.