# Ntlm example

**URL:** <https://www.ruby-forum.com/t/ntlm-example/88364>\
**Category:** Ruby\
**Created:** [March 10, 2007, 2:35am UTC](https://www.ruby-forum.com/t/ntlm-example/88364 "2007-03-10T02:35:42Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sean\_nakasone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9a140/32.png) [@sean\_nakasone](https://www.ruby-forum.com/u/sean_nakasone)\
**Post date:** [March 10, 2007, 2:35am UTC](https://www.ruby-forum.com/t/ntlm-example/88364/1 "2007-03-10T02:35:42Z")

</div>

Can someone please provide me with a simple ntlm example?

Basically, I just want to make an HTTP GET request to my web site, but  
it  
does not work because it requires NTLM authentication. Here’s what I’m  
doing, but I need NTLM incorporated.

require ‘socket’  
client = TCPSocket.open(‘amfmweb’, 80)  
client.send(“GET /gviewer HTTP/1.0\n\n”, 0) # 0 means standard packet  
client.shutdown( 1 )  
response=client.readlines  
puts response  
client.close

thanks,  
Sean.

---

<div class="post-metadata">

**Author:** ![sean\_nakasone](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e9a140/32.png) [@sean\_nakasone](https://www.ruby-forum.com/u/sean_nakasone)\
**Post date:** [March 13, 2007, 1:32am UTC](https://www.ruby-forum.com/t/ntlm-example/88364/2 "2007-03-13T01:32:51Z")

</div>

On 3/9/07, sean nakasone [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Can someone please provide me with a simple ntlm example?

The following chunk of code works in my environment: IIS 6.0, ruby  
1.8.5 [i386-cygwin], and non-standard rubyntlm 0.1.1.

[http://rubyforge.org/projects/rubyntlm/](http://rubyforge.org/projects/rubyntlm/)

* * *

require ‘socket’  
require ‘net/ntlm’

$user = “koheik”  
$passwd = “password”

$host = “www”  
$port = 80

def header(s, host, msg)  
s.print “GET / HTTP/1.1\r\n”  
s.print “Host: #{host}\r\n”  
s.print "Authorization: NTLM " + msg + “\r\n”  
s.print “\r\n”  
end

client = TCPSocket.new($host, $port)

# client → server

t1 = Net::NTLM::Message::Type1.new()  
header(client, $host, t1.encode64)

# server → client

msg = ‘’  
while(line = client.gets)  
if /^WWW-Authenticate: (NTLM|Negotiate) (.+)\r\n/ =~ line  
msg = $2  
end  
break if /^\r\n/ =~ line  
end

if(msg)  
t2 = Net::NTLM::Message.decode64(msg)  
t3 = t2.response({:user =\> $user, :password =\> $passwd}, {:ntlmv2 =\>  
true})  
header(client, $host, t3.encode64)  
end

## client.shutdown(1) response=client.readlines puts response client.close

Thanks,  
Kohei
