如何让两个 Winjs.xhr 调用一个以从网站检索 Captca,另一个调用以提交数据 [英] How to make two Winjs.xhr call one for retrieving the Captca from a website and another to submit data back

查看:30
本文介绍了如何让两个 Winjs.xhr 调用一个以从网站检索 Captca,另一个调用以提交数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 HTML5/JAVASCRIPT 制作 Windows 应用程序.实际上我想要的是从网站检索验证码,然后将验证码与其他表单字段一起提交到网站(.aspx)并返回响应.我认为我必须为此目的处理 cookie,但我不知道该怎么做.向对此感兴趣的人致敬 100 次.这是我所做的.

I 'm making a windows app using HTML5/JAVASCRIPT . What actually i want is to retrieve a captcha from a website and then submitting the captcha along with other form field to the website(.aspx) back and getting the response back.I think that i have to handle cookies for this purpose ,but i do not know how to do this . 100 times salute to the person who will show interest in this . Here is what i did .

             //  Retrieving  the Captcha.   
            WinJS.xhr({
            url: "http://example.in/main.aspx",
            type: "get",
            responseType: "document",
            headers: {
                "CONTENT-TYPE": "application/x-www-form-urlencoded",

                "User-Agent": "Mozilla/5.0 (Windows NT 6.3; Win64; x64; Trident/7.0; MSAppHost/2.0; rv:11.0) like Gecko",
                "CONNECTION": "keep-alive",
                },
            }).then(
                function complete(xhr) {
                    var image = document.createElement("img");
                    image.src = xhr.response.querySelector("img[alt='Captcha']").src;
                  document.getElementById("captcha").appendChild(image);

                });


        //submit the data ..

        document.getElementById("submit").onclick = function () {
            WinJS.xhr({
                url: "http://example.in/main.aspx/",
                type: "post",
                responsetype: "document",
                headers: {
                    "CONTENT-TYPE": "application/x-www-form-urlencoded",
                    "User-Agent": "Mozilla/5.0 (Windows NT 6.3; Win64; x64; Trident/7.0; MSAppHost/2.0; rv:11.0) like Gecko",
                     "CONNECTION" : "keep-alive",
                    },
                data: alldata       // all data contain username,password and captcha(entered by user) 

            }).then(
                   function complete(xhr) {
                           document.getElementById("response").innerHTML = toStaticHTML(xhr.response);
                     });

我知道在这里我必须处理 cookie ,但真的不知道该怎么做.

I know that here i have to handle cookie , but really no idea how to do that.

推荐答案

WinJS.xhr API 只是 HTML XMLHttpRequest API 的包装器,不幸的是它不能让您获得 cookie 等.

The WinJS.xhr API is just a wrapper for the HTML XMLHttpRequest API, which unfortunately doesn't let you get to cookies and such.

因此您需要使用 Windows.Web.Http.HttpClient API 代替,它允许您访问 cookie 和其他所有内容.

You'll thus need to use the Windows.Web.Http.HttpClient API instead, which does allow you access to cookies and everything else.

Windows.Web.Http 开始登陆页面概述,然后是如何连接到 HTTP 服务器 以获取下一级详细信息.另请参阅 HttpClient 示例,特别是场景 8、9 和 10处理 cookie.

Start with the Windows.Web.Http landing page for an overview, followed by How to connect to an HTTP Server for the next level of details. Refer also to the HttpClient sample, specifically scnearios 8, 9, and 10 that deal with cookies.

我还在我的免费电子书 使用 HTML、CSS 和 JavaScript 编写 Windows 应用商店应用程序,第 2 版.

I also cover the details of this API (and making HTTP requests in general) in Chapter 4 of my free ebook, Programming Windows Store Apps with HTML, CSS, and JavaScript, 2nd Edition.

这应该能让你继续前进.

That should get you going.

这篇关于如何让两个 Winjs.xhr 调用一个以从网站检索 Captca,另一个调用以提交数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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