Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
javab_20230928
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Patryk Czarnik
javab_20230928
Commits
2e948db5
Commit
2e948db5
authored
Nov 09, 2023
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Programy oparte o HttpClient
parent
170a4828
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
73 additions
and
2 deletions
+73
-2
.gitignore
PC36-RestKlient/.gitignore
+2
-0
Klient01_URL.java
PC36-RestKlient/src/main/java/rest_klient/Klient01_URL.java
+1
-2
Klient03_HttpClient.java
...Klient/src/main/java/rest_klient/Klient03_HttpClient.java
+36
-0
Klient04_HttpClient_String.java
...src/main/java/rest_klient/Klient04_HttpClient_String.java
+29
-0
Ustawienia.java
PC36-RestKlient/src/main/java/rest_klient/Ustawienia.java
+5
-0
No files found.
PC36-RestKlient/.gitignore
View file @
2e948db5
...
@@ -6,3 +6,5 @@
...
@@ -6,3 +6,5 @@
/*.iml
/*.iml
/.idea/
/.idea/
/wynik*.*
PC36-RestKlient/src/main/java/rest_klient/Klient01_URL.java
View file @
2e948db5
...
@@ -7,11 +7,10 @@ import java.net.URL;
...
@@ -7,11 +7,10 @@ import java.net.URL;
public
class
Klient01_URL
{
public
class
Klient01_URL
{
public
static
void
main
(
String
[]
args
)
{
public
static
void
main
(
String
[]
args
)
{
// Najprostszy sposób w Javie, aby pobra
c
dane z adresu URL, to użyć klasy URL.
// Najprostszy sposób w Javie, aby pobra
ć
dane z adresu URL, to użyć klasy URL.
try
{
try
{
URL
url
=
new
URL
(
"http://localhost:8080/PC35-RestSerwer/products.json"
);
URL
url
=
new
URL
(
"http://localhost:8080/PC35-RestSerwer/products.json"
);
System
.
out
.
println
(
"Odczytuję dane..."
);
System
.
out
.
println
(
"Odczytuję dane..."
);
try
(
InputStream
input
=
url
.
openStream
())
{
try
(
InputStream
input
=
url
.
openStream
())
{
// teraz z inputa możemy czytać ciąg bajtów
// teraz z inputa możemy czytać ciąg bajtów
// ja przerzucę bajty czytane z sieci bezpośrednio do System.out
// ja przerzucę bajty czytane z sieci bezpośrednio do System.out
...
...
PC36-RestKlient/src/main/java/rest_klient/Klient03_HttpClient.java
0 → 100644
View file @
2e948db5
package
rest_klient
;
import
java.io.IOException
;
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
;
import
java.nio.file.Path
;
import
java.nio.file.Paths
;
public
class
Klient03_HttpClient
{
/* W Java 11 pojawiło się rozwiązanie "HttpClient", które umożliwia komunikację HTTP z dużą kontrolą nad szczegółami.
* Wysyłając zapytanie, od razu trzeba podać odpowiedni "BodyHandler",
* który pozwoli nam odczytać treść odpowiedzi we właściwy dla nas sposób.
*
* W tej wersji wynik jest zapisywany do pliku.
*/
public
static
void
main
(
String
[]
args
)
{
HttpClient
httpClient
=
HttpClient
.
newHttpClient
();
try
{
URI
uri
=
new
URI
(
Ustawienia
.
ADRES_USLUGI
+
"/products.json"
);
HttpRequest
request
=
HttpRequest
.
newBuilder
(
uri
).
build
();
HttpResponse
<
Path
>
response
=
httpClient
.
send
(
request
,
BodyHandlers
.
ofFile
(
Paths
.
get
(
"wynik03.json"
)));
System
.
out
.
println
(
"response "
+
response
);
System
.
out
.
println
(
"status: "
+
response
.
statusCode
());
System
.
out
.
println
(
"Content-Type: "
+
response
.
headers
().
firstValue
(
"Content-Type"
).
orElse
(
"BRAK"
));
System
.
out
.
println
(
"OK, zapisany plik: "
+
response
.
body
());
}
catch
(
URISyntaxException
|
IOException
|
InterruptedException
e
)
{
e
.
printStackTrace
();
}
}
}
PC36-RestKlient/src/main/java/rest_klient/Klient04_HttpClient_String.java
0 → 100644
View file @
2e948db5
package
rest_klient
;
import
java.io.IOException
;
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
class
Klient04_HttpClient_String
{
public
static
void
main
(
String
[]
args
)
{
HttpClient
httpClient
=
HttpClient
.
newHttpClient
();
try
{
URI
uri
=
new
URI
(
Ustawienia
.
ADRES_USLUGI
+
"/products.json"
);
HttpRequest
request
=
HttpRequest
.
newBuilder
(
uri
).
build
();
// Body z odpowiedzi pobierzemy jako obiekt String z całą treścią
HttpResponse
<
String
>
response
=
httpClient
.
send
(
request
,
BodyHandlers
.
ofString
());
System
.
out
.
println
(
"response "
+
response
);
System
.
out
.
println
(
"status: "
+
response
.
statusCode
());
System
.
out
.
println
(
"Content-Type: "
+
response
.
headers
().
firstValue
(
"Content-Type"
).
orElse
(
"BRAK"
));
System
.
out
.
println
(
"Treść odpowiedzi:\n"
+
response
.
body
());
// tutaj
}
catch
(
URISyntaxException
|
IOException
|
InterruptedException
e
)
{
e
.
printStackTrace
();
}
}
}
PC36-RestKlient/src/main/java/rest_klient/Ustawienia.java
0 → 100644
View file @
2e948db5
package
rest_klient
;
public
class
Ustawienia
{
public
static
final
String
ADRES_USLUGI
=
"http://localhost:8080/PC35-RestSerwer"
;
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment