Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
2
20230403
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
20230403
Commits
c1a40b5e
Commit
c1a40b5e
authored
May 25, 2023
by
Patryk Czarnik
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Klienty HTTP zawarte w Java SE
parent
1c61ea56
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
59 additions
and
0 deletions
+59
-0
.gitignore
PC34-RestKlient/.gitignore
+2
-0
P01_Url.java
PC34-RestKlient/src/main/java/sklep/klient_rest/P01_Url.java
+24
-0
P03_HttpClient.java
...lient/src/main/java/sklep/klient_rest/P03_HttpClient.java
+33
-0
No files found.
PC34-RestKlient/.gitignore
View file @
c1a40b5e
...
...
@@ -6,3 +6,5 @@
/*.iml
/.idea/
/wynik*
PC34-RestKlient/src/main/java/sklep/klient_rest/P01_Url.java
0 → 100644
View file @
c1a40b5e
package
sklep
.
klient_rest
;
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
P01_Url
{
public
static
void
main
(
String
[]
args
)
{
try
{
URL
url
=
new
URL
(
"http://localhost:8080/PC33-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
();
}
}
}
PC34-RestKlient/src/main/java/sklep/klient_rest/P03_HttpClient.java
0 → 100644
View file @
c1a40b5e
package
sklep
.
klient_rest
;
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
P03_HttpClient
{
public
static
void
main
(
String
[]
args
)
{
HttpClient
httpClient
=
HttpClient
.
newHttpClient
();
try
{
URI
uri
=
new
URI
(
"http://localhost:8080/PC33-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"
));
String
body
=
response
.
body
();
System
.
out
.
println
(
"Rozmiar treści: "
+
body
.
length
());
System
.
out
.
println
(
"\nCała treść:\n"
);
System
.
out
.
println
(
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