HTTPS: Difference between revisions
→{{header|Visual Basic}}: Added support for TLS 1.2 |
m →{{header|Wren}}: Minor tidy |
||
(31 intermediate revisions by 20 users not shown) | |||
Line 1: | Line 1: | ||
{{task|Programming environment operations}} |
{{task|Programming environment operations}} |
||
[[Category:Networking and Web Interaction]] |
[[Category:Networking and Web Interaction]] |
||
;Task: |
;Task: |
||
Send a GET request to obtain the resource located at the URL "https://www.w3.org/", then print it to the console.<br> |
|||
Print an HTTPS URL's content to the console. Checking the host certificate for validity is recommended. |
|||
Checking the host certificate for validity is recommended.<br> |
|||
Do not authenticate. That is the subject of other [[HTTPS request with authentication|tasks]].<br> |
|||
Readers may wish to contrast with the [[HTTP Request]] task, and also the task on [[HTTPS request with authentication]].<br> |
|||
Readers may wish to contrast with the [[HTTP Request]] task, and also the task on [[HTTPS request with authentication]]. |
|||
<br><br> |
|||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
{{libheader|AWS}} |
{{libheader|AWS}} |
||
Exactly the same as the HTTP task, assuming you compiled AWS with openssl support. |
Exactly the same as the HTTP task, assuming you compiled AWS with openssl support. |
||
< |
<syntaxhighlight lang="ada"> |
||
with AWS.Client; |
with AWS.Client; |
||
with AWS.Response; |
with AWS.Response; |
||
Line 22: | Line 18: | ||
URL => "https://sourceforge.net/"))); |
URL => "https://sourceforge.net/"))); |
||
end GetHttps; |
end GetHttps; |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Arturo}}== |
|||
<syntaxhighlight lang="rebol">print read "https://www.w3.org/"</syntaxhighlight> |
|||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
{{libheader|wininet}} |
{{libheader|wininet}} |
||
<syntaxhighlight lang="autohotkey"> |
|||
<lang AutoHotkey> |
|||
URL := "https://sourceforge.net/" |
URL := "https://sourceforge.net/" |
||
WININET_Init() |
WININET_Init() |
||
Line 34: | Line 34: | ||
#include urlgetcontents.ahk |
#include urlgetcontents.ahk |
||
#include wininet.ahk |
#include wininet.ahk |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|BaCon}}== |
=={{header|BaCon}}== |
||
This code requires BaCon 3.8.2 or later. |
This code requires BaCon 3.8.2 or later. |
||
< |
<syntaxhighlight lang="freebasic">OPTION TLS TRUE |
||
website$ = "www.google.com" |
website$ = "www.google.com" |
||
Line 53: | Line 53: | ||
PRINT REPLACE$(total$, "\r\n[0-9a-fA-F]+\r\n", "\r\n", TRUE) : ' Remove chunk indicators from HTML data |
PRINT REPLACE$(total$, "\r\n[0-9a-fA-F]+\r\n", "\r\n", TRUE) : ' Remove chunk indicators from HTML data |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Batch File}}== |
=={{header|Batch File}}== |
||
< |
<syntaxhighlight lang="batch"> |
||
:: Must have curl.exe |
:: Must have curl.exe |
||
curl.exe -k -s -L https://sourceforge.net/ |
curl.exe -k -s -L https://sourceforge.net/ |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|C}}== |
=={{header|C}}== |
||
{{libheader|libcurl}} |
{{libheader|libcurl}} |
||
<syntaxhighlight lang="c">#include <stdio.h> |
|||
<lang c> |
|||
#include <stdio.h> |
|||
#include <stdlib.h> |
#include <stdlib.h> |
||
#include <curl/curl.h> |
#include <curl/curl.h> |
||
CURL *curl; |
|||
char buffer[CURL_ERROR_SIZE]; |
|||
int |
|||
main(void) |
int main(void) { |
||
if ((curl = curl_easy_init()) != NULL) { |
|||
{ |
|||
curl_easy_setopt(curl, CURLOPT_URL, "https://sourceforge.net/"); |
|||
CURL *curl; |
|||
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1); |
|||
char buffer[CURL_ERROR_SIZE]; |
|||
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, buffer); |
|||
if ((curl |
if (curl_easy_perform(curl) != CURLE_OK) { |
||
fprintf(stderr, "%s\n", buffer); |
|||
return EXIT_FAILURE; |
|||
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1); |
|||
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, buffer); |
|||
if (curl_easy_perform(curl) != CURLE_OK) { |
|||
fprintf(stderr, "%s\n", buffer); |
|||
return EXIT_FAILURE; |
|||
} |
|||
curl_easy_cleanup(curl); |
|||
} |
} |
||
curl_easy_cleanup(curl); |
|||
} |
|||
} |
|||
return EXIT_SUCCESS; |
|||
</lang> |
|||
}</syntaxhighlight> |
|||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
{{works with|C sharp|3.0}} |
{{works with|C sharp|3.0}} |
||
< |
<syntaxhighlight lang="csharp"> |
||
using System; |
using System; |
||
using System.Net; |
using System.Net; |
||
Line 105: | Line 99: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
This does not work for urls requiring a secure (SSL) connection. |
This does not work for urls requiring a secure (SSL) connection. |
||
Line 112: | Line 106: | ||
Using the duck-streams as a convenient wrapper for Java's networking classes, grabbing the contents of an HTTPS URL is as easy as: |
Using the duck-streams as a convenient wrapper for Java's networking classes, grabbing the contents of an HTTPS URL is as easy as: |
||
< |
<syntaxhighlight lang="clojure"> |
||
(use '[clojure.contrib.duck-streams :only (slurp*)]) |
(use '[clojure.contrib.duck-streams :only (slurp*)]) |
||
(print (slurp* "https://sourceforge.net")) |
(print (slurp* "https://sourceforge.net")) |
||
</syntaxhighlight> |
|||
</lang> |
|||
The usual Java mechanisms can be used to manage acceptance of SSL certificates if required. |
The usual Java mechanisms can be used to manage acceptance of SSL certificates if required. |
||
{{works with|Clojure|1.2}} |
{{works with|Clojure|1.2}} |
||
< |
<syntaxhighlight lang="clojure"> |
||
(print (slurp "https://sourceforge.net")) |
(print (slurp "https://sourceforge.net")) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|D}}== |
|||
Using curl |
|||
<syntaxhighlight lang="d"> |
|||
import std.stdio; |
|||
import std.net.curl; |
|||
auto data = get("https://sourceforge.net"); |
|||
writeln(data); |
|||
</syntaxhighlight> |
|||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
Line 129: | Line 133: | ||
First grabbing the entire body as a string, and then by pulling from a stream. This is the same code as in [[HTTP Request]]; <code>drakma:http-request</code> supports SSL. |
First grabbing the entire body as a string, and then by pulling from a stream. This is the same code as in [[HTTP Request]]; <code>drakma:http-request</code> supports SSL. |
||
< |
<syntaxhighlight lang="lisp"> |
||
(defun wget-drakma-string (url &optional (out *standard-output*)) |
(defun wget-drakma-string (url &optional (out *standard-output*)) |
||
"Grab the body as a string, and write it to out." |
"Grab the body as a string, and write it to out." |
||
Line 143: | Line 147: | ||
;; Use |
;; Use |
||
(wget-drakma-stream "https://sourceforge.net") |
(wget-drakma-stream "https://sourceforge.net") |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{libheader|Dexador}} |
|||
<syntaxhighlight lang="lisp"> |
|||
(format t "~a~%" (nth-value 0 (dex:get "https://www.w3.org/"))) |
|||
</syntaxhighlight> |
|||
=={{header|Delphi}}== |
=={{header|Delphi}}== |
||
{{libheader|OpenSSL}} |
{{libheader|OpenSSL}} |
||
<syntaxhighlight lang="delphi"> |
|||
<lang Delphi> |
|||
program ShowHTTPS; |
program ShowHTTPS; |
||
Line 168: | Line 178: | ||
end; |
end; |
||
end. |
end. |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|EchoLisp}}== |
=={{header|EchoLisp}}== |
||
'''file->string''' usage: the server must allow cross-domain access, or a browser add-on like cors-everywhere must be installed to bypass cross-domain checking. |
'''file->string''' usage: the server must allow cross-domain access, or a browser add-on like cors-everywhere must be installed to bypass cross-domain checking. |
||
< |
<syntaxhighlight lang="scheme"> |
||
;; asynchronous call back definition |
;; asynchronous call back definition |
||
(define (success name text) (writeln 'Loaded name) (writeln text)) |
(define (success name text) (writeln 'Loaded name) (writeln text)) |
||
;; |
;; |
||
(file->string success "https:/sourceforge.net") |
(file->string success "https:/sourceforge.net") |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Erlang}}== |
=={{header|Erlang}}== |
||
===Synchronous=== |
===Synchronous=== |
||
< |
<syntaxhighlight lang="erlang"> |
||
-module(main). |
-module(main). |
||
-export([main/1]). |
-export([main/1]). |
||
Line 192: | Line 202: | ||
{error, Res} -> io:fwrite("~p~n", [Res]) |
{error, Res} -> io:fwrite("~p~n", [Res]) |
||
end. |
end. |
||
</syntaxhighlight> |
|||
</lang> |
|||
===Asynchronous=== |
===Asynchronous=== |
||
< |
<syntaxhighlight lang="erlang"> |
||
-module(main). |
-module(main). |
||
-export([main/1]). |
-export([main/1]). |
||
Line 208: | Line 218: | ||
after 10000 -> io:fwrite("Timed out.~n",[]) |
after 10000 -> io:fwrite("Timed out.~n",[]) |
||
end. |
end. |
||
</syntaxhighlight> |
|||
</lang> |
|||
Using it |
Using it |
||
< |
<syntaxhighlight lang="erlang"> |
||
|escript ./req.erl https://sourceforge.net/ |
|escript ./req.erl https://sourceforge.net/ |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Fortran}}== |
|||
There is no such network library for communicating with a HTTPS server in fortran. Use appropriate tools (eg. simple node.js snippet) |
|||
<lang fortran> |
|||
program https_example |
|||
implicit none |
|||
character (len=:), allocatable :: code |
|||
character (len=:), allocatable :: command |
|||
logical:: waitForProcess |
|||
! execute Node.js code |
|||
code = "var https = require('https'); & |
|||
https.get('https://sourceforge.net/', function(res) {& |
|||
console.log('statusCode: ', res.statusCode);& |
|||
console.log('Is authorized:' + res.socket.authorized);& |
|||
console.log(res.socket.getPeerCertificate());& |
|||
res.on('data', function(d) {process.stdout.write(d);});});" |
|||
command = 'node -e "' // code // '"' |
|||
call execute_command_line (command, wait=waitForProcess) |
|||
end program https_example |
|||
</lang> |
|||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
The underlying .NET classes handle secure web connections the same way they manage insecure connections. |
The underlying .NET classes handle secure web connections the same way they manage insecure connections. |
||
< |
<syntaxhighlight lang="fsharp"> |
||
#light |
#light |
||
let wget (url : string) = |
let wget (url : string) = |
||
let c = new System.Net.WebClient() |
let c = new System.Net.WebClient() |
||
c.DownloadString(url) |
c.DownloadString(url) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Frink}}== |
=={{header|Frink}}== |
||
<syntaxhighlight lang="frink">print[read["https://sourceforge.net/"]]</syntaxhighlight> |
|||
<lang Frink> |
|||
print[read["https://sourceforge.net/"] |
|||
=={{header|FutureBasic}}== |
|||
</lang> |
|||
<syntaxhighlight lang="futurebasic"> |
|||
include "NSLog.incl" |
|||
local fn GET_HTTPS |
|||
CFStringRef response = unix @"curl -ksL https://sourceforge.net/" |
|||
CFDataRef dta = fn StringData( response, NSUTF8StringEncoding ) |
|||
CFDictionaryRef options = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)} |
|||
CFAttributedStringRef aStr = fn AttributedStringWithHTML( dta, options ) |
|||
NSLog( @"%@", aStr ) |
|||
end fn |
|||
fn GET_HTTPS |
|||
HandleEvents |
|||
</syntaxhighlight> |
|||
{{output}} |
|||
<pre> |
|||
We're sorry -- the Sourceforge site is currently in Disaster Recovery mode. Please check back later. { |
|||
NSColor = "sRGB IEC61966-2.1 colorspace 0 0 0 1"; |
|||
NSFont = "\"Times-Roman 12.00 pt. P [] (0x7f8f94e11ce0) fobj=0x7f8f94e0fda0, spc=3.00\""; |
|||
NSKern = 0; |
|||
NSParagraphStyle = "Alignment 4, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (\n), DefaultTabInterval 36, Blocks (\n), Lists (\n), BaseWritingDirection 0, HyphenationFactor 0, TighteningForTruncation YES, HeaderLevel 0 LineBreakStrategy 0"; |
|||
NSStrokeColor = "sRGB IEC61966-2.1 colorspace 0 0 0 1"; |
|||
NSStrokeWidth = 0; |
|||
} |
|||
</pre> |
|||
=={{header|Go}}== |
=={{header|Go}}== |
||
<syntaxhighlight lang="go"> |
|||
<lang go> |
|||
package main |
package main |
||
Line 269: | Line 283: | ||
io.Copy(os.Stdout, r.Body) |
io.Copy(os.Stdout, r.Body) |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Groovy}}== |
=={{header|Groovy}}== |
||
< |
<syntaxhighlight lang="groovy"> |
||
new URL("https://sourceforge.net").eachLine { println it } |
new URL("https://sourceforge.net").eachLine { println it } |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
Line 282: | Line 296: | ||
This is just the example from [http://hackage.haskell.org/packages/archive/http-conduit/1.8.5.1/doc/html/Network-HTTP-Conduit.html Network.HTTP.Conduit], with the http URL replaced with an https one, since [http://hackage.haskell.org/package/http-conduit http-conduit] natively supports https without needing any additional work. |
This is just the example from [http://hackage.haskell.org/packages/archive/http-conduit/1.8.5.1/doc/html/Network-HTTP-Conduit.html Network.HTTP.Conduit], with the http URL replaced with an https one, since [http://hackage.haskell.org/package/http-conduit http-conduit] natively supports https without needing any additional work. |
||
< |
<syntaxhighlight lang="haskell">#!/usr/bin/runhaskell |
||
import Network.HTTP.Conduit |
import Network.HTTP.Conduit |
||
Line 289: | Line 303: | ||
main = withSocketsDo |
main = withSocketsDo |
||
$ simpleHttp "https://sourceforge.net/" >>= L.putStr</ |
$ simpleHttp "https://sourceforge.net/" >>= L.putStr</syntaxhighlight> |
||
==Icon and {{header|Unicon}}== |
==Icon and {{header|Unicon}}== |
||
< |
<syntaxhighlight lang="unicon"># Requires Unicon version 13 |
||
procedure main(arglist) |
procedure main(arglist) |
||
url := (\arglist[1] | "https://sourceforge.net/") |
url := (\arglist[1] | "https://sourceforge.net/") |
||
Line 298: | Line 312: | ||
while write(read(w)) |
while write(read(w)) |
||
close(w) |
close(w) |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 308: | Line 322: | ||
=={{header|Ioke}}== |
=={{header|Ioke}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang="ioke"> |
||
connection = URL new("https://sourceforge.net") openConnection |
connection = URL new("https://sourceforge.net") openConnection |
||
scanner = Scanner new(connection getInputStream) |
scanner = Scanner new(connection getInputStream) |
||
Line 315: | Line 329: | ||
scanner next println |
scanner next println |
||
) |
) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|J}}== |
=={{header|J}}== |
||
Using <tt>gethttp</tt> from [[Web Scraping#J|Web Scraping]] |
Using <tt>gethttp</tt> from [[Web Scraping#J|Web Scraping]] |
||
<syntaxhighlight lang="j"> |
|||
<lang j> |
|||
#page=: gethttp'https://sourceforge.net' |
#page=: gethttp'https://sourceforge.net' |
||
0 |
0 |
||
#page=: '--no-check-certificate' gethttp'https://sourceforge.net' |
#page=: '--no-check-certificate' gethttp'https://sourceforge.net' |
||
900 |
900 |
||
</syntaxhighlight> |
|||
</lang> |
|||
(We can not load the example page using https unless we disable certificate checking. The numbers are the number of characters retrieved.) |
(We can not load the example page using https unless we disable certificate checking. The numbers are the number of characters retrieved.) |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
=== javax.net.ssl === |
|||
Additional certificate information is available through the [http://java.sun.com/javase/6/docs/api/javax/net/ssl/HttpsURLConnection.html javax.net.ssl.HttpsURLConnection] interface. |
Additional certificate information is available through the [http://java.sun.com/javase/6/docs/api/javax/net/ssl/HttpsURLConnection.html javax.net.ssl.HttpsURLConnection] interface. |
||
<syntaxhighlight lang="java"> |
|||
<lang Java> |
|||
URL url = new URL("https://sourceforge.net"); |
URL url = new URL("https://sourceforge.net"); |
||
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); |
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); |
||
Line 339: | Line 354: | ||
System.out.println(scanner.next()); |
System.out.println(scanner.next()); |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=== java.net.http === |
|||
=={{header|JavaScript}}== |
|||
Using the standard [http://openjdk.java.net/groups/net/httpclient/ Java 11 HTTP Client] |
|||
=== Modern Browsers === |
|||
<lang JavaScript> |
|||
fetch("https://sourceforge.net") |
|||
.then(function(response) { |
|||
return response.text(); |
|||
}) |
|||
.then(function(content) { |
|||
console.log(content) |
|||
}) |
|||
.catch(function (err){ |
|||
console.error(err) |
|||
}); |
|||
</lang> |
|||
<syntaxhighlight lang="java">import java.net.URI; |
|||
=== Node.js === |
|||
import java.net.http.HttpClient; |
|||
<lang JavaScript> |
|||
import java.net.http.HttpRequest; |
|||
const https = require('https'); |
|||
import java.net.http.HttpResponse; |
|||
import java.nio.charset.Charset; |
|||
public class Main { |
|||
https.get("https://sourceforge.net", (resp) => { |
|||
public static void main(String[] args) { |
|||
let content = ''; |
|||
var request = HttpRequest.newBuilder(URI.create("https://sourceforge.net")) |
|||
.GET() |
|||
.build(); |
|||
HttpClient.newHttpClient() |
|||
// A chunk of data has been recieved. |
|||
.sendAsync(request, HttpResponse.BodyHandlers.ofString(Charset.defaultCharset())) |
|||
resp.on('data', (chunk) => { |
|||
.thenApply(HttpResponse::body) |
|||
content += chunk; |
|||
.thenAccept(System.out::println) |
|||
}); |
|||
.join(); |
|||
} |
|||
}</syntaxhighlight> |
|||
=={{header|JavaScript}}== |
|||
// The whole response has been received. Print out the result. |
|||
=== Browser === |
|||
resp.on('end', () => { |
|||
Using fetch API and async/await: |
|||
console.log(content); |
|||
<syntaxhighlight lang="javascript"> |
|||
}); |
|||
const response = await fetch('https://rosettacode.org'); |
|||
const text = await response.text(); |
|||
console.log(text); |
|||
</syntaxhighlight> |
|||
}).on("error", (err) => { |
|||
console.error("Error: " + err.message); |
|||
<syntaxhighlight lang="javascript">fetch("https://sourceforge.net").then(function (response) { |
|||
}); |
|||
return response.text(); |
|||
</lang> |
|||
}).then(function (body) { |
|||
return body; |
|||
});</syntaxhighlight> |
|||
=== Node.js === |
|||
<syntaxhighlight lang="javascript">require("https").get("https://sourceforge.net", function (resp) { |
|||
let body = ""; |
|||
resp.on("data", function (chunk) { |
|||
body += chunk; |
|||
}); |
|||
resp.on("end", function () { |
|||
console.log(body); |
|||
}); |
|||
}).on("error", function (err) { |
|||
console.error("Error: " + err.message); |
|||
});</syntaxhighlight> |
|||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
< |
<syntaxhighlight lang="julia"># v0.6.0 |
||
using Requests |
using Requests |
||
str = readstring(get("https://sourceforge.net/"))</ |
str = readstring(get("https://sourceforge.net/"))</syntaxhighlight> |
||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
< |
<syntaxhighlight lang="scala">// version 1.1.2 |
||
import java.net.URL |
import java.net.URL |
||
import javax.net.ssl.HttpsURLConnection |
import javax.net.ssl.HttpsURLConnection |
||
Line 400: | Line 431: | ||
while (sc.hasNextLine()) println(sc.nextLine()) |
while (sc.hasNextLine()) println(sc.nextLine()) |
||
sc.close() |
sc.close() |
||
}</ |
}</syntaxhighlight> |
||
Or simplier, since Kotlin 1.2 |
|||
<syntaxhighlight lang="scala"> |
|||
import java.net.URL |
|||
fun main(args: Array<String>){ |
|||
println(URL("https://sourceforge.net").readText()) |
|||
} |
|||
</syntaxhighlight> |
|||
=={{header|Lasso}}== |
=={{header|Lasso}}== |
||
< |
<syntaxhighlight lang="lasso">local(x = curl('https://sourceforge.net')) |
||
local(y = #x->result) |
local(y = #x->result) |
||
#y->asString</ |
#y->asString</syntaxhighlight> |
||
If a site with an invalid SSL Cert is encountered the curl type throws the following error: |
If a site with an invalid SSL Cert is encountered the curl type throws the following error: |
||
Line 415: | Line 455: | ||
*Windows: |
*Windows: |
||
{{libheader|Curl Xtra}} |
{{libheader|Curl Xtra}} |
||
< |
<syntaxhighlight lang="lingo">ch = xtra("Curl").new() |
||
CURLOPT_URL = 10002 |
CURLOPT_URL = 10002 |
||
ch.setOption(CURLOPT_URL, "https://sourceforge.net") |
ch.setOption(CURLOPT_URL, "https://sourceforge.net") |
||
Line 424: | Line 464: | ||
put "Result:" && res.readRawString(res.length) |
put "Result:" && res.readRawString(res.length) |
||
end if |
end if |
||
-- "Result: <!doctype html> ..."</ |
-- "Result: <!doctype html> ..."</syntaxhighlight> |
||
*Mac OS X: |
*Mac OS X: |
||
{{libheader|Shell Xtra}} |
{{libheader|Shell Xtra}} |
||
< |
<syntaxhighlight lang="lingo">sx = xtra("Shell").new() |
||
put sx.shell_cmd("curl https://sourceforge.net")</ |
put sx.shell_cmd("curl https://sourceforge.net")</syntaxhighlight> |
||
=={{header|LiveCode}}== |
=={{header|LiveCode}}== |
||
Blocking version< |
Blocking version<syntaxhighlight lang="livecode">libURLSetSSLVerification true --check cert |
||
get URL "https://sourceforge.net/"</ |
get URL "https://sourceforge.net/"</syntaxhighlight> |
||
Non-blocking version, execute getWebResource |
Non-blocking version, execute getWebResource |
||
< |
<syntaxhighlight lang="livecode">on myUrlDownloadFinished |
||
get URL "https://sourceforge.net/" -- this will now fetch a locally cached copy |
get URL "https://sourceforge.net/" -- this will now fetch a locally cached copy |
||
put it |
put it |
||
Line 444: | Line 484: | ||
libURLSetSSLVerification true --check cert |
libURLSetSSLVerification true --check cert |
||
load URL "https://sourceforge.net/" with message "myUrlDownloadFinished" |
load URL "https://sourceforge.net/" with message "myUrlDownloadFinished" |
||
end getWebResource</ |
end getWebResource</syntaxhighlight> |
||
=={{header|LSL}}== |
=={{header|LSL}}== |
||
Line 450: | Line 490: | ||
To test it yourself; rez a box on the ground, and add the following as a New Script. |
To test it yourself; rez a box on the ground, and add the following as a New Script. |
||
< |
<syntaxhighlight lang="lsl">string sURL = "https://SourceForge.Net/"; |
||
key kHttpRequestId; |
key kHttpRequestId; |
||
default { |
default { |
||
Line 469: | Line 509: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
Output: |
Output: |
||
<pre>Status=200 |
<pre>Status=200 |
||
Line 496: | Line 536: | ||
{{works with|Lua|5.1 - 5.3}} |
{{works with|Lua|5.1 - 5.3}} |
||
{{libheader|lua-http}} |
{{libheader|lua-http}} |
||
< |
<syntaxhighlight lang="lua"> |
||
local request = require('http.request') |
local request = require('http.request') |
||
local headers, stream = request.new_from_uri("https://sourceforge.net/"):go() |
local headers, stream = request.new_from_uri("https://sourceforge.net/"):go() |
||
Line 502: | Line 542: | ||
local status = headers:get(':status') |
local status = headers:get(':status') |
||
io.write(string.format('Status: %d\nBody: %s\n', status, body) |
io.write(string.format('Status: %d\nBody: %s\n', status, body) |
||
</syntaxhighlight> |
|||
</lang> |
|||
HTTPS requests can be also done with the much smaller libraries like [[LuaSec]] or [[lua-requests]], but it currently don't support redirects, which is why I used [[lua-http]] in this example. |
HTTPS requests can be also done with the much smaller libraries like [[LuaSec]] or [[lua-requests]], but it currently don't support redirects, which is why I used [[lua-http]] in this example. |
||
=={{header|Maple}}== |
=={{header|Maple}}== |
||
<syntaxhighlight lang="maple"> |
|||
<lang Maple> |
|||
content := URL:-Get( "https://www.google.ca/" ); |
content := URL:-Get( "https://www.google.ca/" ); |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
||
Straight forward "Import" task. More complicated secure web access can be done using J/Link; essentially a link to Java API. |
Straight forward "Import" task. More complicated secure web access can be done using J/Link; essentially a link to Java API. |
||
<syntaxhighlight lang="mathematica"> |
|||
<lang Mathematica> |
|||
content=Import["https://sourceforge.net", "HTML"] |
content=Import["https://sourceforge.net", "HTML"] |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|MATLAB}} / {{header|Octave}}== |
=={{header|MATLAB}} / {{header|Octave}}== |
||
< |
<syntaxhighlight lang="matlab">s=urlread('https://sourceforge.net/')</syntaxhighlight> |
||
=={{header|Nemerle}}== |
=={{header|Nemerle}}== |
||
This example is essentially identical to the [[HTTP]] task because the <tt>WebClient</tt> object can be used with http:, https:, ftp: and file: uri's. |
This example is essentially identical to the [[HTTP]] task because the <tt>WebClient</tt> object can be used with http:, https:, ftp: and file: uri's. |
||
< |
<syntaxhighlight lang="nemerle">using System; |
||
using System.Console; |
using System.Console; |
||
using System.Net; |
using System.Net; |
||
Line 537: | Line 577: | ||
myStream.Close() |
myStream.Close() |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|NewLISP}}== |
=={{header|NewLISP}}== |
||
< |
<syntaxhighlight lang="newlisp">(! "curl https://sourceforge.net")</syntaxhighlight> |
||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
{{libheader|OpenSSL}} |
{{libheader|OpenSSL}} |
||
Compile with <code>nim c -d:ssl httpsClient.nim</code>: |
Compile with <code>nim c -d:ssl httpsClient.nim</code>: |
||
< |
<syntaxhighlight lang="nim">import httpclient |
||
var client = newHttpClient() |
var client = newHttpClient() |
||
echo client.getContent("https://sourceforge.net")</ |
echo client.getContent("https://sourceforge.net")</syntaxhighlight> |
||
=={{header|Objeck}}== |
=={{header|Objeck}}== |
||
< |
<syntaxhighlight lang="objeck"> |
||
use HTTP; |
use HTTP; |
||
Line 563: | Line 603: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Ol}}== |
|||
{{libheader|libcurl}} |
|||
<syntaxhighlight lang="scheme"> |
|||
(import (lib curl)) |
|||
(define curl (make-curl)) |
|||
(curl 'url "https://www.w3.org/") |
|||
(curl 'perform) |
|||
</syntaxhighlight> |
|||
=={{header|Pascal}}== |
=={{header|Pascal}}== |
||
{{works with|Free Pascal}} |
{{works with|Free Pascal}} |
||
Using [http://wiki.freepascal.org/fphttpclient fphttpclient] |
Using [http://wiki.freepascal.org/fphttpclient fphttpclient] |
||
< |
<syntaxhighlight lang="pascal">{$mode objfpc}{$H+} |
||
uses fphttpclient; |
uses fphttpclient; |
||
Line 583: | Line 634: | ||
end; |
end; |
||
writeln(s) |
writeln(s) |
||
end.</ |
end.</syntaxhighlight> |
||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
{{libheader|LWP}} |
{{libheader|LWP}} |
||
< |
<syntaxhighlight lang="perl"> |
||
use strict; |
use strict; |
||
use LWP::UserAgent; |
use LWP::UserAgent; |
||
Line 597: | Line 648: | ||
print $response->as_string; |
print $response->as_string; |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Perl 6}}== |
|||
{{works with|Rakudo|2017.09}} |
|||
There are several modules that provide HTTPS capability. WWW and HTTP::UserAgent are probably the most popular right now, but others exist. |
|||
<lang perl6>use WWW; |
|||
say get 'https://sourceforge.net/';</lang> |
|||
or |
|||
<lang perl6>use HTTP::UserAgent; |
|||
say HTTP::UserAgent.new.get('https://sourceforge.net/').content;</lang> |
|||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
{{libheader|libcurl}} |
{{libheader|Phix/libcurl}} |
||
Exactly the same as the [[HTTP#Phix]] task. |
Exactly the same as the [[HTTP#Phix]] task. |
||
<!--<syntaxhighlight lang="phix">(notonline)--> |
|||
<lang Phix>include builtins\libcurl.e |
|||
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> |
|||
curl_global_init() |
|||
<span style="color: #008080;">include</span> <span style="color: #000000;">builtins</span><span style="color: #0000FF;">\</span><span style="color: #000000;">libcurl</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span> |
|||
atom curl = curl_easy_init() |
|||
<span style="color: #7060A8;">curl_global_init</span><span style="color: #0000FF;">()</span> |
|||
curl_easy_setopt(curl, CURLOPT_URL, "https://sourceforge.net/") |
|||
<span style="color: #004080;">atom</span> <span style="color: #000000;">curl</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">curl_easy_init</span><span style="color: #0000FF;">()</span> |
|||
object res = curl_easy_perform_ex(curl) |
|||
<span style="color: #7060A8;">curl_easy_setopt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">curl</span><span style="color: #0000FF;">,</span> <span style="color: #004600;">CURLOPT_URL</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"https://sourceforge.net/"</span><span style="color: #0000FF;">)</span> |
|||
curl_easy_cleanup(curl) |
|||
<span style="color: #004080;">object</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">curl_easy_perform_ex</span><span style="color: #0000FF;">(</span><span style="color: #000000;">curl</span><span style="color: #0000FF;">)</span> |
|||
curl_global_cleanup() |
|||
<span style="color: #7060A8;">curl_easy_cleanup</span><span style="color: #0000FF;">(</span><span style="color: #000000;">curl</span><span style="color: #0000FF;">)</span> |
|||
<span style="color: #7060A8;">curl_global_cleanup</span><span style="color: #0000FF;">()</span> |
|||
puts(1,res)</lang> |
|||
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">res</span><span style="color: #0000FF;">)</span> |
|||
<!--</syntaxhighlight>--> |
|||
=={{header|PHP}}== |
=={{header|PHP}}== |
||
< |
<syntaxhighlight lang="php"> |
||
echo file_get_contents('https://sourceforge.net'); |
echo file_get_contents('https://sourceforge.net'); |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
PicoLisp has no functionality for communicating with a HTTPS server |
PicoLisp has no functionality for communicating with a HTTPS server |
||
(only for the other direction), but it is easy to use an external tool |
(only for the other direction), but it is easy to use an external tool |
||
<syntaxhighlight lang="picolisp"> |
|||
<lang PicoLisp> |
|||
(in '(curl "https://sourceforge.net") # Open a pipe to 'curl' |
(in '(curl "https://sourceforge.net") # Open a pipe to 'curl' |
||
(out NIL (echo)) ) # Echo to standard output |
(out NIL (echo)) ) # Echo to standard output |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Pike}}== |
=={{header|Pike}}== |
||
< |
<syntaxhighlight lang="pike"> |
||
int main() { |
int main() { |
||
write("%s\n", Protocols.HTTP.get_url_data("https://sourceforge.net")); |
write("%s\n", Protocols.HTTP.get_url_data("https://sourceforge.net")); |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|PowerShell}}== |
=={{header|PowerShell}}== |
||
< |
<syntaxhighlight lang="powershell"> |
||
Invoke-WebRequest 'https://www.rosettacode.org' |
|||
</syntaxhighlight> |
|||
<syntaxhighlight lang="powershell"> |
|||
$wc = New-Object Net.WebClient |
$wc = New-Object Net.WebClient |
||
$wc.DownloadString('https://sourceforge.net') |
$wc.DownloadString('https://sourceforge.net') |
||
</syntaxhighlight> |
|||
</lang> |
|||
If the certificate could not be validated (untrusted, self-signed, expired), then an Exception is thrown with the message ''“The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.”'' so certificate validation is done automatically by the method. |
If the certificate could not be validated (untrusted, self-signed, expired), then an Exception is thrown with the message ''“The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.”'' so certificate validation is done automatically by the method. |
||
=={{header|Python}}== |
=={{header|Python}}== |
||
Python's '''urllib.request''' library, |
Python's '''urllib.request''' library, 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. |
||
<syntaxhighlight lang="python">import urllib.request |
|||
print(urllib.request.urlopen("https://sourceforge.net/").read())</syntaxhighlight> |
|||
Python 3.x: |
|||
<lang Python> |
|||
from urllib.request import urlopen |
|||
print(urlopen('https://sourceforge.net/').read()) |
|||
</lang> |
|||
(Python 2.x) |
|||
<lang Python> |
|||
from urllib2 import urlopen |
|||
print urlopen('https://sourceforge.net/').read() |
|||
</lang> |
|||
{{libheader|Requests}} |
|||
{{works with|Python|2.7, 3.4–3.7}} |
|||
From the Requests documentation: ''"Requests verifies SSL certificates for HTTPS requests, just like a web browser. By default, SSL verification is enabled, and Requests will throw a SSLError if it’s unable to verify the certificate"'' |
|||
<lang Python> |
|||
import requests |
|||
print(requests.get("https://sourceforge.net").text) |
|||
</lang> |
|||
=={{header|R}}== |
=={{header|R}}== |
||
Line 679: | Line 708: | ||
The basic idea is to use getURL (as with [[HTTP_Request]]), but with some extra parameters. |
The basic idea is to use getURL (as with [[HTTP_Request]]), but with some extra parameters. |
||
< |
<syntaxhighlight lang="r">library(RCurl) |
||
webpage <- getURL("https://sourceforge.net/", .opts=list(followlocation=TRUE, ssl.verifyhost=FALSE, ssl.verifypeer=FALSE))</ |
webpage <- getURL("https://sourceforge.net/", .opts=list(followlocation=TRUE, ssl.verifyhost=FALSE, ssl.verifypeer=FALSE))</syntaxhighlight> |
||
In this case, the webpage output contains unprocessed characters, e.g. \" instead of " and \\ instead of \, so we need to process the markup. |
In this case, the webpage output contains unprocessed characters, e.g. \" instead of " and \\ instead of \, so we need to process the markup. |
||
<syntaxhighlight lang="r"> |
|||
<lang R> |
|||
wp <- readLines(tc <- textConnection(webpage)) |
wp <- readLines(tc <- textConnection(webpage)) |
||
close(tc) |
close(tc) |
||
</syntaxhighlight> |
|||
</lang> |
|||
Finally, we parse the HTML and find the interesting bit. |
Finally, we parse the HTML and find the interesting bit. |
||
<syntaxhighlight lang="r"> |
|||
<lang R> |
|||
pagetree <- htmlTreeParse(wp) |
pagetree <- htmlTreeParse(wp) |
||
pagetree$children$html |
pagetree$children$html |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
<syntaxhighlight lang="racket"> |
|||
<lang Racket> |
|||
#lang racket |
#lang racket |
||
(require net/url) |
(require net/url) |
||
Line 702: | Line 731: | ||
#:redirections 100) |
#:redirections 100) |
||
(current-output-port)) |
(current-output-port)) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Raku}}== |
|||
(formerly Perl 6) |
|||
{{works with|Rakudo|2017.09}} |
|||
There are several modules that provide HTTPS capability. WWW and HTTP::UserAgent are probably the most popular right now, but others exist. |
|||
<syntaxhighlight lang="raku" line>use WWW; |
|||
say get 'https://sourceforge.net/';</syntaxhighlight> |
|||
or |
|||
<syntaxhighlight lang="raku" line>use HTTP::UserAgent; |
|||
say HTTP::UserAgent.new.get('https://sourceforge.net/').content;</syntaxhighlight> |
|||
=={{header|REALbasic}}== |
=={{header|REALbasic}}== |
||
REALBasic provides an HTTPSecureSocket class for handling HTTPS connections. The 'Get' method of the HTTPSecureSocket is overloaded and can download data to a file or return data as a string, in both cases an optional timeout argument can be passed. |
REALBasic provides an HTTPSecureSocket class for handling HTTPS connections. The 'Get' method of the HTTPSecureSocket is overloaded and can download data to a file or return data as a string, in both cases an optional timeout argument can be passed. |
||
<syntaxhighlight lang="realbasic"> |
|||
<lang REALbasic> |
|||
Dim sock As New HTTPSecureSocket |
Dim sock As New HTTPSecureSocket |
||
Print(sock.Get("https://sourceforge.net", 10)) //set the timeout period to 10 seconds. |
Print(sock.Get("https://sourceforge.net", 10)) //set the timeout period to 10 seconds. |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"> |
||
cStr= download("http://sourceforge.net/") |
cStr= download("http://sourceforge.net/") |
||
see cStr + nl |
see cStr + nl |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|RLaB}}== |
=={{header|RLaB}}== |
||
Line 724: | Line 764: | ||
This solution doesn't use the <code>open-uri</code> convenience package that the [[HTTP Request#Ruby]] solution uses: the <code>Net::HTTP</code> object must be told to use SSL before the session is started. |
This solution doesn't use the <code>open-uri</code> convenience package that the [[HTTP Request#Ruby]] solution uses: the <code>Net::HTTP</code> object must be told to use SSL before the session is started. |
||
< |
<syntaxhighlight lang="ruby"> |
||
require 'net/https' |
require 'net/https' |
||
require 'uri' |
require 'uri' |
||
Line 740: | Line 780: | ||
puts content.body |
puts content.body |
||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
outputs |
outputs |
||
Line 765: | Line 805: | ||
</body> |
</body> |
||
</html> |
</html> |
||
</pre> |
|||
=={{header|Rust}}== |
|||
<syntaxhighlight lang="rust"> |
|||
extern crate reqwest; |
|||
fn main() { |
|||
let response = match reqwest::blocking::get("https://sourceforge.net") { |
|||
Ok(response) => response, |
|||
Err(e) => panic!("error encountered while making request: {:?}", e), |
|||
}; |
|||
println!("{}", response.text().unwrap()); |
|||
} |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
<!-- Server: sfs-consume-7 --> |
|||
<html class="no-js" lang="en"> |
|||
<head> |
|||
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> |
|||
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" /> |
|||
<script> |
|||
var __gdpr = true; |
|||
var __ccpa = false; |
|||
... |
|||
</pre> |
</pre> |
||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
{{libheader|Scala}} |
{{libheader|Scala}} |
||
< |
<syntaxhighlight lang="scala">import scala.io.Source |
||
object HttpsTest extends App { |
object HttpsTest extends App { |
||
Line 775: | Line 843: | ||
Source.fromURL("https://sourceforge.net").getLines.foreach(println) |
Source.fromURL("https://sourceforge.net").getLines.foreach(println) |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Seed7}}== |
=={{header|Seed7}}== |
||
Line 782: | Line 850: | ||
HTTPS protocol go get a file. |
HTTPS protocol go get a file. |
||
< |
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i"; |
||
include "gethttps.s7i"; |
include "gethttps.s7i"; |
||
include "utf8.s7i"; |
include "utf8.s7i"; |
||
Line 789: | Line 857: | ||
begin |
begin |
||
writeln(STD_UTF8_OUT, getHttps("sourceforge.net")); |
writeln(STD_UTF8_OUT, getHttps("sourceforge.net")); |
||
end func;</ |
end func;</syntaxhighlight> |
||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
<lang |
<syntaxhighlight lang="ruby">require('LWP::UserAgent') |
||
require('LWP::Protocol::https') |
|||
var url = 'https://rosettacode.org'; |
|||
func get(url) { |
|||
var ua = lwp.new( |
|||
static ua = %O<LWP::UserAgent>.new( |
|||
agent => 'Mozilla/5.0', |
|||
agent => 'Mozilla/5.0', |
|||
ssl_opts => Hash.new(verify_hostname => 1), |
|||
ssl_opts => Hash(verify_hostname => 1), |
|||
); |
|||
) |
|||
var resp = ua.get(url) |
|||
if (resp.is_success) { |
|||
return resp.decoded_content |
|||
} |
|||
die "Failed to GET #{url}: #{resp.status_line}" |
|||
} |
|||
say get("https://rosettacode.org")</syntaxhighlight> |
|||
var resp = ua.get(url); |
|||
resp.is_success || die "Failed to GET #{url.dump}: #{resp.status_line}"; |
|||
print resp.decoded_content;</lang> |
|||
=={{header|Swift}}== |
=={{header|Swift}}== |
||
< |
<syntaxhighlight lang="swift">import Foundation |
||
// With https |
// With https |
||
Line 819: | Line 892: | ||
} |
} |
||
CFRunLoopRun() // dispatch</ |
CFRunLoopRun() // dispatch</syntaxhighlight> |
||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
Though Tcl's built-in <code>http</code> package does not understand SSL, it does support the registration of external handlers to accommodate additional protocols. This allows the use of the [http://tls.sourceforge.net/ Tls] package to supply the missing functionality with only a single line to complete the registration. |
Though Tcl's built-in <code>http</code> package does not understand SSL, it does support the registration of external handlers to accommodate additional protocols. This allows the use of the [http://tls.sourceforge.net/ Tls] package to supply the missing functionality with only a single line to complete the registration. |
||
< |
<syntaxhighlight lang="tcl"> |
||
package require http |
package require http |
||
package require tls |
package require tls |
||
Line 841: | Line 914: | ||
puts [http::data $token] |
puts [http::data $token] |
||
http::cleanup $token |
http::cleanup $token |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|TUSCRIPT}}== |
=={{header|TUSCRIPT}}== |
||
< |
<syntaxhighlight lang="tuscript"> |
||
$$ MODE TUSCRIPT |
$$ MODE TUSCRIPT |
||
SET DATEN = REQUEST ("https://sourceforge.net") |
SET DATEN = REQUEST ("https://sourceforge.net") |
||
*{daten} |
*{daten} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|UNIX Shell}}== |
=={{header|UNIX Shell}}== |
||
< |
<syntaxhighlight lang="bash"> |
||
curl -k -s -L https://sourceforge.net/ |
curl -k -s -L https://sourceforge.net/ |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|VBScript}}== |
=={{header|VBScript}}== |
||
Line 859: | Line 932: | ||
Based on code at [http://itknowledgeexchange.techtarget.com/vbscript-systems-administrator/how-to-retrieve-html-web-pages-with-vbscript-via-the-microsoftxmlhttp-object/ How to retrieve HTML web pages with VBScript via the Microsoft.XmlHttp object] |
Based on code at [http://itknowledgeexchange.techtarget.com/vbscript-systems-administrator/how-to-retrieve-html-web-pages-with-vbscript-via-the-microsoftxmlhttp-object/ How to retrieve HTML web pages with VBScript via the Microsoft.XmlHttp object] |
||
<syntaxhighlight lang="vb"> |
|||
<lang vb> |
|||
Option Explicit |
Option Explicit |
||
Line 877: | Line 950: | ||
Set oHTTP = Nothing |
Set oHTTP = Nothing |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Visual Basic}}== |
=={{header|Visual Basic}}== |
||
Line 886: | Line 959: | ||
{{works with|VBA|6.5}} |
{{works with|VBA|6.5}} |
||
{{works with|VBA|7.1}} |
{{works with|VBA|7.1}} |
||
< |
<syntaxhighlight lang="vb">Sub Main() |
||
Dim HttpReq As WinHttp.WinHttpRequest |
Dim HttpReq As WinHttp.WinHttpRequest |
||
' in the "references" dialog of the IDE, check |
' in the "references" dialog of the IDE, check |
||
Line 906: | Line 979: | ||
HttpReq.Send |
HttpReq.Send |
||
Debug.Print HttpReq.ResponseText |
Debug.Print HttpReq.ResponseText |
||
End Sub</ |
End Sub</syntaxhighlight> |
||
=={{header|Visual Basic .NET}}== |
=={{header|Visual Basic .NET}}== |
||
< |
<syntaxhighlight lang="vbnet"> |
||
Imports System.Net |
Imports System.Net |
||
Line 915: | Line 988: | ||
Dim content As String = client.DownloadString("https://sourceforge.net") |
Dim content As String = client.DownloadString("https://sourceforge.net") |
||
Console.WriteLine(content) |
Console.WriteLine(content) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Wren}}== |
|||
{{trans|C}} |
|||
{{libheader|libcurl}} |
|||
An embedded program so we can ask the C host to communicate with libcurl for us. |
|||
<syntaxhighlight lang="wren">/* HTTPS.wren */ |
|||
var CURLOPT_URL = 10002 |
|||
var CURLOPT_FOLLOWLOCATION = 52 |
|||
var CURLOPT_ERRORBUFFER = 10010 |
|||
foreign class Curl { |
|||
construct easyInit() {} |
|||
foreign easySetOpt(opt, param) |
|||
foreign easyPerform() |
|||
foreign easyCleanup() |
|||
} |
|||
var curl = Curl.easyInit() |
|||
if (curl == 0) { |
|||
System.print("Error initializing cURL.") |
|||
return |
|||
} |
|||
curl.easySetOpt(CURLOPT_URL, "https://www.w3.org/") |
|||
curl.easySetOpt(CURLOPT_FOLLOWLOCATION, 1) |
|||
curl.easySetOpt(CURLOPT_ERRORBUFFER, 0) // buffer to be supplied by C |
|||
var status = curl.easyPerform() |
|||
if (status != 0) { |
|||
System.print("Failed to perform task.") |
|||
return |
|||
} |
|||
curl.easyCleanup()</syntaxhighlight> |
|||
<br> |
|||
We now embed this in the following C program, compile and run it. |
|||
<syntaxhighlight lang="c">/* gcc HTTPS.c -o HTTPS -lcurl -lwren -lm */ |
|||
#include <stdio.h> |
|||
#include <stdlib.h> |
|||
#include <string.h> |
|||
#include <curl/curl.h> |
|||
#include "wren.h" |
|||
/* C <=> Wren interface functions */ |
|||
void C_curlAllocate(WrenVM* vm) { |
|||
CURL** pcurl = (CURL**)wrenSetSlotNewForeign(vm, 0, 0, sizeof(CURL*)); |
|||
*pcurl = curl_easy_init(); |
|||
} |
|||
void C_easyPerform(WrenVM* vm) { |
|||
CURL* curl = *(CURL**)wrenGetSlotForeign(vm, 0); |
|||
CURLcode cc = curl_easy_perform(curl); |
|||
wrenSetSlotDouble(vm, 0, (double)cc); |
|||
} |
|||
void C_easyCleanup(WrenVM* vm) { |
|||
CURL* curl = *(CURL**)wrenGetSlotForeign(vm, 0); |
|||
curl_easy_cleanup(curl); |
|||
} |
|||
void C_easySetOpt(WrenVM* vm) { |
|||
CURL* curl = *(CURL**)wrenGetSlotForeign(vm, 0); |
|||
CURLoption opt = (CURLoption)wrenGetSlotDouble(vm, 1); |
|||
if (opt < 10000) { |
|||
long lparam = (long)wrenGetSlotDouble(vm, 2); |
|||
curl_easy_setopt(curl, opt, lparam); |
|||
} else { |
|||
if (opt == CURLOPT_URL) { |
|||
const char *url = wrenGetSlotString(vm, 2); |
|||
curl_easy_setopt(curl, opt, url); |
|||
} else if (opt == CURLOPT_ERRORBUFFER) { |
|||
char buffer[CURL_ERROR_SIZE]; |
|||
curl_easy_setopt(curl, opt, buffer); |
|||
} |
|||
} |
|||
} |
|||
WrenForeignClassMethods bindForeignClass(WrenVM* vm, const char* module, const char* className) { |
|||
WrenForeignClassMethods methods; |
|||
methods.allocate = NULL; |
|||
methods.finalize = NULL; |
|||
if (strcmp(module, "main") == 0) { |
|||
if (strcmp(className, "Curl") == 0) { |
|||
methods.allocate = C_curlAllocate; |
|||
} |
|||
} |
|||
return methods; |
|||
} |
|||
WrenForeignMethodFn bindForeignMethod( |
|||
WrenVM* vm, |
|||
const char* module, |
|||
const char* className, |
|||
bool isStatic, |
|||
const char* signature) { |
|||
if (strcmp(module, "main") == 0) { |
|||
if (strcmp(className, "Curl") == 0) { |
|||
if (!isStatic && strcmp(signature, "easySetOpt(_,_)") == 0) return C_easySetOpt; |
|||
if (!isStatic && strcmp(signature, "easyPerform()") == 0) return C_easyPerform; |
|||
if (!isStatic && strcmp(signature, "easyCleanup()") == 0) return C_easyCleanup; |
|||
} |
|||
} |
|||
return NULL; |
|||
} |
|||
static void writeFn(WrenVM* vm, const char* text) { |
|||
printf("%s", text); |
|||
} |
|||
void errorFn(WrenVM* vm, WrenErrorType errorType, const char* module, const int line, const char* msg) { |
|||
switch (errorType) { |
|||
case WREN_ERROR_COMPILE: |
|||
printf("[%s line %d] [Error] %s\n", module, line, msg); |
|||
break; |
|||
case WREN_ERROR_STACK_TRACE: |
|||
printf("[%s line %d] in %s\n", module, line, msg); |
|||
break; |
|||
case WREN_ERROR_RUNTIME: |
|||
printf("[Runtime Error] %s\n", msg); |
|||
break; |
|||
} |
|||
} |
|||
char *readFile(const char *fileName) { |
|||
FILE *f = fopen(fileName, "r"); |
|||
fseek(f, 0, SEEK_END); |
|||
long fsize = ftell(f); |
|||
rewind(f); |
|||
char *script = malloc(fsize + 1); |
|||
fread(script, 1, fsize, f); |
|||
fclose(f); |
|||
script[fsize] = 0; |
|||
return script; |
|||
} |
|||
int main(int argc, char **argv) { |
|||
WrenConfiguration config; |
|||
wrenInitConfiguration(&config); |
|||
config.writeFn = &writeFn; |
|||
config.errorFn = &errorFn; |
|||
config.bindForeignClassFn = &bindForeignClass; |
|||
config.bindForeignMethodFn = &bindForeignMethod; |
|||
WrenVM* vm = wrenNewVM(&config); |
|||
const char* module = "main"; |
|||
const char* fileName = "HTTPS.wren"; |
|||
char *script = readFile(fileName); |
|||
WrenInterpretResult result = wrenInterpret(vm, module, script); |
|||
switch (result) { |
|||
case WREN_RESULT_COMPILE_ERROR: |
|||
printf("Compile Error!\n"); |
|||
break; |
|||
case WREN_RESULT_RUNTIME_ERROR: |
|||
printf("Runtime Error!\n"); |
|||
break; |
|||
case WREN_RESULT_SUCCESS: |
|||
break; |
|||
} |
|||
wrenFreeVM(vm); |
|||
free(script); |
|||
return 0; |
|||
}</syntaxhighlight> |
|||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
Using the cURL library to do the heavy lifting: |
Using the cURL library to do the heavy lifting: |
||
< |
<syntaxhighlight lang="zkl">zkl: var ZC=Import("zklCurl") |
||
zkl: var data=ZC().get("https://sourceforge.net") |
zkl: var data=ZC().get("https://sourceforge.net") |
||
L(Data(36,265),826,0)</ |
L(Data(36,265),826,0)</syntaxhighlight> |
||
get returns the text of the response along with two counts: the bytes of header in front of the html code and the byte count of stuff after the end of the page. So, if you wanted to look at the header: |
get returns the text of the response along with two counts: the bytes of header in front of the html code and the byte count of stuff after the end of the page. So, if you wanted to look at the header: |
||
<pre>zkl: data[0][0,data[1]).text |
<pre>zkl: data[0][0,data[1]).text |
||
Line 944: | Line 1,182: | ||
{{omit from|Brainf***}} |
{{omit from|Brainf***}} |
||
{{omit from|Commodore BASIC|Does not have network access}} |
{{omit from|Commodore BASIC|Does not have network access}} |
||
{{omit from|EasyLang|Has no internet functions}} |
|||
{{omit from|Inform 7|Does not have network access.}} |
{{omit from|Inform 7|Does not have network access.}} |
||
{{omit from|Integer BASIC|No TCP/IP network support on Apple II}} |
{{omit from|Integer BASIC|No TCP/IP network support on Apple II}} |
Latest revision as of 10:01, 10 December 2023
You are encouraged to solve this task according to the task description, using any language you may know.
- Task
Send a GET request to obtain the resource located at the URL "https://www.w3.org/", then print it to the console.
Checking the host certificate for validity is recommended.
Do not authenticate. 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.
Ada
Exactly the same as the HTTP task, assuming you compiled AWS with openssl support.
with AWS.Client;
with AWS.Response;
with Ada.Text_IO; use Ada.Text_IO;
procedure GetHttps is
begin
Put_Line (AWS.Response.Message_Body (AWS.Client.Get (
URL => "https://sourceforge.net/")));
end GetHttps;
Arturo
print read "https://www.w3.org/"
AutoHotkey
URL := "https://sourceforge.net/"
WININET_Init()
msgbox % html := UrlGetContents(URL)
WININET_UnInit()
return
#include urlgetcontents.ahk
#include wininet.ahk
BaCon
This code requires BaCon 3.8.2 or later.
OPTION TLS TRUE
website$ = "www.google.com"
OPEN website$ & ":443" FOR NETWORK AS mynet
SEND "GET / HTTP/1.1\r\nHost: " & website$ & "\r\n\r\n" TO mynet
WHILE WAIT(mynet, 1000)
RECEIVE dat$ FROM mynet
total$ = total$ & dat$
IF REGEX(dat$, "\r\n\r\n$") THEN BREAK : ' Quit receiving data when end indicator was reached
WEND
CLOSE NETWORK mynet
PRINT REPLACE$(total$, "\r\n[0-9a-fA-F]+\r\n", "\r\n", TRUE) : ' Remove chunk indicators from HTML data
Batch File
:: Must have curl.exe
curl.exe -k -s -L https://sourceforge.net/
C
#include <stdio.h>
#include <stdlib.h>
#include <curl/curl.h>
CURL *curl;
char buffer[CURL_ERROR_SIZE];
int main(void) {
if ((curl = curl_easy_init()) != NULL) {
curl_easy_setopt(curl, CURLOPT_URL, "https://sourceforge.net/");
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, buffer);
if (curl_easy_perform(curl) != CURLE_OK) {
fprintf(stderr, "%s\n", buffer);
return EXIT_FAILURE;
}
curl_easy_cleanup(curl);
}
return EXIT_SUCCESS;
}
C#
using System;
using System.Net;
class Program
{
static void Main(string[] args)
{
var client = new WebClient();
var data = client.DownloadString("https://www.google.com");
Console.WriteLine(data);
}
}
This does not work for urls requiring a secure (SSL) connection.
Clojure
Using the duck-streams as a convenient wrapper for Java's networking classes, grabbing the contents of an HTTPS URL is as easy as:
(use '[clojure.contrib.duck-streams :only (slurp*)])
(print (slurp* "https://sourceforge.net"))
The usual Java mechanisms can be used to manage acceptance of SSL certificates if required.
(print (slurp "https://sourceforge.net"))
D
Using curl
import std.stdio;
import std.net.curl;
auto data = get("https://sourceforge.net");
writeln(data);
Common Lisp
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.
(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")
(format t "~a~%" (nth-value 0 (dex:get "https://www.w3.org/")))
Delphi
program ShowHTTPS;
{$APPTYPE CONSOLE}
uses IdHttp, IdSSLOpenSSL;
var
s: string;
lHTTP: TIdHTTP;
begin
lHTTP := TIdHTTP.Create(nil);
try
lHTTP.IOHandler := TIdSSLIOHandlerSocketOpenSSL.Create(lHTTP);
lHTTP.HandleRedirects := True;
s := lHTTP.Get('https://sourceforge.net/');
Writeln(s);
finally
lHTTP.Free;
end;
end.
EchoLisp
file->string usage: the server must allow cross-domain access, or a browser add-on like cors-everywhere must be installed to bypass cross-domain checking.
;; asynchronous call back definition
(define (success name text) (writeln 'Loaded name) (writeln text))
;;
(file->string success "https:/sourceforge.net")
Erlang
Synchronous
-module(main).
-export([main/1]).
main([Url|[]]) ->
inets:start(),
ssl:start(),
case http:request(get, {URL, []}, [{ssl,[{verify,0}]}], []) of
{ok, {_V, _H, Body}} -> io:fwrite("~p~n",[Body]);
{error, Res} -> io:fwrite("~p~n", [Res])
end.
Asynchronous
-module(main).
-export([main/1]).
main([Url|[]]) ->
inets:start(),
ssl:start(),
http:request(get, {Url, [] }, [{ssl,[{verify,0}]}], [{sync, false}]),
receive
{http, {_ReqId, Res}} -> io:fwrite("~p~n",[Res]);
_Any -> io:fwrite("Error: ~p~n",[_Any])
after 10000 -> io:fwrite("Timed out.~n",[])
end.
Using it
|escript ./req.erl https://sourceforge.net/
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)
Frink
print[read["https://sourceforge.net/"]]
FutureBasic
include "NSLog.incl"
local fn GET_HTTPS
CFStringRef response = unix @"curl -ksL https://sourceforge.net/"
CFDataRef dta = fn StringData( response, NSUTF8StringEncoding )
CFDictionaryRef options = @{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)}
CFAttributedStringRef aStr = fn AttributedStringWithHTML( dta, options )
NSLog( @"%@", aStr )
end fn
fn GET_HTTPS
HandleEvents
- Output:
We're sorry -- the Sourceforge site is currently in Disaster Recovery mode. Please check back later. { NSColor = "sRGB IEC61966-2.1 colorspace 0 0 0 1"; NSFont = "\"Times-Roman 12.00 pt. P [] (0x7f8f94e11ce0) fobj=0x7f8f94e0fda0, spc=3.00\""; NSKern = 0; NSParagraphStyle = "Alignment 4, LineSpacing 0, ParagraphSpacing 0, ParagraphSpacingBefore 0, HeadIndent 0, TailIndent 0, FirstLineHeadIndent 0, LineHeight 0/0, LineHeightMultiple 0, LineBreakMode 0, Tabs (\n), DefaultTabInterval 36, Blocks (\n), Lists (\n), BaseWritingDirection 0, HyphenationFactor 0, TighteningForTruncation YES, HeaderLevel 0 LineBreakStrategy 0"; NSStrokeColor = "sRGB IEC61966-2.1 colorspace 0 0 0 1"; NSStrokeWidth = 0; }
Go
package main
import (
"io"
"log"
"net/http"
"os"
)
func main() {
r, err := http.Get("https://sourceforge.net/")
if err != nil {
log.Fatalln(err)
}
io.Copy(os.Stdout, r.Body)
}
Groovy
new URL("https://sourceforge.net").eachLine { println it }
Haskell
This is just the example from Network.HTTP.Conduit, with the http URL replaced with an https one, since http-conduit natively supports https without needing any additional work.
#!/usr/bin/runhaskell
import Network.HTTP.Conduit
import qualified Data.ByteString.Lazy as L
import Network (withSocketsDo)
main = withSocketsDo
$ simpleHttp "https://sourceforge.net/" >>= L.putStr
Icon and Unicon
# Requires Unicon version 13
procedure main(arglist)
url := (\arglist[1] | "https://sourceforge.net/")
w := open(url, "m-") | stop("Cannot open " || url)
while write(read(w))
close(w)
end
- Output:
prompt$ unicon -s https.icn -x | head -n2 <!doctype html> <!-- Server: sfs-consume-15 -->
Ioke
connection = URL new("https://sourceforge.net") openConnection
scanner = Scanner new(connection getInputStream)
while(scanner hasNext,
scanner next println
)
J
Using gethttp from Web Scraping
#page=: gethttp'https://sourceforge.net'
0
#page=: '--no-check-certificate' gethttp'https://sourceforge.net'
900
(We can not load the example page using https unless we disable certificate checking. The numbers are the number of characters retrieved.)
Java
javax.net.ssl
Additional certificate information is available through the javax.net.ssl.HttpsURLConnection interface.
URL url = new URL("https://sourceforge.net");
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
Scanner scanner = new Scanner(connection.getInputStream());
while (scanner.hasNext()) {
System.out.println(scanner.next());
}
java.net.http
Using the standard Java 11 HTTP Client
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.Charset;
public class Main {
public static void main(String[] args) {
var request = HttpRequest.newBuilder(URI.create("https://sourceforge.net"))
.GET()
.build();
HttpClient.newHttpClient()
.sendAsync(request, HttpResponse.BodyHandlers.ofString(Charset.defaultCharset()))
.thenApply(HttpResponse::body)
.thenAccept(System.out::println)
.join();
}
}
JavaScript
Browser
Using fetch API and async/await:
const response = await fetch('https://rosettacode.org');
const text = await response.text();
console.log(text);
fetch("https://sourceforge.net").then(function (response) {
return response.text();
}).then(function (body) {
return body;
});
Node.js
require("https").get("https://sourceforge.net", function (resp) {
let body = "";
resp.on("data", function (chunk) {
body += chunk;
});
resp.on("end", function () {
console.log(body);
});
}).on("error", function (err) {
console.error("Error: " + err.message);
});
Julia
# v0.6.0
using Requests
str = readstring(get("https://sourceforge.net/"))
Kotlin
// version 1.1.2
import java.net.URL
import javax.net.ssl.HttpsURLConnection
import java.io.InputStreamReader
import java.util.Scanner
fun main(args: Array<String>) {
val url = URL("https://en.wikipedia.org/wiki/Main_Page")
val connection = url.openConnection() as HttpsURLConnection
val isr = InputStreamReader(connection.inputStream)
val sc = Scanner(isr)
while (sc.hasNextLine()) println(sc.nextLine())
sc.close()
}
Or simplier, since Kotlin 1.2
import java.net.URL
fun main(args: Array<String>){
println(URL("https://sourceforge.net").readText())
}
Lasso
local(x = curl('https://sourceforge.net'))
local(y = #x->result)
#y->asString
If a site with an invalid SSL Cert is encountered the curl type throws the following error:
- Output:
FAILURE: 60 Peer certificate cannot be authenticated with given CA certificates
Lingo
- Windows:
ch = xtra("Curl").new()
CURLOPT_URL = 10002
ch.setOption(CURLOPT_URL, "https://sourceforge.net")
res = ch.exec(1)
if integerP(res) then
put "Error:" && curl_error(res)
else
put "Result:" && res.readRawString(res.length)
end if
-- "Result: <!doctype html> ..."
- Mac OS X:
sx = xtra("Shell").new()
put sx.shell_cmd("curl https://sourceforge.net")
LiveCode
Blocking version
libURLSetSSLVerification true --check cert
get URL "https://sourceforge.net/"
Non-blocking version, execute getWebResource
on myUrlDownloadFinished
get URL "https://sourceforge.net/" -- this will now fetch a locally cached copy
put it
end myUrlDownloadFinished
command getWebResource
libURLFollowHttpRedirects true
libURLSetSSLVerification true --check cert
load URL "https://sourceforge.net/" with message "myUrlDownloadFinished"
end getWebResource
LSL
Virtually identical to the HTTP Task.
To test it yourself; rez a box on the ground, and add the following as a New Script.
string sURL = "https://SourceForge.Net/";
key kHttpRequestId;
default {
state_entry() {
kHttpRequestId = llHTTPRequest(sURL, [], "");
}
http_response(key kRequestId, integer iStatus, list lMetaData, string sBody) {
if(kRequestId==kHttpRequestId) {
llOwnerSay("Status="+(string)iStatus);
integer x = 0;
for(x=0 ; x<llGetListLength(lMetaData) ; x++) {
llOwnerSay("llList2String(lMetaData, "+(string)x+")="+llList2String(lMetaData, x));
}
list lBody = llParseString2List(sBody, ["\n"], []);
for(x=0 ; x<llGetListLength(lBody) ; x++) {
llOwnerSay("llList2String(lBody, "+(string)x+")="+llList2String(lBody, x));
}
}
}
}
Output:
Status=200 llList2String(lMetaData, 0)=0 llList2String(lMetaData, 1)=2048 llList2String(lBody, 0)=<!doctype html> llList2String(lBody, 1)=<!-- Server: sfs-consume-7 --> llList2String(lBody, 2)=<!--[if lt IE 7 ]> <html lang="en" class="no-js ie6" > <![endif]--> llList2String(lBody, 3)=<!--[if IE 7 ]> <html lang="en" class="no-js ie7" > <![endif]--> llList2String(lBody, 4)=<!--[if IE 8 ]> <html lang="en" class="no-js ie8" > <![endif]--> llList2String(lBody, 5)=<!--[if IE 9 ]> <html lang="en" class="no-js ie9" > <![endif]--> llList2String(lBody, 6)=<!--[if (gt IE 9)|!(IE)]>--> <html lang="en" class="no-js"> <!--<![endif]--> llList2String(lBody, 7)= <head> llList2String(lBody, 8)= <meta charset="utf-8"> llList2String(lBody, 9)= llList2String(lBody, 10)= <meta id="webtracker" name="webtracker" content='{"event_id": "ea71f064-ca28-11e1-98cc-0019b9f0e8fc"}'> llList2String(lBody, 11)= <meta name="description" content="Free, secure and fast downloads from the largest Open Source applications and software directory - SourceForge.net"> llList2String(lBody, 12)= <meta name="keywords" content="Open Source, Open Source Software, Development, Community, Source Code, Secure, Downloads, Free Software"> llList2String(lBody, 13)=<meta name="msvalidate.01" content="0279349BB9CF7ACA882F86F29C50D3EA" /> llList2String(lBody, 14)= <meta name="viewport" content="width=device-width, initial-scale=1.0"> llList2String(lBody, 15)= <title>SourceForge - Download, Develop and Publish Free Open Source Software</title> llList2String(lBody, 16)= <link rel="shortcut icon" href="http://a.fsdn.com/con/img/sftheme/favicon.ico"> ... ... ... ... ... ... ... ... ... ... ... ... ... ...
Lua
local request = require('http.request')
local headers, stream = request.new_from_uri("https://sourceforge.net/"):go()
local body = stream:get_body_as_string()
local status = headers:get(':status')
io.write(string.format('Status: %d\nBody: %s\n', status, body)
HTTPS requests can be also done with the much smaller libraries like LuaSec or lua-requests, but it currently don't support redirects, which is why I used lua-http in this example.
Maple
content := URL:-Get( "https://www.google.ca/" );
Mathematica / Wolfram Language
Straight forward "Import" task. More complicated secure web access can be done using J/Link; essentially a link to Java API.
content=Import["https://sourceforge.net", "HTML"]
MATLAB / Octave
s=urlread('https://sourceforge.net/')
Nemerle
This example is essentially identical to the HTTP task because the WebClient object can be used with http:, https:, ftp: and file: uri's.
using System;
using System.Console;
using System.Net;
using System.IO;
module HTTP
{
Main() : void
{
def wc = WebClient();
def myStream = wc.OpenRead(https://sourceforge.com);
def sr = StreamReader(myStream);
WriteLine(sr.ReadToEnd());
myStream.Close()
}
}
NewLISP
(! "curl https://sourceforge.net")
Nim
Compile with nim c -d:ssl httpsClient.nim
:
import httpclient
var client = newHttpClient()
echo client.getContent("https://sourceforge.net")
Objeck
use HTTP;
class HttpsTest {
function : Main(args : String[]) ~ Nil {
client := HttpsClient->New();
lines := client->Get("https://sourceforge.net");
each(i : lines) {
lines->Get(i)->As(String)->PrintLine();
};
}
}
Ol
(import (lib curl))
(define curl (make-curl))
(curl 'url "https://www.w3.org/")
(curl 'perform)
Pascal
Using fphttpclient
{$mode objfpc}{$H+}
uses fphttpclient;
var
s: string;
hc: tfphttpclient;
begin
hc := tfphttpclient.create(nil);
try
s := hc.get('https://www.example.com')
finally
hc.free
end;
writeln(s)
end.
Perl
use strict;
use LWP::UserAgent;
my $url = 'https://www.rosettacode.org';
my $response = LWP::UserAgent->new->get( $url );
$response->is_success or die "Failed to GET '$url': ", $response->status_line;
print $response->as_string;
Phix
Exactly the same as the HTTP#Phix task.
without js include builtins\libcurl.e curl_global_init() atom curl = curl_easy_init() curl_easy_setopt(curl, CURLOPT_URL, "https://sourceforge.net/") object res = curl_easy_perform_ex(curl) curl_easy_cleanup(curl) curl_global_cleanup() puts(1,res)
PHP
echo file_get_contents('https://sourceforge.net');
PicoLisp
PicoLisp has no functionality for communicating with a HTTPS server (only for the other direction), but it is easy to use an external tool
(in '(curl "https://sourceforge.net") # Open a pipe to 'curl'
(out NIL (echo)) ) # Echo to standard output
Pike
int main() {
write("%s\n", Protocols.HTTP.get_url_data("https://sourceforge.net"));
}
PowerShell
Invoke-WebRequest 'https://www.rosettacode.org'
$wc = New-Object Net.WebClient
$wc.DownloadString('https://sourceforge.net')
If the certificate could not be validated (untrusted, self-signed, expired), then an Exception is thrown with the message “The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.” so certificate validation is done automatically by the method.
Python
Python's urllib.request library, 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.
import urllib.request
print(urllib.request.urlopen("https://sourceforge.net/").read())
R
The basic idea is to use getURL (as with HTTP_Request), but with some extra parameters.
library(RCurl)
webpage <- getURL("https://sourceforge.net/", .opts=list(followlocation=TRUE, ssl.verifyhost=FALSE, ssl.verifypeer=FALSE))
In this case, the webpage output contains unprocessed characters, e.g. \" instead of " and \\ instead of \, so we need to process the markup.
wp <- readLines(tc <- textConnection(webpage))
close(tc)
Finally, we parse the HTML and find the interesting bit.
pagetree <- htmlTreeParse(wp)
pagetree$children$html
Racket
#lang racket
(require net/url)
(copy-port (get-pure-port (string->url "https://www.google.com")
#:redirections 100)
(current-output-port))
Raku
(formerly Perl 6)
There are several modules that provide HTTPS capability. WWW and HTTP::UserAgent are probably the most popular right now, but others exist.
use WWW;
say get 'https://sourceforge.net/';
or
use HTTP::UserAgent;
say HTTP::UserAgent.new.get('https://sourceforge.net/').content;
REALbasic
REALBasic provides an HTTPSecureSocket class for handling HTTPS connections. The 'Get' method of the HTTPSecureSocket is overloaded and can download data to a file or return data as a string, in both cases an optional timeout argument can be passed.
Dim sock As New HTTPSecureSocket
Print(sock.Get("https://sourceforge.net", 10)) //set the timeout period to 10 seconds.
Ring
cStr= download("http://sourceforge.net/")
see cStr + nl
RLaB
See HTTP#RLaB
Ruby
This solution doesn't use the open-uri
convenience package that the HTTP Request#Ruby solution uses: the Net::HTTP
object must be told to use SSL before the session is started.
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
http.start do
content = http.get(uri)
p [content.code, content.message]
pp content.to_hash
puts content.body
end
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>
Rust
extern crate reqwest;
fn main() {
let response = match reqwest::blocking::get("https://sourceforge.net") {
Ok(response) => response,
Err(e) => panic!("error encountered while making request: {:?}", e),
};
println!("{}", response.text().unwrap());
}
- Output:
<!-- Server: sfs-consume-7 --> <html class="no-js" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no" /> <script> var __gdpr = true; var __ccpa = false; ...
Scala
import scala.io.Source
object HttpsTest extends App {
System.setProperty("http.agent", "*")
Source.fromURL("https://sourceforge.net").getLines.foreach(println)
}
Seed7
The library gethttps.s7i defines the function getHttps which uses the HTTPS protocol go get a file.
$ include "seed7_05.s7i";
include "gethttps.s7i";
include "utf8.s7i";
const proc: main is func
begin
writeln(STD_UTF8_OUT, getHttps("sourceforge.net"));
end func;
Sidef
require('LWP::UserAgent')
require('LWP::Protocol::https')
func get(url) {
static ua = %O<LWP::UserAgent>.new(
agent => 'Mozilla/5.0',
ssl_opts => Hash(verify_hostname => 1),
)
var resp = ua.get(url)
if (resp.is_success) {
return resp.decoded_content
}
die "Failed to GET #{url}: #{resp.status_line}"
}
say get("https://rosettacode.org")
Swift
import Foundation
// With https
let request = NSURLRequest(URL: NSURL(string: "https://sourceforge.net")!)
NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue()) {res, data, err in // callback
// data is binary
if (data != nil) {
let string = NSString(data: data!, encoding: NSUTF8StringEncoding)
println(string)
}
}
CFRunLoopRun() // dispatch
Tcl
Though Tcl's built-in http
package does not understand SSL, it does support the registration of external handlers to accommodate additional protocols. This allows the use of the Tls package to supply the missing functionality with only a single line to complete the registration.
package require http
package require tls
# Tell the http package what to do with “https:” URLs.
#
# First argument is the protocol name, second the default port, and
# third the connection builder command
http::register "https" 443 ::tls::socket
# Make a secure connection, which is almost identical to normal
# connections except for the different protocol in the URL.
set token [http::geturl "https://sourceforge.net/"]
# Now as for conventional use of the “http” package
puts [http::data $token]
http::cleanup $token
TUSCRIPT
$$ MODE TUSCRIPT
SET DATEN = REQUEST ("https://sourceforge.net")
*{daten}
UNIX Shell
curl -k -s -L https://sourceforge.net/
VBScript
Based on code at How to retrieve HTML web pages with VBScript via the Microsoft.XmlHttp object
Option Explicit
Const sURL="https://sourceforge.net/"
Dim oHTTP
Set oHTTP = CreateObject("Microsoft.XmlHTTP")
On Error Resume Next
oHTTP.Open "GET", sURL, False
oHTTP.Send ""
If Err.Number = 0 Then
WScript.Echo oHTTP.responseText
Else
Wscript.Echo "error " & Err.Number & ": " & Err.Description
End If
Set oHTTP = Nothing
Visual Basic
Sub Main()
Dim HttpReq As WinHttp.WinHttpRequest
' in the "references" dialog of the IDE, check
' "Microsoft WinHTTP Services, version 5.1" (winhttp.dll)
Const HTTPREQUEST_PROXYSETTING_PROXY As Long = 2
Const WINHTTP_FLAG_SECURE_PROTOCOL_TLS1 As Long = &H80&
Const WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_1 As Long = &H200&
Const WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_2 As Long = &H800&
#Const USE_PROXY = 1
Set HttpReq = New WinHttp.WinHttpRequest
HttpReq.Open "GET", "https://groups.google.com/robots.txt"
HttpReq.Option(WinHttpRequestOption_SecureProtocols) = WINHTTP_FLAG_SECURE_PROTOCOL_TLS1 Or _
WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_1 Or _
WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_2
#If USE_PROXY Then
HttpReq.SetProxy HTTPREQUEST_PROXYSETTING_PROXY, "my_proxy:80"
#End If
HttpReq.SetTimeouts 1000, 1000, 1000, 1000
HttpReq.Send
Debug.Print HttpReq.ResponseText
End Sub
Visual Basic .NET
Imports System.Net
Dim client As WebClient = New WebClient()
Dim content As String = client.DownloadString("https://sourceforge.net")
Console.WriteLine(content)
Wren
An embedded program so we can ask the C host to communicate with libcurl for us.
/* HTTPS.wren */
var CURLOPT_URL = 10002
var CURLOPT_FOLLOWLOCATION = 52
var CURLOPT_ERRORBUFFER = 10010
foreign class Curl {
construct easyInit() {}
foreign easySetOpt(opt, param)
foreign easyPerform()
foreign easyCleanup()
}
var curl = Curl.easyInit()
if (curl == 0) {
System.print("Error initializing cURL.")
return
}
curl.easySetOpt(CURLOPT_URL, "https://www.w3.org/")
curl.easySetOpt(CURLOPT_FOLLOWLOCATION, 1)
curl.easySetOpt(CURLOPT_ERRORBUFFER, 0) // buffer to be supplied by C
var status = curl.easyPerform()
if (status != 0) {
System.print("Failed to perform task.")
return
}
curl.easyCleanup()
We now embed this in the following C program, compile and run it.
/* gcc HTTPS.c -o HTTPS -lcurl -lwren -lm */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
#include "wren.h"
/* C <=> Wren interface functions */
void C_curlAllocate(WrenVM* vm) {
CURL** pcurl = (CURL**)wrenSetSlotNewForeign(vm, 0, 0, sizeof(CURL*));
*pcurl = curl_easy_init();
}
void C_easyPerform(WrenVM* vm) {
CURL* curl = *(CURL**)wrenGetSlotForeign(vm, 0);
CURLcode cc = curl_easy_perform(curl);
wrenSetSlotDouble(vm, 0, (double)cc);
}
void C_easyCleanup(WrenVM* vm) {
CURL* curl = *(CURL**)wrenGetSlotForeign(vm, 0);
curl_easy_cleanup(curl);
}
void C_easySetOpt(WrenVM* vm) {
CURL* curl = *(CURL**)wrenGetSlotForeign(vm, 0);
CURLoption opt = (CURLoption)wrenGetSlotDouble(vm, 1);
if (opt < 10000) {
long lparam = (long)wrenGetSlotDouble(vm, 2);
curl_easy_setopt(curl, opt, lparam);
} else {
if (opt == CURLOPT_URL) {
const char *url = wrenGetSlotString(vm, 2);
curl_easy_setopt(curl, opt, url);
} else if (opt == CURLOPT_ERRORBUFFER) {
char buffer[CURL_ERROR_SIZE];
curl_easy_setopt(curl, opt, buffer);
}
}
}
WrenForeignClassMethods bindForeignClass(WrenVM* vm, const char* module, const char* className) {
WrenForeignClassMethods methods;
methods.allocate = NULL;
methods.finalize = NULL;
if (strcmp(module, "main") == 0) {
if (strcmp(className, "Curl") == 0) {
methods.allocate = C_curlAllocate;
}
}
return methods;
}
WrenForeignMethodFn bindForeignMethod(
WrenVM* vm,
const char* module,
const char* className,
bool isStatic,
const char* signature) {
if (strcmp(module, "main") == 0) {
if (strcmp(className, "Curl") == 0) {
if (!isStatic && strcmp(signature, "easySetOpt(_,_)") == 0) return C_easySetOpt;
if (!isStatic && strcmp(signature, "easyPerform()") == 0) return C_easyPerform;
if (!isStatic && strcmp(signature, "easyCleanup()") == 0) return C_easyCleanup;
}
}
return NULL;
}
static void writeFn(WrenVM* vm, const char* text) {
printf("%s", text);
}
void errorFn(WrenVM* vm, WrenErrorType errorType, const char* module, const int line, const char* msg) {
switch (errorType) {
case WREN_ERROR_COMPILE:
printf("[%s line %d] [Error] %s\n", module, line, msg);
break;
case WREN_ERROR_STACK_TRACE:
printf("[%s line %d] in %s\n", module, line, msg);
break;
case WREN_ERROR_RUNTIME:
printf("[Runtime Error] %s\n", msg);
break;
}
}
char *readFile(const char *fileName) {
FILE *f = fopen(fileName, "r");
fseek(f, 0, SEEK_END);
long fsize = ftell(f);
rewind(f);
char *script = malloc(fsize + 1);
fread(script, 1, fsize, f);
fclose(f);
script[fsize] = 0;
return script;
}
int main(int argc, char **argv) {
WrenConfiguration config;
wrenInitConfiguration(&config);
config.writeFn = &writeFn;
config.errorFn = &errorFn;
config.bindForeignClassFn = &bindForeignClass;
config.bindForeignMethodFn = &bindForeignMethod;
WrenVM* vm = wrenNewVM(&config);
const char* module = "main";
const char* fileName = "HTTPS.wren";
char *script = readFile(fileName);
WrenInterpretResult result = wrenInterpret(vm, module, script);
switch (result) {
case WREN_RESULT_COMPILE_ERROR:
printf("Compile Error!\n");
break;
case WREN_RESULT_RUNTIME_ERROR:
printf("Runtime Error!\n");
break;
case WREN_RESULT_SUCCESS:
break;
}
wrenFreeVM(vm);
free(script);
return 0;
}
zkl
Using the cURL library to do the heavy lifting:
zkl: var ZC=Import("zklCurl")
zkl: var data=ZC().get("https://sourceforge.net")
L(Data(36,265),826,0)
get returns the text of the response along with two counts: the bytes of header in front of the html code and the byte count of stuff after the end of the page. So, if you wanted to look at the header:
zkl: data[0][0,data[1]).text HTTP/1.1 200 OK Server: nginx Date: Sun, 23 Mar 2014 07:36:51 GMT Content-Type: text/html; charset=utf-8 Connection: close ...
or some of the html:
zkl: data[0][data[1],200).text <!doctype html> <!-- Server: sfs-consume-8 --> <!--[if lt IE 7 ]> <html lang="en" class="no-js ie6"> <![endif]--> <!--[if IE 7 ]> <html lang="en" class="no-js ie7"> <![endif]--> <!--[if IE 8 ]>
- Programming Tasks
- Programming environment operations
- Networking and Web Interaction
- Ada
- AWS
- Arturo
- AutoHotkey
- Wininet
- BaCon
- Batch File
- C
- Libcurl
- C sharp
- Clojure
- D
- Common Lisp
- DRAKMA
- Dexador
- Delphi
- OpenSSL
- EchoLisp
- Erlang
- F Sharp
- Frink
- FutureBasic
- Go
- Groovy
- Haskell
- Http-conduit
- Unicon
- Ioke
- J
- Java
- JavaScript
- Julia
- Kotlin
- Lasso
- Lingo
- Curl Xtra
- Shell Xtra
- LiveCode
- LSL
- Lua
- Lua-http
- Maple
- Mathematica
- Wolfram Language
- MATLAB
- Octave
- Nemerle
- NewLISP
- Nim
- Objeck
- Ol
- Pascal
- Perl
- LWP
- Phix
- Phix/libcurl
- PHP
- PicoLisp
- Pike
- PowerShell
- Python
- R
- RCurl
- XML
- Racket
- Raku
- REALbasic
- Ring
- RLaB
- Ruby
- Rust
- Scala
- Seed7
- Sidef
- Swift
- Tcl
- TUSCRIPT
- UNIX Shell
- VBScript
- Microsoft.XmlHTTP
- Visual Basic
- Microsoft.WinHttp
- Visual Basic .NET
- Wren
- Zkl
- Applesoft BASIC/Omit
- Brainf***/Omit
- Commodore BASIC/Omit
- EasyLang/Omit
- Inform 7/Omit
- Integer BASIC/Omit
- Locomotive Basic/Omit
- Lotus 123 Macro Scripting/Omit
- M4/Omit
- Maxima/Omit
- ML/I/Omit
- Openscad/Omit
- PARI/GP/Omit
- PostScript/Omit
- Retro/Omit
- SQL PL/Omit
- TI-83 BASIC/Omit
- TI-89 BASIC/Omit
- Unlambda/Omit
- Yorick/Omit
- ZX Spectrum Basic/Omit