Node.js:远程提交表单

发布于 2024-12-11 04:20:00 字数 1716 浏览 0 评论 0原文

我目前正在开发一种 Node.js 的 Web 代理,但我在提交表单时遇到了麻烦,在大多数网站上我都能成功提交表单,但在其他一些网站上我就没那么幸运了。我无法确定我是否做错了什么。

有没有更好的方法可以做到这一点?

另外,我如何使用 Express.js bodyparser 处理多部分表单?

目前,我的表单处理方式是这样的:

function proxy(req, res,request)
{ 

var sess = req.session;

var onUrl_Parse = function(url){
        var  Uri= new URI.URI(url);//Parses incoming url
            var options = {
                uri: url,
                method: req.method
            }

                options.headers={"User-Agent": "Mozilla/5.0 (Windows  NT 6.1; rv:6.0) Gecko/20110814 Firefox/6.0", "Cookie":req.session.cook};



        if(req.body) //If x-www-form-urlencoded is posted.
        {
            var options = {
                uri: url,
                method: req.method,
                body: req.rawBody
            }
                options.headers={"User-Agent": "Mozilla/5.0 (Windows  NT 6.1; rv:6.0) Gecko/20110814 Firefox/6.0", "Cookie":req.session.cook, "Content-Type":"application/x-www-form-urlencoded"};
        }


    onRequestOptions(options, url);
}

,onRequestOptions = function(options, url)
{       
    request(options, function(error, response, body)
    {       
            if(!error){
                    if(response.headers['set-cookie'])
                        req.session.cook=response.headers['set-cookie'];
                Proxy_Parser.Parser(body, url, async, onParse);// Parses returned html return displayable content
            }

    });
}

,onParse = function(HTML_BODY)
    {
        if(HTML_BODY=="")
            res.end();
        res.write(HTML_BODY);
        res.end();
        console.log("DONEEEEE");
    }   



Url_Parser.Url(req, URI, onUrl_Parse);

}

I'm currently working on a sort of Web Proxy for Node.js, but I am having trouble with submitting forms, on most sites I am able to successfully submit a form but on some other sites I am not so fortunate. I can't pinpoint if there is anything I'm doing wrong.

Is there a possible better way of doing this?

Also, how would I be able to handle multipart forms using the Express.js bodyparser?

At the moment this is what I have in the way of form processing is this:

function proxy(req, res,request)
{ 

var sess = req.session;

var onUrl_Parse = function(url){
        var  Uri= new URI.URI(url);//Parses incoming url
            var options = {
                uri: url,
                method: req.method
            }

                options.headers={"User-Agent": "Mozilla/5.0 (Windows  NT 6.1; rv:6.0) Gecko/20110814 Firefox/6.0", "Cookie":req.session.cook};



        if(req.body) //If x-www-form-urlencoded is posted.
        {
            var options = {
                uri: url,
                method: req.method,
                body: req.rawBody
            }
                options.headers={"User-Agent": "Mozilla/5.0 (Windows  NT 6.1; rv:6.0) Gecko/20110814 Firefox/6.0", "Cookie":req.session.cook, "Content-Type":"application/x-www-form-urlencoded"};
        }


    onRequestOptions(options, url);
}

,onRequestOptions = function(options, url)
{       
    request(options, function(error, response, body)
    {       
            if(!error){
                    if(response.headers['set-cookie'])
                        req.session.cook=response.headers['set-cookie'];
                Proxy_Parser.Parser(body, url, async, onParse);// Parses returned html return displayable content
            }

    });
}

,onParse = function(HTML_BODY)
    {
        if(HTML_BODY=="")
            res.end();
        res.write(HTML_BODY);
        res.end();
        console.log("DONEEEEE");
    }   



Url_Parser.Url(req, URI, onUrl_Parse);

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

满地尘埃落定 2024-12-18 04:20:00

我不确定你到底想要完成什么,但是 https://github.com/felixge/node-无论如何,强大是推荐的!

I am not sure what exactly you are trying to accomplish, but https://github.com/felixge/node-formidable is a anyway recommended !!

空宴 2024-12-18 04:20:00

我会从 node-http-proxy 之类的东西开始。所有艰苦的工作都已为您完成,您只需定义要代理的路由并为自定义响应信息放入一些处理程序即可。

I would start with something like node-http-proxy. All the hard work is done for you and you can just define the routes you want to proxy and put in some handlers for the custom response info.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文