node.js POST请求失败 [英] node.js POST request fails
问题描述
我试图用node.js执行POST请求,但它总是似乎超时。我也试着在PHP中的cURL做的请求,只是为了确保和那工作正常。此外,当在本地服务器(127.0.0.1)而不是远程服务器上执行完全相同的请求时,它也工作得很好。
node.js :
var postRequest = {
host:www.facepunch.com,
path:/newreply.php?do=postreply&t=+ threadid,
port:80,
method:POST,
header:{
Cookie: cookie,
'Content-Type':'application / x-www-form-urlencoded'
}
};
buffer =;
var req = http.request(postRequest,function(res)
{
console.log(res);
res.on(data,function (data){buffer = buffer + data;});
res.on(end,function(){require(fs).writeFile(output.html,buffer);});
});
var body =postdata\r\\\
;
postRequest.headers [Content-Length] = body.length;
req.write(body);
req.end();
cURL和PHP
<?php
if($ _SERVER [REMOTE_ADDR] ==127.0.0.1)
{
$ body = 身体;
$ ch = curl_init();
curl_setopt($ ch,CURLOPT_URL,http://www.facepunch.com/newreply.php?do=postreply&t=。$ threadid);
curl_setopt($ ch,CURLOPT_POST,15);
curl_setopt($ ch,CURLOPT_POSTFIELDS,$ body);
curl_setopt($ ch,CURLOPT_COOKIE,cookie);
curl_setopt($ ch,CURLOPT_RETURNTRANSFER,1);
$ result = curl_exec($ ch);
curl_close($ ch);
}
?>这里是怎么回事?
< >解决方案您将标头传递到http请求调用,然后尝试在事实之后添加 Content-Length
标头。在传递值之前,您应该这样做,因为它改变了http请求设置 Transfer-Encoding
的方式:
var body =postdata;
var postRequest = {
host:www.facepunch.com,
path:/newreply.php?do=postreply&t=+ threadid,
port:80,
方法:POST,
header:{
'Cookie':cookie,
'Content-Type':'application / www-form-urlencoded',
'Content-Length':Buffer.byteLength(body)
}
};
var buffer =;
var req = http.request(postRequest,function(res)
{
console.log(res);
res.on(data,function (data){buffer = buffer + data;});
res.on(end,function(){require(fs).writeFile(output.html,buffer);});
});
req.write(body);
req.end();
I'm trying to perform a POST request with node.js, but it always seems to time out. I also tried doing the request with cURL in PHP just to make sure and that works fine. Also, when performing the exact same request on my local server (127.0.0.1) instead of the remote server, it works perfectly fine too.
node.js:
var postRequest = {
host: "www.facepunch.com",
path: "/newreply.php?do=postreply&t=" + threadid,
port: 80,
method: "POST",
headers: {
Cookie: "cookie",
'Content-Type': 'application/x-www-form-urlencoded'
}
};
buffer = "";
var req = http.request( postRequest, function( res )
{
console.log( res );
res.on( "data", function( data ) { buffer = buffer + data; } );
res.on( "end", function() { require( "fs" ).writeFile( "output.html", buffer ); } );
} );
var body = "postdata\r\n";
postRequest.headers["Content-Length"] = body.length;
req.write( body );
req.end();
cURL and PHP
<?php
if ( $_SERVER["REMOTE_ADDR"] == "127.0.0.1" )
{
$body = "body";
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, "http://www.facepunch.com/newreply.php?do=postreply&t=" . $threadid );
curl_setopt( $ch, CURLOPT_POST, 15 );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $body );
curl_setopt( $ch, CURLOPT_COOKIE, "cookie" );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
$result = curl_exec( $ch );
curl_close( $ch );
}
?>
What is going on here?
解决方案 You're passing the headers to the http request call and then trying to add the Content-Length
header after the fact. You should be doing that before you pass in the values, as it changes the way http request sets up Transfer-Encoding
:
var body = "postdata";
var postRequest = {
host: "www.facepunch.com",
path: "/newreply.php?do=postreply&t=" + threadid,
port: 80,
method: "POST",
headers: {
'Cookie': "cookie",
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': Buffer.byteLength(body)
}
};
var buffer = "";
var req = http.request( postRequest, function( res )
{
console.log( res );
res.on( "data", function( data ) { buffer = buffer + data; } );
res.on( "end", function() { require( "fs" ).writeFile( "output.html", buffer ); } );
} );
req.write( body );
req.end();
这篇关于node.js POST请求失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!