Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
javab_20230617
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_20230617
Commits
9a941116
Commit
9a941116
authored
Aug 20, 2023
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Zapytania HTTP za pomocą technik Java SE
parent
88ce6720
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
99 additions
and
0 deletions
+99
-0
.gitignore
PC33-RestKlient/.gitignore
+3
-0
Klient01_URL.java
PC33-RestKlient/src/main/java/sklep/klient/Klient01_URL.java
+26
-0
Klient03_HttpClient.java
...lient/src/main/java/sklep/klient/Klient03_HttpClient.java
+36
-0
Klient04_HttpClient_String.java
...rc/main/java/sklep/klient/Klient04_HttpClient_String.java
+34
-0
No files found.
PC33-RestKlient/.gitignore
View file @
9a941116
...
...
@@ -6,3 +6,6 @@
/*.iml
/.idea/
/wynik*
PC33-RestKlient/src/main/java/sklep/klient/Klient01_URL.java
0 → 100644
View file @
9a941116
package
sklep
.
klient
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.net.URL
;
import
java.nio.file.Files
;
import
java.nio.file.Paths
;
import
java.nio.file.StandardCopyOption
;
public
class
Klient01_URL
{
// W najprostszych sytuacjach "zasób" z internetu można pobrać za pomocą klasy URL.
// Tutaj wynik zapytania zapisujemy do pliku.
public
static
void
main
(
String
[]
args
)
{
try
{
URL
url
=
new
URL
(
"http://localhost:8080/PC32-RestSerwer/products.json"
);
try
(
InputStream
inputStream
=
url
.
openStream
())
{
Files
.
copy
(
inputStream
,
Paths
.
get
(
"wynik01.json"
),
StandardCopyOption
.
REPLACE_EXISTING
);
System
.
out
.
println
(
"OK"
);
}
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
}
PC33-RestKlient/src/main/java/sklep/klient/Klient03_HttpClient.java
0 → 100644
View file @
9a941116
package
sklep
.
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
(
"http://localhost:8080/PC32-RestSerwer/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
();
}
}
}
PC33-RestKlient/src/main/java/sklep/klient/Klient04_HttpClient_String.java
0 → 100644
View file @
9a941116
package
sklep
.
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
{
/* 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 pobierany jako String.
*/
public
static
void
main
(
String
[]
args
)
{
HttpClient
httpClient
=
HttpClient
.
newHttpClient
();
try
{
URI
uri
=
new
URI
(
"http://localhost:8080/PC32-RestSerwer/products.json"
);
HttpRequest
request
=
HttpRequest
.
newBuilder
(
uri
).
build
();
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
());
}
catch
(
URISyntaxException
|
IOException
|
InterruptedException
e
)
{
e
.
printStackTrace
();
}
}
}
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