HTTPS/Authenticated: Difference between revisions
m (→{{header|Wren}}: Another minor change) |
|||
(14 intermediate revisions by 9 users not shown) | |||
Line 3: | Line 3: | ||
The goal of this task is to demonstrate [[HTTPS request]]s with authentication. |
The goal of this task is to demonstrate [[HTTPS request]]s with authentication. |
||
Implementations of this task should not use client certificates for this: that is the subject of [[Client-Authenticated HTTPS Request|another task]]. |
Implementations of this task should not use client certificates for this: that is the subject of [[Client-Authenticated HTTPS Request|another task]]. |
||
=={{header|Arturo}}== |
|||
<syntaxhighlight lang="arturo">user: "admin" |
|||
pass: "admin" |
|||
inspect request.headers:#[ |
|||
Authorization: "Basic " ++ encode user ++ ":" ++ pass |
|||
] "https://httpbin.org/basic-auth/admin/admin" ø</syntaxhighlight> |
|||
{{out}} |
|||
<pre>[ :dictionary |
|||
version : 1.1 :string |
|||
body : { |
|||
"authenticated": true, |
|||
"user": "admin" |
|||
} |
|||
:string |
|||
headers : [ :dictionary |
|||
server : gunicorn/19.9.0 :string |
|||
content-length : 48 :integer |
|||
access-control-allow-credentials : true :logical |
|||
content-type : application/json :string |
|||
date : [ :date |
|||
hour : 15 :integer |
|||
minute : 14 :integer |
|||
second : 57 :integer |
|||
nanosecond : 0 :integer |
|||
day : 20 :integer |
|||
Day : Tuesday :string |
|||
days : 353 :integer |
|||
month : 12 :integer |
|||
Month : December :string |
|||
year : 2022 :integer |
|||
utc : -3600 :integer |
|||
] |
|||
access-control-allow-origin : * :string |
|||
connection : keep-alive :string |
|||
] |
|||
status : 200 :integer |
|||
]</pre> |
|||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
{{libheader|iweb}} |
{{libheader|iweb}} |
||
{{libheader|COM}} |
{{libheader|COM}} |
||
< |
<syntaxhighlight lang="autohotkey">iWeb_Init() |
||
pwb := iWeb_newGui(0, 0, 1000, 800) |
pwb := iWeb_newGui(0, 0, 1000, 800) |
||
iWeb_nav(pwb, "http://www.facebook.com/login.php?ref=pf") |
iWeb_nav(pwb, "http://www.facebook.com/login.php?ref=pf") |
||
Line 21: | Line 63: | ||
#Include iweb.ahk |
#Include iweb.ahk |
||
#Include COM.ahk |
#Include COM.ahk |
||
#Include COMinvokeDeep.ahk</ |
#Include COMinvokeDeep.ahk</syntaxhighlight> |
||
=={{header|BaCon}}== |
=={{header|BaCon}}== |
||
< |
<syntaxhighlight lang="bacon">OPTION TLS TRUE |
||
website$ = "website.com" |
website$ = "website.com" |
||
Line 40: | Line 82: | ||
CLOSE NETWORK conn |
CLOSE NETWORK conn |
||
PRINT total$</ |
PRINT total$</syntaxhighlight> |
||
=={{header|C}}== |
=={{header|C}}== |
||
{{libheader|libcurl}} |
{{libheader|libcurl}} |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#include <stdlib.h> |
#include <stdlib.h> |
||
#include "curl/curl.h" |
#include "curl/curl.h" |
||
Line 65: | Line 107: | ||
} |
} |
||
return EXIT_SUCCESS; |
return EXIT_SUCCESS; |
||
}</ |
}</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 90: | Line 132: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
{{libheader|clj-http}} |
{{libheader|clj-http}} |
||
< |
<syntaxhighlight lang="clojure">(clj-http.client/get "https://somedomain.com" |
||
{:basic-auth ["user" "pass"]})</ |
{:basic-auth ["user" "pass"]})</syntaxhighlight> |
||
=={{header|Delphi}}== |
=={{header|Delphi}}== |
||
< |
<syntaxhighlight lang="delphi">program ShowHTTPSAuthenticated; |
||
{$APPTYPE CONSOLE} |
{$APPTYPE CONSOLE} |
||
Line 124: | Line 166: | ||
lIOHandler.Free; |
lIOHandler.Free; |
||
end; |
end; |
||
end.</ |
end.</syntaxhighlight> |
||
=={{header|Go}}== |
=={{header|Go}}== |
||
The task solution is really the client program, but to test it I wrote a server and created a custom certificate. I won't describe the certificate, but this is the server: |
The task solution is really the client program, but to test it I wrote a server and created a custom certificate. I won't describe the certificate, but this is the server: |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 165: | Line 207: | ||
http.HandleFunc("/", hw) |
http.HandleFunc("/", hw) |
||
log.Fatal(http.ListenAndServeTLS(":8080", "cert.pem", "key.pem", nil)) |
log.Fatal(http.ListenAndServeTLS(":8080", "cert.pem", "key.pem", nil)) |
||
}</ |
}</syntaxhighlight> |
||
It is a "Hello world" server, but over TLS and with basic authentication required on the Get. Errors are logged to aid client debugging. |
It is a "Hello world" server, but over TLS and with basic authentication required on the Get. Errors are logged to aid client debugging. |
||
The client: |
The client: |
||
< |
<syntaxhighlight lang="go">package main |
||
import ( |
import ( |
||
Line 217: | Line 259: | ||
} |
} |
||
fmt.Println(string(b)) |
fmt.Println(string(b)) |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
Line 223: | Line 265: | ||
Example uses the [https://hackage.haskell.org/package/req <tt>req</tt>] and [https://hackage.haskell.org/package/aeson <tt>aeson</tt>] packages: |
Example uses the [https://hackage.haskell.org/package/req <tt>req</tt>] and [https://hackage.haskell.org/package/aeson <tt>aeson</tt>] packages: |
||
< |
<syntaxhighlight lang="haskell">{-# LANGUAGE OverloadedStrings #-} |
||
module Main (main) where |
module Main (main) where |
||
Line 249: | Line 291: | ||
jsonResponse |
jsonResponse |
||
(basicAuth "someuser" "somepassword") |
(basicAuth "someuser" "somepassword") |
||
print (responseBody response :: Value)</ |
print (responseBody response :: Value)</syntaxhighlight> |
||
=={{header|Java}}== |
|||
<syntaxhighlight lang="java"> |
|||
import java.io.IOException; |
|||
import java.net.Authenticator; |
|||
import java.net.PasswordAuthentication; |
|||
import java.net.URI; |
|||
import java.net.URISyntaxException; |
|||
import java.net.http.HttpClient; |
|||
import java.net.http.HttpRequest; |
|||
import java.net.http.HttpResponse; |
|||
import java.net.http.HttpResponse.BodyHandlers; |
|||
public final class HTTPSAuthenticated { |
|||
public static void main(String[] aArgs) throws IOException, InterruptedException, URISyntaxException { |
|||
HttpClient client = HttpClient.newBuilder() |
|||
.authenticator( new MyAuthenticator() ) |
|||
.build(); |
|||
HttpRequest request = HttpRequest.newBuilder() |
|||
.GET() |
|||
.uri( new URI("https://postman-echo.com/basic-auth") ) // This website requires authentication |
|||
.build(); |
|||
HttpResponse<String> response = client.send(request, BodyHandlers.ofString()); |
|||
System.out.println("Status: " + response.statusCode()); |
|||
} |
|||
} |
|||
final class MyAuthenticator extends Authenticator { |
|||
@Override |
|||
protected PasswordAuthentication getPasswordAuthentication() { |
|||
String username = "kingkong"; |
|||
String password = "test1234"; |
|||
return new PasswordAuthentication(username, password.toCharArray()); |
|||
} |
|||
} |
|||
</syntaxhighlight> |
|||
{{ out }} |
|||
<pre> |
|||
Status: 200 |
|||
</pre> |
|||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
An example using HTTP (see the source for HTTP.jl for the code below ) to access and play a song: |
An example using HTTP (see the source for HTTP.jl for the code below ) to access and play a song: |
||
<syntaxhighlight lang="julia"> |
|||
<lang Julia> |
|||
using HTTP, HTTP.IOExtras, JSON, MusicProcessing |
using HTTP, HTTP.IOExtras, JSON, MusicProcessing |
||
HTTP.open("POST", "http://music.com/play") do io |
HTTP.open("POST", "http://music.com/play") do io |
||
Line 267: | Line 356: | ||
end |
end |
||
end |
end |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
< |
<syntaxhighlight lang="scala">// version 1.2.0 |
||
import java.net.Authenticator |
import java.net.Authenticator |
||
Line 297: | Line 386: | ||
println(line) |
println(line) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Lasso}}== |
=={{header|Lasso}}== |
||
< |
<syntaxhighlight lang="lasso">local(username = 'hello',password = 'world') |
||
local(x = curl('https://sourceforge.net')) |
local(x = curl('https://sourceforge.net')) |
||
#x->set(CURLOPT_USERPWD, #username + ':' + #password) |
#x->set(CURLOPT_USERPWD, #username + ':' + #password) |
||
local(y = #x->result) |
local(y = #x->result) |
||
#y->asString</ |
#y->asString</syntaxhighlight> |
||
=={{header|LiveCode}}== |
=={{header|LiveCode}}== |
||
HTTP Basic Auth as part of url |
HTTP Basic Auth as part of url |
||
< |
<syntaxhighlight lang="livecode">command getAuthWebResource |
||
libURLFollowHttpRedirects true |
libURLFollowHttpRedirects true |
||
libURLSetSSLVerification true |
libURLSetSSLVerification true |
||
put URL "https://user:passwd@example.basicauth.com/" into response |
put URL "https://user:passwd@example.basicauth.com/" into response |
||
put response |
put response |
||
end getAuthWebResource</ |
end getAuthWebResource</syntaxhighlight> |
||
You can also set the headers for the basic auth requests |
You can also set the headers for the basic auth requests |
||
< |
<syntaxhighlight lang="livecode">command getAuthWebResource |
||
libURLFollowHttpRedirects true |
libURLFollowHttpRedirects true |
||
libURLSetSSLVerification true |
libURLSetSSLVerification true |
||
Line 322: | Line 411: | ||
put URL "https://example.basicauth.com" into response |
put URL "https://example.basicauth.com" into response |
||
put response |
put response |
||
end getAuthWebResource</ |
end getAuthWebResource</syntaxhighlight> |
||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
Line 328: | Line 417: | ||
{{libheader|lua-requests}} |
{{libheader|lua-requests}} |
||
< |
<syntaxhighlight lang="lua"> |
||
local requests = require('requests') |
local requests = require('requests') |
||
local auth = requests.HTTPBasicAuth('admin', 'admin') |
local auth = requests.HTTPBasicAuth('admin', 'admin') |
||
Line 336: | Line 425: | ||
}) |
}) |
||
io.write(string.format('Status: %d', resp.status_code)) |
io.write(string.format('Status: %d', resp.status_code)) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 344: | Line 433: | ||
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang="mathematica">a = RunThrough["curl -u JohnDoe:Password https://www.example.com", 1] |
||
For[ i=0, i < Length[a] , i++, SomeFunction[a]]</ |
For[ i=0, i < Length[a] , i++, SomeFunction[a]]</syntaxhighlight> |
||
=={{header|Nim}}== |
|||
{{libheader|OpenSSL}} |
|||
Compile with command <code>nim c -d:ssl https_authenticated.nim</code>. |
|||
<syntaxhighlight lang="nim">import httpclient, base64 |
|||
const |
|||
User = "admin" |
|||
Password = "admin" |
|||
let headers = newHttpHeaders({"Authorization": "Basic " & base64.encode(User & ":" & Password)}) |
|||
let client = newHttpClient(headers = headers) |
|||
echo client.getContent("https://httpbin.org/basic-auth/admin/admin")</syntaxhighlight> |
|||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
{{libheader|LWP}} |
{{libheader|LWP}} |
||
< |
<syntaxhighlight lang="perl">use LWP::UserAgent qw(); |
||
my $ua = LWP::UserAgent->new; |
my $ua = LWP::UserAgent->new; |
||
my $netloc = 'http://www.buddhism-dict.net/cgi-bin/xpr-dealt.pl:80'; |
my $netloc = 'http://www.buddhism-dict.net/cgi-bin/xpr-dealt.pl:80'; |
||
Line 367: | Line 471: | ||
passwd => 'YYYYYY', |
passwd => 'YYYYYY', |
||
'.persistent' => 'y', # tick checkbox |
'.persistent' => 'y', # tick checkbox |
||
});</ |
});</syntaxhighlight> |
||
=={{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. |
||
You can of course use curl_easy_setopt(curl,CURLOPT_USERPWD,"user:password") rather than embed that in the url. |
You can of course use curl_easy_setopt(curl,CURLOPT_USERPWD,"user:password") rather than embed that in the url. |
||
<!--<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://user:password@example.com/") |
|||
<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://user:password@example.com/"</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|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
< |
<syntaxhighlight lang="picolisp">(let (User "Bill" Pass "T0p5ecRet" Url "https://www.example.com") |
||
(in (list 'curl "-u" (pack User ': Pass) Url) |
(in (list 'curl "-u" (pack User ': Pass) Url) |
||
(while (line) |
(while (line) |
||
(doSomeProcessingWithLine @) ) ) )</ |
(doSomeProcessingWithLine @) ) ) )</syntaxhighlight> |
||
=={{header|PowerShell}}== |
=={{header|PowerShell}}== |
||
{{trans|C#}} |
{{trans|C#}} |
||
< |
<syntaxhighlight lang="powershell">$client = [Net.WebClient]::new() |
||
# credentials of current user: |
# credentials of current user: |
||
$client.Credentials = [Net.CredentialCache]::DefaultCredentials |
$client.Credentials = [Net.CredentialCache]::DefaultCredentials |
||
Line 396: | Line 503: | ||
# $client.Credentials = [System.Net.NetworkCredential]::new("User", "Password") |
# $client.Credentials = [System.Net.NetworkCredential]::new("User", "Password") |
||
$data = $client.DownloadString("https://example.com") |
$data = $client.DownloadString("https://example.com") |
||
Write-Host $data</ |
Write-Host $data</syntaxhighlight> |
||
=={{header|Python}}== |
=={{header|Python}}== |
||
{{works with|Python|2.4 and 2.6}} |
{{works with|Python|2.4 and 2.6}} |
||
Line 402: | Line 509: | ||
'''Note:''' You should install '''''mechanize''''' to run code below. Visit: http://wwwsearch.sourceforge.net/mechanize/ |
'''Note:''' You should install '''''mechanize''''' to run code below. Visit: http://wwwsearch.sourceforge.net/mechanize/ |
||
< |
<syntaxhighlight lang="python">#!/usr/bin/python |
||
# -*- coding: utf-8 -*- |
# -*- coding: utf-8 -*- |
||
Line 425: | Line 532: | ||
response = br.submit() |
response = br.submit() |
||
print response.read()</ |
print response.read()</syntaxhighlight> |
||
{{libheader|Requests}} |
{{libheader|Requests}} |
||
{{works with|Python|2.7, 3.4–3.7}} |
{{works with|Python|2.7, 3.4–3.7}} |
||
< |
<syntaxhighlight lang="python">import requests |
||
username = "user" |
username = "user" |
||
Line 437: | Line 544: | ||
response = requests.get(url, auth=(username, password) |
response = requests.get(url, auth=(username, password) |
||
print(response.text)</ |
print(response.text)</syntaxhighlight> |
||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
< |
<syntaxhighlight lang="racket"> |
||
#lang racket |
#lang racket |
||
Line 461: | Line 568: | ||
(for ([l (in-port read-line (get-pure-port (string->url "https://www.google.com/")))]) |
(for ([l (in-port read-line (get-pure-port (string->url "https://www.google.com/")))]) |
||
(displayln l)))) |
(displayln l)))) |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
Line 468: | Line 575: | ||
Used here to connect to my local wireless router to a page that is password protected. Obviously not going to be generally publicly accessible but should be easily adaptable to other sites / devices. |
Used here to connect to my local wireless router to a page that is password protected. Obviously not going to be generally publicly accessible but should be easily adaptable to other sites / devices. |
||
<lang |
<syntaxhighlight lang="raku" line>use HTTP::UserAgent; |
||
my $username = 'username'; # my username |
my $username = 'username'; # my username |
||
Line 477: | Line 584: | ||
$ua.auth( $username, $password ); |
$ua.auth( $username, $password ); |
||
my $response = $ua.get: $address; |
my $response = $ua.get: $address; |
||
say $response.is-success ?? $response.content !! $response.status-line;</ |
say $response.is-success ?? $response.content !! $response.status-line;</syntaxhighlight> |
||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
< |
<syntaxhighlight lang="ruby">require 'uri' |
||
require 'net/http' |
require 'net/http' |
||
Line 488: | Line 595: | ||
request.basic_auth('username', 'password') |
request.basic_auth('username', 'password') |
||
http.request request |
http.request request |
||
end</ |
end</syntaxhighlight> |
||
=={{header|Run BASIC}}== |
=={{header|Run BASIC}}== |
||
< |
<syntaxhighlight lang="runbasic">html " |
||
<CENTER><TABLE CELLPADDING=0 CELLSPACING=0 border=1 bgcolor=wheat> |
<CENTER><TABLE CELLPADDING=0 CELLSPACING=0 border=1 bgcolor=wheat> |
||
<TR><TD colspan=2 bgcolor=tan align=center>LOGIN</TD></TR> |
<TR><TD colspan=2 bgcolor=tan align=center>LOGIN</TD></TR> |
||
Line 522: | Line 629: | ||
[exit] |
[exit] |
||
end</ |
end</syntaxhighlight> |
||
[[File:ClientAuthorizationRunBasic.png]] |
[[File:ClientAuthorizationRunBasic.png]] |
||
=={{header|Rust}}== |
|||
<syntaxhighlight lang="rust"> |
|||
extern crate reqwest; |
|||
use reqwest::blocking::Client; |
|||
use reqwest::header::CONNECTION; |
|||
fn main() { |
|||
let client = Client::new(); |
|||
// reqwest uses strongly-typed structs for creating headers |
|||
let res = client |
|||
.get("https://www.example.com") |
|||
.basic_auth("user", Some("password")) |
|||
.header(CONNECTION, "close") |
|||
.send() |
|||
.unwrap(); |
|||
let body = res.text().unwrap(); |
|||
println!("{}", body); |
|||
} |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
</pre> |
|||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
< |
<syntaxhighlight lang="scala">import java.net.{Authenticator, PasswordAuthentication, URL} |
||
import javax.net.ssl.HttpsURLConnection |
import javax.net.ssl.HttpsURLConnection |
||
Line 549: | Line 684: | ||
new BufferedSource(con.getInputStream).getLines.foreach(println(_)) |
new BufferedSource(con.getInputStream).getLines.foreach(println(_)) |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
< |
<syntaxhighlight lang="ruby">require('WWW::Mechanize') |
||
var mech = %s'WWW::Mechanize'.new( |
var mech = %s'WWW::Mechanize'.new( |
||
Line 565: | Line 700: | ||
'login' => 'XXXXXX', |
'login' => 'XXXXXX', |
||
'passwd' => 'YYYYYY', |
'passwd' => 'YYYYYY', |
||
))</ |
))</syntaxhighlight> |
||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
Line 571: | Line 706: | ||
{{tcllib|base64}} |
{{tcllib|base64}} |
||
Uses the [http://tls.sourceforge.net Tls] package. |
Uses the [http://tls.sourceforge.net Tls] package. |
||
< |
<syntaxhighlight lang="tcl">package require http |
||
package require tls |
package require tls |
||
http::register https 443 ::tls::socket |
http::register https 443 ::tls::socket |
||
Line 585: | Line 720: | ||
# Now as for conventional use of the “http” package |
# Now as for conventional use of the “http” package |
||
set data [http::data $token] |
set data [http::data $token] |
||
http::cleanup $token</ |
http::cleanup $token</syntaxhighlight> |
||
=={{header|Visual Basic}}== |
=={{header|Visual Basic}}== |
||
Line 594: | Line 729: | ||
{{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() |
||
' in the "references" dialog of the IDE, check |
' in the "references" dialog of the IDE, check |
||
' "Microsoft WinHTTP Services, version 5.1" (winhttp.dll) |
' "Microsoft WinHTTP Services, version 5.1" (winhttp.dll) |
||
Line 615: | Line 750: | ||
HttpReq.Send |
HttpReq.Send |
||
Debug.Print HttpReq.ResponseText |
Debug.Print HttpReq.ResponseText |
||
End Sub</ |
End Sub</syntaxhighlight> |
||
=={{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_Authenticated.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://user:password@secure.example.com/") |
|||
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_Authenticated.c -o HTTPS_Authenticated -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_Authenticated.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 cURL to do the heavy lifting, get a XML list of computers connected to my router. |
Using cURL to do the heavy lifting, get a XML list of computers connected to my router. |
||
< |
<syntaxhighlight lang="zkl">zkl: var ZC=Import("zklCurl") |
||
zkl: var data=ZC().get("http://usr:pw@192.168.1.1/computer_list.xml") |
zkl: var data=ZC().get("http://usr:pw@192.168.1.1/computer_list.xml") |
||
L(Data(1,049),121,0) |
L(Data(1,049),121,0) |
||
zkl: data[0][121,*].text</ |
zkl: data[0][121,*].text</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 634: | Line 934: | ||
{{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|Locomotive Basic|Does not have network access.}} |
{{omit from|Locomotive Basic|Does not have network access.}} |
Latest revision as of 10:13, 10 December 2023
You are encouraged to solve this task according to the task description, using any language you may know.
The goal of this task is to demonstrate HTTPS requests with authentication. Implementations of this task should not use client certificates for this: that is the subject of another task.
Arturo
user: "admin"
pass: "admin"
inspect request.headers:#[
Authorization: "Basic " ++ encode user ++ ":" ++ pass
] "https://httpbin.org/basic-auth/admin/admin" ø
- Output:
[ :dictionary version : 1.1 :string body : { "authenticated": true, "user": "admin" } :string headers : [ :dictionary server : gunicorn/19.9.0 :string content-length : 48 :integer access-control-allow-credentials : true :logical content-type : application/json :string date : [ :date hour : 15 :integer minute : 14 :integer second : 57 :integer nanosecond : 0 :integer day : 20 :integer Day : Tuesday :string days : 353 :integer month : 12 :integer Month : December :string year : 2022 :integer utc : -3600 :integer ] access-control-allow-origin : * :string connection : keep-alive :string ] status : 200 :integer ]
AutoHotkey
iWeb_Init()
pwb := iWeb_newGui(0, 0, 1000, 800)
iWeb_nav(pwb, "http://www.facebook.com/login.php?ref=pf")
iWeb_Term()
iWeb_complete(pwb)
inputbox, email, email
inputbox, pass, password
iWeb_setDomObj(pwb,"Email",email)
iWeb_setDomObj(pwb,"pass",pass)
iWeb_clickDomObj(pwb, "login")
return
#Include iweb.ahk
#Include COM.ahk
#Include COMinvokeDeep.ahk
BaCon
OPTION TLS TRUE
website$ = "website.com"
username$ = "nobody"
password$ = "ignore"
OPEN website$ & ":443" FOR NETWORK AS conn
SEND "GET / HTTP/1.1\r\nHost: " & website$ & "\r\nAuthorization: Basic " & B64ENC$(username$ & ":" & password$) & "\r\n\r\n" TO conn
WHILE WAIT(conn, 2000)
RECEIVE data$ FROM conn
total$ = total$ & data$
IF INSTR(data$, "</html>") THEN BREAK
WEND
CLOSE NETWORK conn
PRINT total$
C
#include <stdio.h>
#include <stdlib.h>
#include "curl/curl.h"
int
main(void)
{
CURL *curl;
char buffer[CURL_ERROR_SIZE];
if ((curl = curl_easy_init()) != NULL) {
curl_easy_setopt(curl, CURLOPT_URL, "https://user:password@secure.example.com/");
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();
// credentials of current user:
client.Credentials = CredentialCache.DefaultCredentials;
// or specify credentials manually:
client.Credentials = new NetworkCredential("User", "Password");
var data = client.DownloadString("https://example.com");
Console.WriteLine(data);
}
}
Clojure
(clj-http.client/get "https://somedomain.com"
{:basic-auth ["user" "pass"]})
Delphi
program ShowHTTPSAuthenticated;
{$APPTYPE CONSOLE}
uses IdHttp, IdSSLOpenSSL;
var
s: string;
lHTTP: TIdHTTP;
lIOHandler: TIdSSLIOHandlerSocketOpenSSL;
begin
ReportMemoryLeaksOnShutdown := True;
lHTTP := TIdHTTP.Create(nil);
lIOHandler := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
try
lHTTP.Request.Username := 'USERNAME';
lHTTP.Request.Password := 'PASSWD';
lHTTP.IOHandler := lIOHandler;
lHTTP.HandleRedirects := True;
s := lHTTP.Get('https://SomeSecureSite.net/');
Writeln(s);
finally
lHTTP.Free;
lIOHandler.Free;
end;
end.
Go
The task solution is really the client program, but to test it I wrote a server and created a custom certificate. I won't describe the certificate, but this is the server:
package main
import (
"encoding/base64"
"io"
"log"
"net/http"
"strings"
)
const userPass = "rosetta:code"
const unauth = http.StatusUnauthorized
func hw(w http.ResponseWriter, req *http.Request) {
auth := req.Header.Get("Authorization")
if !strings.HasPrefix(auth, "Basic ") {
log.Print("Invalid authorization:", auth)
http.Error(w, http.StatusText(unauth), unauth)
return
}
up, err := base64.StdEncoding.DecodeString(auth[6:])
if err != nil {
log.Print("authorization decode error:", err)
http.Error(w, http.StatusText(unauth), unauth)
return
}
if string(up) != userPass {
log.Print("invalid username:password:", string(up))
http.Error(w, http.StatusText(unauth), unauth)
return
}
io.WriteString(w, "Goodbye, World!")
}
func main() {
http.HandleFunc("/", hw)
log.Fatal(http.ListenAndServeTLS(":8080", "cert.pem", "key.pem", nil))
}
It is a "Hello world" server, but over TLS and with basic authentication required on the Get. Errors are logged to aid client debugging.
The client:
package main
import (
"crypto/tls"
"crypto/x509"
"fmt"
"io/ioutil"
"log"
"net/http"
)
const (
userid = "rosetta"
password = "code"
)
func main() {
// Use custom certificate for testing. Not exactly required by task.
b, err := ioutil.ReadFile("cert.pem")
if err != nil {
log.Fatal(err)
}
pool := x509.NewCertPool()
if ok := pool.AppendCertsFromPEM(b); !ok {
log.Fatal("Failed to append cert")
}
tc := &tls.Config{RootCAs: pool}
tr := &http.Transport{TLSClientConfig: tc}
client := &http.Client{Transport: tr}
req, err := http.NewRequest("GET", "https://127.0.0.1:8080", nil)
if err != nil {
log.Fatal(err)
}
// This one line implements the authentication required for the task.
req.SetBasicAuth(userid, password)
// Make request and show output.
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
b, err = ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
log.Fatal(err)
}
fmt.Println(string(b))
}
Haskell
Example uses the req and aeson packages:
{-# LANGUAGE OverloadedStrings #-}
module Main (main) where
import Data.Aeson (Value)
import Data.Default.Class (def)
import Network.HTTP.Req
( (/:)
, GET(..)
, NoReqBody(..)
, basicAuth
, https
, jsonResponse
, req
, responseBody
, runReq
)
main :: IO ()
main = do
response <- runReq def $ req
GET
(https "httpbin.org" /: "basic-auth" /: "someuser" /: "somepassword")
NoReqBody
jsonResponse
(basicAuth "someuser" "somepassword")
print (responseBody response :: Value)
Java
import java.io.IOException;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandlers;
public final class HTTPSAuthenticated {
public static void main(String[] aArgs) throws IOException, InterruptedException, URISyntaxException {
HttpClient client = HttpClient.newBuilder()
.authenticator( new MyAuthenticator() )
.build();
HttpRequest request = HttpRequest.newBuilder()
.GET()
.uri( new URI("https://postman-echo.com/basic-auth") ) // This website requires authentication
.build();
HttpResponse<String> response = client.send(request, BodyHandlers.ofString());
System.out.println("Status: " + response.statusCode());
}
}
final class MyAuthenticator extends Authenticator {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
String username = "kingkong";
String password = "test1234";
return new PasswordAuthentication(username, password.toCharArray());
}
}
- Output:
Status: 200
Julia
An example using HTTP (see the source for HTTP.jl for the code below ) to access and play a song:
using HTTP, HTTP.IOExtras, JSON, MusicProcessing
HTTP.open("POST", "http://music.com/play") do io
write(io, JSON.json([
"auth" => "12345XXXX",
"song_id" => 7,
]))
r = startread(io)
@show r.status
while !eof(io)
bytes = readavailable(io)
play(bytes)
end
end
Kotlin
// version 1.2.0
import java.net.Authenticator
import java.net.PasswordAuthentication
import javax.net.ssl.HttpsURLConnection
import java.net.URL
import java.io.InputStreamReader
import java.io.BufferedReader
object PasswordAuthenticator : Authenticator() {
override fun getPasswordAuthentication() =
PasswordAuthentication ("username", "password".toCharArray())
}
fun main(args: Array<String>) {
val url = URL("https://somehost.com")
val con = url.openConnection() as HttpsURLConnection
Authenticator.setDefault(PasswordAuthenticator)
con.allowUserInteraction = true
con.connect()
val isr = InputStreamReader(con.inputStream)
val br = BufferedReader(isr)
while (true) {
val line = br.readLine()
if (line == null) break
println(line)
}
}
Lasso
local(username = 'hello',password = 'world')
local(x = curl('https://sourceforge.net'))
#x->set(CURLOPT_USERPWD, #username + ':' + #password)
local(y = #x->result)
#y->asString
LiveCode
HTTP Basic Auth as part of url
command getAuthWebResource
libURLFollowHttpRedirects true
libURLSetSSLVerification true
put URL "https://user:passwd@example.basicauth.com/" into response
put response
end getAuthWebResource
You can also set the headers for the basic auth requests
command getAuthWebResource
libURLFollowHttpRedirects true
libURLSetSSLVerification true
set the httpHeaders to "Authorization: Basic " && base64Encode("user:passwd")
put URL "https://example.basicauth.com" into response
put response
end getAuthWebResource
Lua
local requests = require('requests')
local auth = requests.HTTPBasicAuth('admin', 'admin')
local resp, e = requests.get({
url = 'https://httpbin.org/basic-auth/admin/admin',
auth = auth
})
io.write(string.format('Status: %d', resp.status_code))
- Output:
Status: 200
Mathematica / Wolfram Language
a = RunThrough["curl -u JohnDoe:Password https://www.example.com", 1]
For[ i=0, i < Length[a] , i++, SomeFunction[a]]
Nim
Compile with command nim c -d:ssl https_authenticated.nim
.
import httpclient, base64
const
User = "admin"
Password = "admin"
let headers = newHttpHeaders({"Authorization": "Basic " & base64.encode(User & ":" & Password)})
let client = newHttpClient(headers = headers)
echo client.getContent("https://httpbin.org/basic-auth/admin/admin")
Perl
use LWP::UserAgent qw();
my $ua = LWP::UserAgent->new;
my $netloc = 'http://www.buddhism-dict.net/cgi-bin/xpr-dealt.pl:80';
$ua->credentials(
$netloc,
'CJK-E and Buddhist Dictionaries', # basic realm
'guest', # user
'', # empty pw
);
my $response = $ua->get($netloc);
use WWW::Mechanize qw();
my $mech = WWW::Mechanize->new;
$mech->get('https://login.yahoo.com/');
$mech->submit_form(with_fields => {
login => 'XXXXXX',
passwd => 'YYYYYY',
'.persistent' => 'y', # tick checkbox
});
Phix
Exactly the same as the HTTP#Phix task. You can of course use curl_easy_setopt(curl,CURLOPT_USERPWD,"user:password") rather than embed that in the url.
without js include builtins\libcurl.e curl_global_init() atom curl = curl_easy_init() curl_easy_setopt(curl, CURLOPT_URL, "https://user:password@example.com/") object res = curl_easy_perform_ex(curl) curl_easy_cleanup(curl) curl_global_cleanup() puts(1,res)
PicoLisp
(let (User "Bill" Pass "T0p5ecRet" Url "https://www.example.com")
(in (list 'curl "-u" (pack User ': Pass) Url)
(while (line)
(doSomeProcessingWithLine @) ) ) )
PowerShell
$client = [Net.WebClient]::new()
# credentials of current user:
$client.Credentials = [Net.CredentialCache]::DefaultCredentials
# or specify credentials manually:
# $client.Credentials = [System.Net.NetworkCredential]::new("User", "Password")
$data = $client.DownloadString("https://example.com")
Write-Host $data
Python
Note: You should install mechanize to run code below. Visit: http://wwwsearch.sourceforge.net/mechanize/
#!/usr/bin/python
# -*- coding: utf-8 -*-
from mechanize import Browser
USER_AGENT = "Mozilla/5.0 (X11; U; Linux i686; tr-TR; rv:1.8.1.9) Gecko/20071102 Pardus/2007 Firefox/2.0.0.9"
br = Browser()
br.addheaders = [("User-agent", USER_AGENT)]
# remove comment if you get debug output
# br.set_debug_redirects(True)
# br.set_debug_responses(True)
# br.set_debug_http(True)
br.open("https://www.facebook.com")
br.select_form("loginform")
br['email'] = "xxxxxxx@xxxxx.com"
br['pass'] = "xxxxxxxxx"
br['persistent'] = ["1"]
response = br.submit()
print response.read()
import requests
username = "user"
password = "pass"
url = "https://www.example.com"
response = requests.get(url, auth=(username, password)
print(response.text)
Racket
#lang racket
(require net/url net/url-connect openssl)
(module+ main
(parameterize ([current-https-protocol (ssl-make-client-context)])
(ssl-set-verify! (current-https-protocol) #t)
;; When this is #f, we correctly get an exception:
;; error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed
(when #t
(ssl-load-verify-source! (current-https-protocol)
'(directory
;; This location works on Debian 6;
;; adjust as needed for your platform.
"/etc/ssl/certs"
)))
(for ([l (in-port read-line (get-pure-port (string->url "https://www.google.com/")))])
(displayln l))))
Raku
(formerly Perl 6)
Used here to connect to my local wireless router to a page that is password protected. Obviously not going to be generally publicly accessible but should be easily adaptable to other sites / devices.
use HTTP::UserAgent;
my $username = 'username'; # my username
my $password = 'password'; # my password
my $address = 'http://192.168.1.1/Status_Router.asp'; # my local wireless router
my $ua = HTTP::UserAgent.new;
$ua.auth( $username, $password );
my $response = $ua.get: $address;
say $response.is-success ?? $response.content !! $response.status-line;
Ruby
require 'uri'
require 'net/http'
uri = URI.parse('https://www.example.com')
response = Net::HTTP.start(uri.host, uri.port, use_ssl: true) do |http|
request = Net::HTTP::Get.new uri
request.basic_auth('username', 'password')
http.request request
end
Run BASIC
html "
<CENTER><TABLE CELLPADDING=0 CELLSPACING=0 border=1 bgcolor=wheat>
<TR><TD colspan=2 bgcolor=tan align=center>LOGIN</TD></TR>
<TR><TD align=right>UserName</TD><TD>"
TEXTBOX #userName, ""
html "</TR></TD><TR><TD align=right>Password:</TD><TD>"
PasswordBox #passWord, ""
html "</TD></TR><TD align=center colspan=2>"
button #si, "Signin", [doSignin]
html " "
button #ex, "Exit", [exit]
html "</TD></TR></TABLE>"
WAIT
[doSignin]
loginUserName$ = trim$(#userName contents$())
loginPassWord$ = trim$(#passWord contents$())
if (loginUserName$ = "admin" and loginPassWord$ = "admin" then
print "Login ok"
else
print "invalid User or Pass"
cls
goto [loop]
end if
print Platform$ ' OS where Run BASIC is being hosted
print UserInfo$ ' Information about the user's web browser
print UserAddress$ ' IP address of the user
[exit]
end
Rust
extern crate reqwest;
use reqwest::blocking::Client;
use reqwest::header::CONNECTION;
fn main() {
let client = Client::new();
// reqwest uses strongly-typed structs for creating headers
let res = client
.get("https://www.example.com")
.basic_auth("user", Some("password"))
.header(CONNECTION, "close")
.send()
.unwrap();
let body = res.text().unwrap();
println!("{}", body);
}
- Output:
Scala
import java.net.{Authenticator, PasswordAuthentication, URL}
import javax.net.ssl.HttpsURLConnection
import scala.io.BufferedSource
object Authenticated extends App {
val con: HttpsURLConnection =
new URL("https://somehost.com").openConnection().asInstanceOf[HttpsURLConnection]
object PasswordAuthenticator extends Authenticator {
override def getPasswordAuthentication =
new PasswordAuthentication("username", "password".toCharArray)
}
Authenticator.setDefault(PasswordAuthenticator)
con.setAllowUserInteraction(true)
con.connect()
new BufferedSource(con.getInputStream).getLines.foreach(println(_))
}
Sidef
require('WWW::Mechanize')
var mech = %s'WWW::Mechanize'.new(
cookie_jar => Hash.new,
agent => 'Mozilla/5.0',
)
mech.get('https://login.yahoo.com/')
mech.submit_form(
form_id => 'mbr-login-form', # form id
fields => Hash.new(
'login' => 'XXXXXX',
'passwd' => 'YYYYYY',
))
Tcl
for the binary encode
subcommand, otherwise uses
Uses the Tls package.
package require http
package require tls
http::register https 443 ::tls::socket
# Generate the authentication
set user theUser
set pass thePassword
dict set auth Authenticate "Basic [binary encode base64 ${user}:${pass}]"
# Make a secure authenticated connection
set token [http::geturl https://secure.example.com/ -headers $auth]
# Now as for conventional use of the “http” package
set data [http::data $token]
http::cleanup $token
Visual Basic
Sub Main()
' in the "references" dialog of the IDE, check
' "Microsoft WinHTTP Services, version 5.1" (winhttp.dll)
Dim HttpReq As WinHttp.WinHttpRequest
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 HTTPREQUEST_PROXYSETTING_PROXY As Long = 2
#Const USE_PROXY = 1
Set HttpReq = New WinHttp.WinHttpRequest
HttpReq.Open "GET", "https://www.abc.com/xyz/index.html"
HttpReq.Option(WinHttpRequestOption_SecureProtocols) = WINHTTP_FLAG_SECURE_PROTOCOL_TLS1 Or _
WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_1 Or _
WINHTTP_FLAG_SECURE_PROTOCOL_TLS1_2
HttpReq.SetCredentials "<username>", "<password>", 0&
#If USE_PROXY Then
HttpReq.SetProxy HTTPREQUEST_PROXYSETTING_PROXY, "10.167.1.1:80"
#End If
HttpReq.SetTimeouts 1000, 1000, 1000, 1000
HttpReq.Send
Debug.Print HttpReq.ResponseText
End Sub
Wren
An embedded program so we can ask the C host to communicate with libcurl for us.
/* HTTPS_Authenticated.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://user:password@secure.example.com/")
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_Authenticated.c -o HTTPS_Authenticated -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_Authenticated.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 cURL to do the heavy lifting, get a XML list of computers connected to my router.
zkl: var ZC=Import("zklCurl")
zkl: var data=ZC().get("http://usr:pw@192.168.1.1/computer_list.xml")
L(Data(1,049),121,0)
zkl: data[0][121,*].text
- Output:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <computers> <ip_address0>192.168.1.100</ip_address0><host_name0>core-shot ...
- Programming Tasks
- Networking and Web Interaction
- Arturo
- AutoHotkey
- Iweb
- COM
- BaCon
- C
- Libcurl
- C sharp
- Clojure
- Clj-http
- Delphi
- Go
- Haskell
- Java
- Julia
- Kotlin
- Lasso
- LiveCode
- Lua
- Lua-requests
- Mathematica
- Wolfram Language
- Nim
- OpenSSL
- Perl
- LWP
- Phix
- Phix/libcurl
- PicoLisp
- PowerShell
- Python
- Requests
- Racket
- Raku
- Ruby
- Run BASIC
- Rust
- Scala
- Sidef
- Tcl
- Tcllib
- Visual Basic
- Microsoft.WinHttp
- Wren
- Zkl
- Batch File/Omit
- Brainf***/Omit
- Commodore BASIC/Omit
- EasyLang/Omit
- Inform 7/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