如何使用用户名和密码发出Http请求
问题描述:
我试图向一个网站提出Http请求,假设是gmail.com。一旦gmail打开,需要提供用户名,密码。然后我需要获取数据并在我的应用程序中使用它。我能用Java做,但我的项目在MFC中,我使用DHtmalDialog来使用javascript。这是我现在编写的代码。请帮帮我,我是javascript的新手。
//这里调用函数
httpGet(gmail.com); // gmail就是例如
Hi,
I am trying to make Http request to one site suppose "gmail.com". Once gmail opens need to provide username ,password. then I need to get data and use it in my application. I am able to do it in Java, but my project is in MFC and I am using DHtmalDialog to use javascript. here is my code what I have wrote upto now. Please help me I am new in javascript.
// Here calling the function
httpGet("gmail.com"); // gmail is just for example
function httpGet(theUrl)
{
var xmlHttp = null;
xmlHttp = new XMLHttpRequest();
xmlHttp.open("POST", theUrl, false);
xmlHttp.setRequestHeader("username", "invalid");
xmlHttp.setRequestHeader("password", "invalid;);
xmlHttp.setRequestHeader("Content-Type", "application/json");
xmlHttp.setRequestHeader("Accept", "application/json");
xmlHttp.setRequestHeader("Accept-Encoding", "gzip,deflate");
xmlHttp.send();
alert(xmlHttp.readyState); // here I am getting 4
alert(xmlHttp.status); // here I am getting 200
alert(xmlHttp.responseText); // but here it is blank
// xmlHttp.responseType = "json";
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
alert(xmlHttp.responseText); // here also it is blank
}
else {
alert("fail to get status");
}
return xmlHttp.responseText;
}
答
在这里阅读,
http://eloquentjavascript.net/1st_edition/chapter14.html [ ^ ]
http://stackoverflow.com/questions/220231/accessing-the-web-pages-http-headers -in-javascript [ ^ ]
它会对你有帮助。
Read here,
http://eloquentjavascript.net/1st_edition/chapter14.html[^]
http://stackoverflow.com/questions/220231/accessing-the-web-pages-http-headers-in-javascript[^]
It will help you.