HTTPS: Difference between revisions

From Rosetta Code
Content added Content deleted
m (omit TI-BASIC)
(add Ruby)
Line 4: Line 4:


Readers may wish to contrast with the [[HTTP Request]] task, and also the task on [[HTTPS request with authentication]].
Readers may wish to contrast with the [[HTTP Request]] task, and also the task on [[HTTPS request with authentication]].

=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
{{libheader|wininet}}
{{libheader|wininet}}
<lang AutoHotkey>
<lang AutoHotkey>URL := "https://sourceforge.net/"
URL := "https://sourceforge.net/"
WININET_Init()
WININET_Init()
msgbox % html := UrlGetContents(URL)
msgbox % html := UrlGetContents(URL)
Line 13: Line 13:
return
return
#include urlgetcontents.ahk
#include urlgetcontents.ahk
#include wininet.ahk
#include wininet.ahk</lang>
</lang>


=={{header|Common Lisp}}==
=={{header|Common Lisp}}==
Line 46: Line 45:
Python's '''urllib2''' ('''urllib.request''' in Python3) has support for SSL if the interpreter's underlying ''httplib'' libraries were compiled with SSL support. By default this will be the enabled for default Python installations on most platforms.
Python's '''urllib2''' ('''urllib.request''' in Python3) has support for SSL if the interpreter's underlying ''httplib'' libraries were compiled with SSL support. By default this will be the enabled for default Python installations on most platforms.


<lang Python>
<lang Python>import urllib2
import urllib2
request = urllib2.urlopen('https://sourceforge.net/')
request = urllib2.urlopen('https://sourceforge.net/')
print request.read()
print request.read()
request.close()
request.close()</lang>
</lang>


=={{header|R}}==
=={{header|R}}==
Line 73: Line 70:
pagetree$children$html
pagetree$children$html
</lang>
</lang>

=={{header|Ruby}}==
<lang ruby>require 'net/https'
require 'uri'
require 'pp'

uri = URI.parse('https://sourceforge.net')
http = Net::HTTP.new(uri.host,uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
content = http.get("/")

p [content.code, content.message]
pp content.to_hash
puts content.body</lang>

outputs
<pre>["302", "Found"]
{"location"=>["http://sourceforge.net/"],
"content-type"=>["text/html; charset=UTF-8"],
"connection"=>["close"],
"server"=>["nginx/0.7.60"],
"date"=>["Sun, 30 Aug 2009 20:20:07 GMT"],
"content-length"=>["229"],
"set-cookie"=>
["sf.consume=89f65c6fadd222338b2f3de6f8e8a17b2c8f67c2gAJ9cQEoVQhfZXhwaXJlc3ECY2RhdGV0aW1lCmRhdGV0aW1lCnEDVQoH9gETAw4HAAAAhVJxBFUDX2lkcQVVIDEyOWI2MmVkOWMwMWYxYWZiYzE5Y2JhYzcwZDMxYTE4cQZVDl9hY2Nlc3NlZF90aW1lcQdHQdKmt73UN21VDl9jcmVhdGlvbl90aW1lcQhHQdKmt73UN2V1Lg==; expires=Tue, 19-Jan-2038 03:14:07 GMT; Path=/"]}
<html>
<head>
<title>302 Found</title>
</head>
<body>
<h1>302 Found</h1>
The resource was found at <a href="http://sourceforge.net/">http://sourceforge.net/</a>;
you should be redirected automatically.


</body>
</html></pre>


=={{header|Tcl}}==
=={{header|Tcl}}==

Revision as of 20:24, 30 August 2009

Task
HTTPS
You are encouraged to solve this task according to the task description, using any language you may know.

Print an HTTPS URL's content to the console. Checking the host certificate for validity is recommended. The client should not authenticate itself to the server — the webpage https://sourceforge.net/ supports that access policy — as that is the subject of other tasks.

Readers may wish to contrast with the HTTP Request task, and also the task on HTTPS request with authentication.

AutoHotkey

Library: wininet

<lang AutoHotkey>URL  := "https://sourceforge.net/" WININET_Init() msgbox % html := UrlGetContents(URL) WININET_UnInit() return

  1. include urlgetcontents.ahk
  2. include wininet.ahk</lang>

Common Lisp

Library: DRAKMA

First grabbing the entire body as a string, and then by pulling from a stream. This is the same code as in HTTP Request; drakma:http-request supports SSL.

<lang lisp>(defun wget-drakma-string (url &optional (out *standard-output*))

 "Grab the body as a string, and write it to out."
 (write-string (drakma:http-request url) out))

(defun wget-drakma-stream (url &optional (out *standard-output*))

 "Grab the body as a stream, and write it to out."
 (loop with body = (drakma:http-request url :want-stream t)
       for line = (read-line body nil nil)
       while line do (write-line line)
       finally (close body)))
Use

(wget-drakma-stream "https://sourceforge.net") </lang>

F#

The underlying .NET classes handle secure web connections the same way they manage insecure connections.

#light
let wget (url : string) =
    let c = new System.Net.WebClient()
    c.DownloadString(url)

Python

Python's urllib2 (urllib.request in Python3) has support for SSL if the interpreter's underlying httplib libraries were compiled with SSL support. By default this will be the enabled for default Python installations on most platforms.

<lang Python>import urllib2 request = urllib2.urlopen('https://sourceforge.net/') print request.read() request.close()</lang>

R

Library: RCurl
Library: XML

The basic idea is to use getURL (as with HTTP_Request), but with some extra parameters. <lang R> library(RCurl) webpage <- getURL("https://sourceforge.net/", .opts=list(followlocation=TRUE, ssl.verifyhost=FALSE, ssl.verifypeer=FALSE)) </lang> In this case, the webpage output contains unprocessed characters, e.g. \" instead of " and \\ instead of \, so we need to process the markup. <lang R> wp <- readLines(tc <- textConnection(webpage)) close(tc) </lang> Finally, we parse the HTML and find the interesting bit. <lang R> pagetree <- htmlTreeParse(wp) pagetree$children$html </lang>

Ruby

<lang ruby>require 'net/https' require 'uri' require 'pp'

uri = URI.parse('https://sourceforge.net') http = Net::HTTP.new(uri.host,uri.port) http.use_ssl = true http.verify_mode = OpenSSL::SSL::VERIFY_NONE content = http.get("/")

p [content.code, content.message] pp content.to_hash puts content.body</lang>

outputs

["302", "Found"]
{"location"=>["http://sourceforge.net/"],
 "content-type"=>["text/html; charset=UTF-8"],
 "connection"=>["close"],
 "server"=>["nginx/0.7.60"],
 "date"=>["Sun, 30 Aug 2009 20:20:07 GMT"],
 "content-length"=>["229"],
 "set-cookie"=>
  ["sf.consume=89f65c6fadd222338b2f3de6f8e8a17b2c8f67c2gAJ9cQEoVQhfZXhwaXJlc3ECY2RhdGV0aW1lCmRhdGV0aW1lCnEDVQoH9gETAw4HAAAAhVJxBFUDX2lkcQVVIDEyOWI2MmVkOWMwMWYxYWZiYzE5Y2JhYzcwZDMxYTE4cQZVDl9hY2Nlc3NlZF90aW1lcQdHQdKmt73UN21VDl9jcmVhdGlvbl90aW1lcQhHQdKmt73UN2V1Lg==; expires=Tue, 19-Jan-2038 03:14:07 GMT; Path=/"]}
<html>
 <head>
  <title>302 Found</title>
 </head>
 <body>
  <h1>302 Found</h1>
  The resource was found at <a href="http://sourceforge.net/">http://sourceforge.net/</a>;
you should be redirected automatically.


 </body>
</html>

Tcl

Uses the Tls package. <lang tcl>package require http package require tls http::register https 443 ::tls::socket

  1. Make a secure connection

set token [http::geturl https://sourceforge.net/]

  1. Now as for conventional use of the “http” package

puts [http::data $token] http::cleanup $token</lang>