从Java发布数据并在node.js application中接收
我用Java编写了一个演示,每秒发布一次数据:从Java发布数据并在node.js application中接收,java,node.js,post,Java,Node.js,Post,我用Java编写了一个演示,每秒发布一次数据: public static void main(String[] arystrArgs) { //Get Operating System name and version String strOSname = System.getProperty("os.name").toLowerCase(); //Display application title and what it is running on
public static void main(String[] arystrArgs) {
//Get Operating System name and version
String strOSname = System.getProperty("os.name").toLowerCase();
//Display application title and what it is running on
System.out.println("Java Data Posting Demo");
System.out.println("Build date: " + BUILD_DATE + ", Version: " + VERSION_NO);
System.out.println("Running on: " + strOSname.substring(0, 1).toUpperCase()
+ strOSname.substring(1).toLowerCase());
//Post data to server
HttpURLConnection conn = null;
while( true ) {
try {
Thread.sleep(DELAY_BETWEEN_POSTS);
URL url = new URL("http://localhost:8080");
conn = (HttpURLConnection)url.openConnection();
if ( conn != null ) {
//Whatever you wants to post...
String strPostData = "p1=Hello&p2=" + (new Date()).getTime();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-length", Integer.toString(strPostData.length()));
conn.setRequestProperty("Content-Language", "en-GB");
conn.setRequestProperty("charset", "utf-8");
conn.setUseCaches(false);
conn.setDoOutput(true);
DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(strPostData);
dos.close();
System.out.println("Post to: " + url.toString() + ", data: " + strPostData);
}
} catch (InterruptedException|IOException ex) {
//ex.printStackTrace();
} finally {
if ( conn != null ) {
conn.disconnect();
conn = null;
}
}
}
}
我已经编写了一个Node.js应用程序,它侦听端口8080,但在http处理程序中没有看到任何POST请求,我只在使用相同地址和端口的浏览器进行测试时看到GET请求
node.js应用程序中的代码段:
function defaultHandler(request, response) {
try{
if ( request.method == "POST" ) {
var strBody = "";
request.on("data", function(chunk) {
strBody += chunk;
});
request.on("end", function() {
console.log("Received posted data: " + strBody);
});
} else {
console.dir(request);
}
} catch( ex ) {
console.dir(ex);
}
};
var app = http.createServer(defaultHandler);
app.listen(8080);
这是一个精简版,但我所看到的只是获取请求。我可以看到Java正在连接和发布数据,就像我启动Node.js时一样,只有当我启动Node.js时,Java应用程序连接到URL,然后在发布之间的第二个延迟时间开始发布,如果我终止节点,然后它停止发布,重新启动节点会导致发布恢复。您的节点应用程序从不向客户端发送响应。您发送了大量请求,但Java客户端从未收到来自服务器的响应。您应该在响应时执行end方法
var http = require('http');
function defaultHandler(request, response) {
try {
if (request.method == "POST") {
var strBody = "";
request.on("data", function(chunk) {
strBody += chunk;
});
request.on("end", function() {
console.log("Received posted data: " + strBody);
});
} else {
console.dir(request);
}
respone.end(); // for example here
} catch (ex) {
console.dir(ex);
}
};
var app = http.createServer(defaultHandler);
app.listen(8080);
在这里您可以找到文档-
我不是Java开发人员,但我认为您可以尝试在连接上使用flush和getResponseCode方法
conn.setDoOutput(true);
DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(strPostData);
dos.flush();
dos.close();
int responseCode = conn.getResponseCode();
您的节点应用程序从不向客户端发送响应。您发送了大量请求,但Java客户端从未收到来自服务器的响应。您应该在响应时执行end方法
var http = require('http');
function defaultHandler(request, response) {
try {
if (request.method == "POST") {
var strBody = "";
request.on("data", function(chunk) {
strBody += chunk;
});
request.on("end", function() {
console.log("Received posted data: " + strBody);
});
} else {
console.dir(request);
}
respone.end(); // for example here
} catch (ex) {
console.dir(ex);
}
};
var app = http.createServer(defaultHandler);
app.listen(8080);
在这里您可以找到文档-
我不是Java开发人员,但我认为您可以尝试在连接上使用flush和getResponseCode方法
conn.setDoOutput(true);
DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(strPostData);
dos.flush();
dos.close();
int responseCode = conn.getResponseCode();
修正了,是Java,我修改了Java代码如下:
URL url = new URL("http://localhost:8080");
conn = (HttpURLConnection)url.openConnection();
if ( conn != null ) {
//Whatever you wants to post...
String strPostData = "p1=Hello&p2=" + (new Date()).getTime();
conn.setRequestMethod("POST");
conn.setRequestProperty("User-Agent", USER_AGENT);
conn.setRequestProperty("Accept-Language", "en-GB,en;q=0.5");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-length", Integer.toString(strPostData.length()));
conn.setRequestProperty("Content-Language", "en-GB");
conn.setRequestProperty("charset", "utf-8");
conn.setUseCaches(false);
conn.setDoOutput(true);
DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(strPostData);
dos.flush();
dos.close();
int intResponse = conn.getResponseCode();
System.out.println("\nSending 'POST' to " + url.toString() +
", data: " + strPostData + ", rc: " + intResponse);;
}
修正了,是Java,我修改了Java代码如下:
URL url = new URL("http://localhost:8080");
conn = (HttpURLConnection)url.openConnection();
if ( conn != null ) {
//Whatever you wants to post...
String strPostData = "p1=Hello&p2=" + (new Date()).getTime();
conn.setRequestMethod("POST");
conn.setRequestProperty("User-Agent", USER_AGENT);
conn.setRequestProperty("Accept-Language", "en-GB,en;q=0.5");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-length", Integer.toString(strPostData.length()));
conn.setRequestProperty("Content-Language", "en-GB");
conn.setRequestProperty("charset", "utf-8");
conn.setUseCaches(false);
conn.setDoOutput(true);
DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(strPostData);
dos.flush();
dos.close();
int intResponse = conn.getResponseCode();
System.out.println("\nSending 'POST' to " + url.toString() +
", data: " + strPostData + ", rc: " + intResponse);;
}
我将尝试将response.end()添加到node.js代码中,但就Java而言,我尝试刷新并关闭,没有任何更改。我尝试在node.js收到POST请求时添加控制台消息,但它从不显示指示node.js未接收POST请求的消息。您确定Java发送了请求吗?我与Postman(发送请求)检查了您的节点代码,该代码正常工作。我曾写信告诉您该节点工作正常,问题是Java。。。但我不是Java开发人员,我只能在NodeI方面帮助你。我会尝试将response.end()添加到node.js代码中,但就Java而言,我尝试刷新并关闭,没有任何更改。我尝试在node.js收到POST请求时添加控制台消息,但它从不显示指示node.js未接收POST请求的消息。您确定Java发送了请求吗?我与Postman(发送请求)检查了您的节点代码,该代码正常工作。我曾写信告诉您该节点工作正常,问题是Java。。。但我不是Java开发人员,我只能在Node方面帮助您