我对Spring-xd完全陌生,我的任务是创建一个流,该流将发布到特定端口(例如:我的本地主机)的http数据存储在本地文件中
首先,我部署了此流:
xd:> stream create --name otherfilestream --definition "http --port=8080 |
file --name=myfile --dir=/some/custom/directory" --deploy
每当我打电话时,它就会工作并在系统上创建本地文件: xd:> http post --target http://localhost:8080 --data“ hello”
现在,我想知道如何使用Java代码触发此发布请求。 我尝试过传统的发帖请求方式,但是没有用。
private void sendPost() throws Exception {
String url = "http://localhost:8080/";
URL obj = new URL(null, url, new sun.net.www.protocol.https.Handler());
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", USER_AGENT);
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
String urlParameters = "Hello.txt";
// Send post request
con.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + urlParameters);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
//print result
System.out.println(response.toString());
}
}
我的问题是..从外部Java代码发送发布请求的正确方法是什么?
谢谢