Javascript 如何调用两个Winjs.xhr,一个用于从网站检索Captca,另一个用于提交数据
我正在使用HTML5/JAVASCRIPT制作一个windows应用程序。实际上,我想要的是从网站检索验证码,然后将验证码和其他表单字段一起提交到website.aspx并获得回复。我认为我必须为此处理cookies,但我不知道如何做到这一点。 向对此感兴趣的人致敬100次。 这就是我所做的Javascript 如何调用两个Winjs.xhr,一个用于从网站检索Captca,另一个用于提交数据,javascript,windows,html,winjs,Javascript,Windows,Html,Winjs,我正在使用HTML5/JAVASCRIPT制作一个windows应用程序。实际上,我想要的是从网站检索验证码,然后将验证码和其他表单字段一起提交到website.aspx并获得回复。我认为我必须为此处理cookies,但我不知道如何做到这一点。 向对此感兴趣的人致敬100次。 这就是我所做的 // Retrieving the Captcha. WinJS.xhr({ url: "http://example.i
// 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,但真的不知道怎么做。WinJS.xhr API只是HTML XMLHttpRequest API的包装,不幸的是,它不允许您访问cookie等 因此,您需要使用API,它允许您访问Cookie和其他所有内容 从开始查看概述,然后查看下一级详细信息。另请参阅,特别是处理cookies的scnearios 8、9和10 在我的免费电子书的第4章中,我还介绍了这个API的详细信息以及一般的HTTP请求
这应该可以让你走了。@Kraig-你好……我很高兴在这里见到你。实际上,我想针对所有平台开发我的应用程序。不幸的是,Windows.Web.Http.HttpClient无法在这里工作。你可能需要为此领域编写一些特定于平台的代码。XMLHttpRequest-see for generic docs不提供对Cookie的太多控制,这就是为什么在Windows上添加HttpClient API的原因之一。如果您的目标是跨平台,您可以将Apache Cordova作为一个工具集,它可能有一些解决此类问题的解决方案,但是我对Cordova还不太熟悉,所以我不知道。@kraig Brockschmidt-Howdy…最后一个问题..有没有办法在xhr调用中绑定会话。我的意思是说,如果我们用ASP.NET_sessionID绑定上面代码中的两个winjs.xhr调用,那么我们就可以实现我们想要的。。。。。我不知道,但我相信这是你在服务器端看到的东西。WinJS.xhr只是在下面使用XMLHttpRequest,如果有会话cookie,我想它会把它捡起来。然而,我没有直接处理这个问题,所以我可能是错的。因此,我建议您调查会话和XMLHttpRequest,这将为您提供一些见解。