Handling requests with cookies, redirect and other request related things in dart

62 Views Asked by At

I was completed building my webscraper in python and was sucessful in it. Here's my code to achieve this:

import asyncio
import aiohttp
from vtop_handler.constants import HEADERS

VTOP_BASE_URL = r"https://vtop2.vitap.ac.in/vtop/"
VTOP_LOGIN_URL = r"https://vtop2.vitap.ac.in/vtop/vtopLogin"

async def main():
    async with aiohttp.ClientSession() as sess:
        await sess.get(VTOP_BASE_URL,headers = HEADERS)
        async with sess.post(VTOP_LOGIN_URL, headers = HEADERS) as resp:
            login_html = await resp.text()
            print(login_html)
if __name__ == '__main__':
    asyncio.run(main())

The output I get from the fowlling code is as follows: Login page with captcha

While this is the case with python I tried a similar thing with dart,

import 'dart:io';
import 'package:requests/requests.dart';

String baseUrlString = "https://vtop2.vitap.ac.in/vtop";
String redirectUrlString = "$baseUrlString/initialProcess";
String getLoginFromString = "$baseUrlString/vtopLogin";

String hostName = Requests.getHostname(baseUrlString);

void main() async {
  var resp = await Requests.get(baseUrlString,
      verify: false, persistCookies: true);
   
  // getting the cookie to store and request with it
  var jsessIdFull = getJSessionId(headers: resp.headers)!; // This is of the form JSESSIONID='AKLM..."
  var jsessId = jsessIdFull.split('=')[1]; // jessId = 'AKLM...'
  print("JSESSIONID : $jsessId");

  await Requests.addCookie(hostName, "JSESSIONID", jsessId);
  await Requests.addCookie(hostName, "loginUserType", "vtopuser");

  // sending the redirect-request
  await sendRequest1();

  // sending the actual request to get the login page
  await sendRequest2();
}

String? getJSessionId({required Map<String, String> headers}) {
  var cookies = headers['set-cookie'];
  var jsessionid = cookies?.split(';')[0];
  return jsessionid;
}

Future<void> sendRequest1() async {
  var resp1 = await Requests.get(redirectUrlString,
      verify: false, persistCookies: true);

  var jsessIdFull = getJSessionId(headers: resp1.headers);

  if (jsessIdFull == null) {
    print("JSESSIONID not found in response");
    return;
  }

  var jsessId = jsessIdFull.split('=')[1];
  print("JSESSIONID from redirect : $jsessId");

  // clear all the previous cookiess and store new ones
  await Requests.clearStoredCookies(hostName);
  await Requests.addCookie(
      Requests.getHostname(baseUrlString), "JSESSIONID", jsessId);
}

bool isLoggedIn(String content) {
  return !content.contains(
      "You are logged out due to inactivity for more than 15 minutes");
}

Future<void> sendRequest2() async {
  var resp2 = await Requests.get(getLoginFromString);
  if (isLoggedIn(resp2.content())) {
    print("Logged In ");
    return;
  }
  print(resp2.content());
}


output: Session Timedout

I have tried different packages being doubtful of my handling of cookies the packages I have tried are

  • dio
  • http

I think the issue when the baseUrlString is called. I have manually copied the cookie from the browser and checked it with an http client it works. I want to know what I did wrong and why doesn't my dart code work, and any possible solutions to fix it.

0

There are 0 best solutions below