Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
C
Curl
Project
Project
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
9831006
Curl
Commits
f243ea99
Commit
f243ea99
authored
Jun 01, 2020
by
kiana
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
first commit
parents
Pipeline
#4987
failed with stages
Changes
3
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
223 additions
and
0 deletions
+223
-0
GetResponse.java
src/GetResponse.java
+116
-0
Main.java
src/Main.java
+11
-0
Response.java
src/Response.java
+96
-0
No files found.
src/GetResponse.java
0 → 100644
View file @
f243ea99
import
javax.imageio.IIOException
;
import
java.io.IOException
;
import
java.net.HttpURLConnection
;
import
java.net.ProtocolException
;
import
java.net.URL
;
import
java.util.HashMap
;
import
java.util.Scanner
;
import
java.nio.file.Files
;
import
java.io.File
;
import
java.io.FileWriter
;
import
java.net.URLEncoder
;
import
java.util.Map
;
public
class
GetResponse
{
private
static
final
String
USER_AGENT
=
"Mozilla/5.0"
;
private
static
final
String
GET_URL
=
"https://localhost:9090/SpringMVCExample"
;
private
static
final
String
POST_URL
=
"https://localhost:9090/SpringMVCExample/home"
;
private
static
final
String
POST_PARAMS
=
"userName=Pankaj"
;
Map
<
String
,
String
>
params
=
new
HashMap
<>();
//params.put("v", "dQw4w9WgXcQ");
String
requestMethod
;
String
headers
;
Boolean
invisible
;
String
help
;
String
arg
;
String
address
;
String
completeRequest
;
String
content
;
Scanner
scanner
=
new
Scanner
(
System
.
in
);
public
GetResponse
(){
splitRequest
();
try
{
response
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
public
String
[]
splitRequest
()
{
completeRequest
=
scanner
.
next
();
String
[]
splittedRequest
=
completeRequest
.
split
(
"\\s"
);
address
=
splittedRequest
[
0
];
return
splittedRequest
;
}
public
void
response
()
throws
IOException
{
URL
obj
=
new
URL
(
GET_URL
);
HttpURLConnection
con
=
(
HttpURLConnection
)
obj
.
openConnection
();
con
.
setRequestMethod
(
"GET"
);
con
.
setRequestProperty
(
"User-Agent"
,
USER_AGENT
);
int
responseCode
=
con
.
getResponseCode
();
System
.
out
.
println
(
"GET Response Code :: "
+
responseCode
);
StringBuilder
requestData
=
new
StringBuilder
();
for
(
Map
.
Entry
<
String
,
String
>
param
:
params
.
entrySet
())
{
if
(
requestData
.
length
()
!=
0
)
{
requestData
.
append
(
'&'
);
}
// Encode the parameter based on the parameter map we've defined
// and append the values from the map to form a single parameter
requestData
.
append
(
URLEncoder
.
encode
(
param
.
getKey
(),
"UTF-8"
));
requestData
.
append
(
'='
);
requestData
.
append
(
URLEncoder
.
encode
(
String
.
valueOf
(
param
.
getValue
()),
"UTF-8"
));
}
// Convert the requestData into bytes
byte
[]
requestDataByes
=
requestData
.
toString
().
getBytes
(
"UTF-8"
);
String
[]
splittedRequestCopy
=
splitRequest
();
for
(
int
i
=
0
;
i
<
splittedRequestCopy
.
length
-
1
;
i
++)
{
if
(
splittedRequestCopy
[
i
].
equals
(
"-M"
)
||
splittedRequestCopy
[
i
].
equals
(
"--method"
))
{
if
(
splittedRequestCopy
[
i
+
1
].
equals
(
"GET"
)
||
splittedRequestCopy
[
i
+
1
].
equals
(
"POST"
)
||
splittedRequestCopy
[
i
+
1
].
equals
(
"HEAD"
)
||
splittedRequestCopy
[
i
+
1
].
equals
(
"DELETE"
)
||
splittedRequestCopy
[
i
+
1
].
equals
(
"PUT"
)
||
splittedRequestCopy
[
i
+
1
].
equals
(
"CONNECT"
)
||
splittedRequestCopy
[
i
+
1
].
equals
(
"OPTIONS"
)
||
splittedRequestCopy
[
i
+
1
].
equals
(
"TRACE"
)
||
splittedRequestCopy
[
i
+
1
].
equals
(
"PATCH"
))
con
.
setRequestMethod
(
splittedRequestCopy
[
i
+
1
]);
}
else
if
(
splittedRequestCopy
[
i
].
equals
(
"-H"
)
||
splittedRequestCopy
[
i
].
equals
(
"--header"
))
{
con
.
setRequestProperty
(
"Content-Type"
,
"application/json"
);
con
.
setRequestProperty
(
"Content-Length"
,
String
.
valueOf
(
requestDataByes
.
length
));
}
else
if
(
splittedRequestCopy
[
i
].
equals
(
"-i"
))
{
invisible
=
true
;
}
else
if
(
splittedRequestCopy
[
i
].
equals
(
"-O"
)
||
splittedRequestCopy
[
i
].
equals
(
"--output"
))
{
try
{
content
=
con
.
getResponseMessage
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
try
{
File
newTextFile
=
new
File
(
"C:/thetextfile.txt"
);
FileWriter
fw
=
new
FileWriter
(
newTextFile
);
fw
.
write
(
content
);
fw
.
close
();
}
catch
(
IOException
iox
)
{
iox
.
printStackTrace
();
}
}
content
=
con
.
getResponseMessage
();
System
.
out
.
println
(
content
);
int
code
=
con
.
getResponseCode
();
System
.
out
.
println
(
code
);
}
}
}
src/Main.java
0 → 100644
View file @
f243ea99
import
java.io.IOException
;
public
class
Main
{
public
static
void
main
(
String
[]
args
){
// GetResponse getResponse = new GetResponse();
// getResponse.splitRequest();
// Response response = new Response();
GetResponse
response
=
new
GetResponse
();
}
}
src/Response.java
0 → 100644
View file @
f243ea99
import
java.io.BufferedReader
;
import
java.io.IOException
;
import
java.io.InputStreamReader
;
import
java.io.OutputStream
;
import
java.net.HttpURLConnection
;
import
java.net.URL
;
public
class
Response
{
private
static
final
String
USER_AGENT
=
"Mozilla/5.0"
;
private
static
final
String
GET_URL
=
"https://localhost:9090/SpringMVCExample"
;
private
static
final
String
POST_URL
=
"https://localhost:9090/SpringMVCExample/home"
;
private
static
final
String
POST_PARAMS
=
"userName=Pankaj"
;
public
Response
(){
try
{
sendGET
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
System
.
out
.
println
(
"GET DONE"
);
try
{
sendPOST
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
System
.
out
.
println
(
"POST DONE"
);
}
private
static
void
sendGET
()
throws
IOException
{
URL
obj
=
new
URL
(
GET_URL
);
HttpURLConnection
con
=
(
HttpURLConnection
)
obj
.
openConnection
();
con
.
setRequestMethod
(
"GET"
);
con
.
setRequestProperty
(
"User-Agent"
,
USER_AGENT
);
int
responseCode
=
con
.
getResponseCode
();
System
.
out
.
println
(
"GET Response Code :: "
+
responseCode
);
if
(
responseCode
==
HttpURLConnection
.
HTTP_OK
)
{
// success
BufferedReader
in
=
new
BufferedReader
(
new
InputStreamReader
(
con
.
getInputStream
()));
String
inputLine
;
StringBuffer
response
=
new
StringBuffer
();
while
((
inputLine
=
in
.
readLine
())
!=
null
)
{
response
.
append
(
inputLine
);
}
in
.
close
();
// print result
System
.
out
.
println
(
response
.
toString
());
}
else
{
System
.
out
.
println
(
"GET request not worked"
);
}
}
private
static
void
sendPOST
()
throws
IOException
{
URL
obj
=
new
URL
(
POST_URL
);
HttpURLConnection
con
=
(
HttpURLConnection
)
obj
.
openConnection
();
con
.
setRequestMethod
(
"POST"
);
con
.
setRequestProperty
(
"User-Agent"
,
USER_AGENT
);
// For POST only - START
con
.
setDoOutput
(
true
);
OutputStream
os
=
con
.
getOutputStream
();
os
.
write
(
POST_PARAMS
.
getBytes
());
os
.
flush
();
os
.
close
();
// For POST only - END
int
responseCode
=
con
.
getResponseCode
();
System
.
out
.
println
(
"POST Response Code :: "
+
responseCode
);
if
(
responseCode
==
HttpURLConnection
.
HTTP_OK
)
{
//success
BufferedReader
in
=
new
BufferedReader
(
new
InputStreamReader
(
con
.
getInputStream
()));
String
inputLine
;
StringBuffer
response
=
new
StringBuffer
();
while
((
inputLine
=
in
.
readLine
())
!=
null
)
{
response
.
append
(
inputLine
);
}
in
.
close
();
// print result
System
.
out
.
println
(
response
.
toString
());
}
else
{
System
.
out
.
println
(
"POST request not worked"
);
}
}
}
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