0

I am trying using JSoup to login to a website that uses Microsoft-IIS/7.5 server powered by ASP.NET

Whenever I open the website it opens this Login prompt

JSoup throws org.jsoup.HttpStatusException: HTTP error fetching URL. Status=401 when establishing a connection. I know nothing about web servers or how this login process goes I am just a Java developer and I encountered this issue when I tried to build an app that tries download some data from this website.

The answers here using basic authentication did not solve my problem and it keeps sending the same exception.

  • What authentication are you using? basic authentication or windows authentcation? Both of them are challenge base authentication, so when you access the website without authorization header, IIS just return 401 error and ask for credential. In c#, we use either network credential or defaultcredential to pass user credential to get authentication ticket. You may need to do same thing with java. – Jokies Ding Apr 20 '20 at 07:03
  • @JokiesDing Due to lack of knowledge, I do not know what type of authentication is used or even what that is supposed to mean. All what I know is that Microsoft-IIS/7.5 server is used which is powered by ASP.NET and this site is an intranet. Can't we infer the type of authentication from that? I tried basic authentication as described in another post but it didn't work. – Bishoy Essam Apr 23 '20 at 11:35

1 Answers1

0

Some servers refuse serving content for requests without any headers.
Try setting some headers, example values:

Document doc = Jsoup.connect(loginPageUrl)
   .userAgent("Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36")
   .header("Accept", "text/html,application/xhtml+xml text/html,application/xhtml+xml")
   .header("Accept-Language", "en-US")
   .header("Accept-Encoding", "gzip, deflate")
   .header("Referer", loginPageUrl)
   .get();
Krystian G
  • 2,842
  • 3
  • 11
  • 25
  • I tried those headers with basic authentication but didn't work and the same exception was thrown. Thanks for your answer. – Bishoy Essam Apr 23 '20 at 11:38