2

A Golang web scraper needs to extract information from a webpage that is NTLM-authenticated.

Having a valid username & password, how can the web scraper perform the NTLM 4-way handshake with the server in order to gain access to the protected webpage behind?

url, username, password := "http://www.some-website.com", "admin", "12345"

client := &http.Client{}
req, _ := http.NewRequest("GET", url, nil)
req.Header.Set("Authorization", "NTLM")
res, _ := client.Do(req)

1 Answer 1

4

You can use a package like Azure/go-ntlmssp to authenticate before you start scraping.

url, username, password := "http://www.some-website.com", "admin", "12345"

client := &http.Client{
    Transport: ntlmssp.Negotiator{
        RoundTripper:&http.Transport{},
    },
}

req, _ := http.NewRequest("GET", url, nil)
req.SetBasicAuth(username, password)

res, _ := client.Do(req)
5
  • @John i have tried the same code but it did not work for me, now here the thing is sometimes the authentication happens and sometimes not, please suggest Commented Jun 21, 2018 at 13:59
  • @VijayKumar I have not provided error handling in the example code. Try checking the errors to see what's wrong. Commented Jun 26, 2018 at 6:18
  • @JohnSPerayil error says 401 unauthorized, its an https domain. Do we need to modify anything for an https domain Commented Jun 28, 2018 at 10:56
  • 1
    @VijayKumar HTTPS shouldn't be a problem, besides the server is returning Unauthorised. But would need more details to debug the issue since you said it happens only sometimes. This comment box is not the right place to discuss it are so create a question on SO with more details or create a room on chat.stackoverflow.com and invite me so that we can discuss it. Commented Jun 29, 2018 at 5:34
  • @JohnSPerayil i have created a group and invited you there, please join in Commented Jun 29, 2018 at 9:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.