How to make two Winjs.xhr call one for retrieving the Captca from a website and another to submit data back

和自甴很熟 提交于 2019-12-25 06:05:05

问题


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);
                     });

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


回答1:


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

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

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.

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.



来源:https://stackoverflow.com/questions/25613012/how-to-make-two-winjs-xhr-call-one-for-retrieving-the-captca-from-a-website-and

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!