在Java中复制Curl unix-socket命令 [英] Replicate Curl unix-socket command in Java

查看:159
本文介绍了在Java中复制Curl unix-socket命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

键入以下内容时,我想获得curl输出的相同字符串标准:

I want to get the same string standard outputted by curl when I type something like:

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

在我的Java代码中。
我发现我需要使用AFUNIXSocket,但是我只能在连接上设置套接字文件,所以我想我必须手动管理我的http连接。

In my Java code. I figured out I need to use AFUNIXSocket, but I can set just the socket file on connection, so I suppose I have to manage my http connection manually.

有没有办法使它变得容易?如何通过我创建的套接字启动http连接?

Is there a way to make it easier? How do I start an http connection through the socket I create?

推荐答案

因此,我尝试自己管理http连接,它可以工作,但是我仍然在寻找一种更清洁的方式来做到这一点。
这就是我的做法:

So, I tried to manage the http connection by myself, it works but Im still looking for a cleaner way to do that. That's how I did:

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";

进行卷曲调用的方法相同:

That is the same to do a curl call like that:

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

这篇关于在Java中复制Curl unix-socket命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆