HTTPS/Authenticated: Difference between revisions

m
syntax highlighting fixup automation
m (→‎{{header|Phix}}: added syntax colouring, marked p2js incompatible)
m (syntax highlighting fixup automation)
Line 7:
{{libheader|iweb}}
{{libheader|COM}}
<langsyntaxhighlight AutoHotkeylang="autohotkey">iWeb_Init()
pwb := iWeb_newGui(0, 0, 1000, 800)
iWeb_nav(pwb, "http://www.facebook.com/login.php?ref=pf")
Line 21:
#Include iweb.ahk
#Include COM.ahk
#Include COMinvokeDeep.ahk</langsyntaxhighlight>
 
=={{header|BaCon}}==
<langsyntaxhighlight lang="bacon">OPTION TLS TRUE
 
website$ = "website.com"
Line 40:
CLOSE NETWORK conn
 
PRINT total$</langsyntaxhighlight>
 
=={{header|C}}==
{{libheader|libcurl}}
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include "curl/curl.h"
Line 65:
}
return EXIT_SUCCESS;
}</langsyntaxhighlight>
 
=={{header|C sharp|C#}}==
{{works with|C sharp|3.0}}
 
<langsyntaxhighlight lang="csharp">
using System;
using System.Net;
Line 90:
}
}
</syntaxhighlight>
</lang>
 
=={{header|Clojure}}==
{{libheader|clj-http}}
 
<langsyntaxhighlight lang="clojure">(clj-http.client/get "https://somedomain.com"
{:basic-auth ["user" "pass"]})</langsyntaxhighlight>
 
=={{header|Delphi}}==
<langsyntaxhighlight Delphilang="delphi">program ShowHTTPSAuthenticated;
 
{$APPTYPE CONSOLE}
Line 124:
lIOHandler.Free;
end;
end.</langsyntaxhighlight>
 
=={{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:
<langsyntaxhighlight lang="go">package main
 
import (
Line 165:
http.HandleFunc("/", hw)
log.Fatal(http.ListenAndServeTLS(":8080", "cert.pem", "key.pem", nil))
}</langsyntaxhighlight>
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:
<langsyntaxhighlight lang="go">package main
 
import (
Line 217:
}
fmt.Println(string(b))
}</langsyntaxhighlight>
 
=={{header|Haskell}}==
Line 223:
Example uses the [https://hackage.haskell.org/package/req <tt>req</tt>] and [https://hackage.haskell.org/package/aeson <tt>aeson</tt>] packages:
 
<langsyntaxhighlight lang="haskell">{-# LANGUAGE OverloadedStrings #-}
 
module Main (main) where
Line 249:
jsonResponse
(basicAuth "someuser" "somepassword")
print (responseBody response :: Value)</langsyntaxhighlight>
 
=={{header|Julia}}==
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
HTTP.open("POST", "http://music.com/play") do io
Line 267:
end
end
</syntaxhighlight>
</lang>
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.2.0
 
import java.net.Authenticator
Line 297:
println(line)
}
}</langsyntaxhighlight>
 
=={{header|Lasso}}==
<langsyntaxhighlight Lassolang="lasso">local(username = 'hello',password = 'world')
local(x = curl('https://sourceforge.net'))
#x->set(CURLOPT_USERPWD, #username + ':' + #password)
local(y = #x->result)
#y->asString</langsyntaxhighlight>
 
=={{header|LiveCode}}==
HTTP Basic Auth as part of url
<langsyntaxhighlight LiveCodelang="livecode">command getAuthWebResource
libURLFollowHttpRedirects true
libURLSetSSLVerification true
put URL "https://user:passwd@example.basicauth.com/" into response
put response
end getAuthWebResource</langsyntaxhighlight>
 
You can also set the headers for the basic auth requests
<langsyntaxhighlight LiveCodelang="livecode">command getAuthWebResource
libURLFollowHttpRedirects true
libURLSetSSLVerification true
Line 322:
put URL "https://example.basicauth.com" into response
put response
end getAuthWebResource</langsyntaxhighlight>
 
=={{header|Lua}}==
Line 328:
{{libheader|lua-requests}}
 
<langsyntaxhighlight lang="lua">
local requests = require('requests')
local auth = requests.HTTPBasicAuth('admin', 'admin')
Line 336:
})
io.write(string.format('Status: %d', resp.status_code))
</syntaxhighlight>
</lang>
 
{{out}}
Line 346:
{{libheader|OpenSSL}}
 
<langsyntaxhighlight Nimlang="nim">import httpclient, base64
 
const
Line 354:
let headers = newHttpHeaders({"Authorization": "Basic " & base64.encode(User & ":" & Password)})
let client = newHttpClient(headers = headers)
echo client.getContent("https://httpbin.org/basic-auth/admin/admin")</langsyntaxhighlight>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">a = RunThrough["curl -u JohnDoe:Password https://www.example.com", 1]
For[ i=0, i < Length[a] , i++, SomeFunction[a]]</langsyntaxhighlight>
 
=={{header|Perl}}==
{{libheader|LWP}}
<langsyntaxhighlight lang="perl">use LWP::UserAgent qw();
my $ua = LWP::UserAgent->new;
my $netloc = 'http://www.buddhism-dict.net/cgi-bin/xpr-dealt.pl:80';
Line 380:
passwd => 'YYYYYY',
'.persistent' => 'y', # tick checkbox
});</langsyntaxhighlight>
 
=={{header|Phix}}==
Line 386:
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.
<!--<langsyntaxhighlight Phixlang="phix">(notonline)-->
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span>
<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>
Line 397:
<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>
<!--</langsyntaxhighlight>-->
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(let (User "Bill" Pass "T0p5ecRet" Url "https://www.example.com")
(in (list 'curl "-u" (pack User ': Pass) Url)
(while (line)
(doSomeProcessingWithLine @) ) ) )</langsyntaxhighlight>
=={{header|PowerShell}}==
{{trans|C#}}
<langsyntaxhighlight PowerShelllang="powershell">$client = [Net.WebClient]::new()
# credentials of current user:
$client.Credentials = [Net.CredentialCache]::DefaultCredentials
Line 412:
# $client.Credentials = [System.Net.NetworkCredential]::new("User", "Password")
$data = $client.DownloadString("https://example.com")
Write-Host $data</langsyntaxhighlight>
=={{header|Python}}==
{{works with|Python|2.4 and 2.6}}
Line 418:
'''Note:''' You should install '''''mechanize''''' to run code below. Visit: http://wwwsearch.sourceforge.net/mechanize/
 
<langsyntaxhighlight lang="python">#!/usr/bin/python
# -*- coding: utf-8 -*-
 
Line 441:
 
response = br.submit()
print response.read()</langsyntaxhighlight>
 
{{libheader|Requests}}
{{works with|Python|2.7, 3.4–3.7}}
<langsyntaxhighlight lang="python">import requests
 
username = "user"
Line 453:
response = requests.get(url, auth=(username, password)
 
print(response.text)</langsyntaxhighlight>
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">
#lang racket
 
Line 477:
(for ([l (in-port read-line (get-pure-port (string->url "https://www.google.com/")))])
(displayln l))))
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
Line 484:
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.
 
<syntaxhighlight lang="raku" perl6line>use HTTP::UserAgent;
 
my $username = 'username'; # my username
Line 493:
$ua.auth( $username, $password );
my $response = $ua.get: $address;
say $response.is-success ?? $response.content !! $response.status-line;</langsyntaxhighlight>
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">require 'uri'
require 'net/http'
 
Line 504:
request.basic_auth('username', 'password')
http.request request
end</langsyntaxhighlight>
 
=={{header|Run BASIC}}==
<langsyntaxhighlight lang="runbasic">html "
<CENTER><TABLE CELLPADDING=0 CELLSPACING=0 border=1 bgcolor=wheat>
<TR><TD colspan=2 bgcolor=tan align=center>LOGIN</TD></TR>
Line 538:
 
[exit]
end</langsyntaxhighlight>
[[File:ClientAuthorizationRunBasic.png]]
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">
extern crate reqwest;
 
Line 563:
println!("{}", body);
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 570:
 
=={{header|Scala}}==
<langsyntaxhighlight Scalalang="scala">import java.net.{Authenticator, PasswordAuthentication, URL}
 
import javax.net.ssl.HttpsURLConnection
Line 593:
new BufferedSource(con.getInputStream).getLines.foreach(println(_))
 
}</langsyntaxhighlight>
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">require('WWW::Mechanize')
 
var mech = %s'WWW::Mechanize'.new(
Line 609:
'login' => 'XXXXXX',
'passwd' => 'YYYYYY',
))</langsyntaxhighlight>
 
=={{header|Tcl}}==
Line 615:
{{tcllib|base64}}
Uses the [http://tls.sourceforge.net Tls] package.
<langsyntaxhighlight Tcllang="tcl">package require http
package require tls
http::register https 443 ::tls::socket
Line 629:
# Now as for conventional use of the “http” package
set data [http::data $token]
http::cleanup $token</langsyntaxhighlight>
 
=={{header|Visual Basic}}==
Line 638:
{{works with|VBA|6.5}}
{{works with|VBA|7.1}}
<langsyntaxhighlight lang="vb">Sub Main()
' in the "references" dialog of the IDE, check
' "Microsoft WinHTTP Services, version 5.1" (winhttp.dll)
Line 659:
HttpReq.Send
Debug.Print HttpReq.ResponseText
End Sub</langsyntaxhighlight>
 
=={{header|Wren}}==
Line 665:
{{libheader|libcurl}}
An embedded program so we can ask the C host to communicate with libcurl for us.
<langsyntaxhighlight lang="ecmascript">/* https_authenticated.wren */
 
var CURLOPT_URL = 10002
Line 695:
return
}
curl.easyCleanup()</langsyntaxhighlight>
<br>
We now embed this in the following C program, compile and run it.
<langsyntaxhighlight lang="c">/* gcc https_authenticated.c -o https_authenticated -lcurl -lwren -lm */
 
#include <stdio.h>
Line 824:
free(script);
return 0;
}</langsyntaxhighlight>
 
=={{header|zkl}}==
Using cURL to do the heavy lifting, get a XML list of computers connected to my router.
<langsyntaxhighlight lang="zkl">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</langsyntaxhighlight>
{{out}}
<pre>
10,327

edits