简体   繁体   中英

javascript : Login to a website using XMLHttpRequest

im trying to write a code in javascript that enables me to login to a website . when i first sends the http ( get request ) to get the login web-page the website response correctly to my request which give me a cookie (before log-in). now i want to send username and password in another POST request along with the cookie to get the login authentication . the problem is when im sending that request just the same as the webpage source code does it will raise an error ( xhr.status is 0 ) . whats possibly wrong with that ?

here is my code :

var url = "https://sess.shirazu.ac.ir/sess/Start.aspx";
var username = "myUsername";
var password = "myPassword";
var xhr;
login: function(){


      xhr = new XMLHttpRequest();
      xhr.withCredentials = true;
      xhr.onreadystatechange = (e) => { 
          if (xhr.readyState !== 4) {
            return;
          }

          if (xhr.status === 200) {

            var pageSource =  xhr.responseText; //gets the response of the request of the login page
//here is where im hashing the password with the given key in the retrieved page-source
            var parser = new DOMParser();
            var doc = parser.parseFromString(pageSource, "text/xml");   
            var RKeyElement = String(doc.getElementById("_RKey"));
            var RKey = RKeyElement.substring(RKeyElement.search("value"));  
            RKey = RKey.substring(RKey.search("\"")+1, RKey.lastIndexOf("\"")); //getting the 32-digit-long _RKey

            DoLogin(username, password , '', RKey);
          }
          else {
            Alert.alert("error");
          }
        };

      xhr.open('GET', url, true);
      xhr.send(null);

  },
}

the code above works good the problem is with this DoLogin() function:

function DoLogin(iId, iPass, iCode, RKey) {
    var Inc = Md5High(RKey, iPass);    //this works fine (Hashing the password)
    var Request = xhr;
    Request.onreadystatechange = (e) => {
      if (Request.readyState !== 4) {
        return;
      }

      if (Request.status === 200) {
        console.log('success', Request.responseText);
      }
      else {
        console.log('error');   //status is 0 when this occures
      }
    };

  Request.abort();

  Request.open('post', "https://sess.shirazu.ac.ir/sess/17610358812"+"/sess/Script/AjaxEnvironment.aspx?Act=MakeMember&Id=" + iId + "&Pass=" + Inc + "&Code=" + iCode, true);
  Request.send();
}

i couldn't solve the problem with the POST method . but i figured out that as long as the content of the request is empty there would be no difference in using POST or GET method . so what solved my problem was simply replacing the POST method in the second function with GET and it did work perfectly .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM