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
4aeae7c2
Commit
4aeae7c2
authored
Jun 08, 2020
by
kiana
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
saving and response to requests again is improved
parent
f243ea99
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
434 additions
and
171 deletions
+434
-171
Fire.java
src/Fire.java
+119
-0
GetResponse.java
src/GetResponse.java
+257
-75
Main.java
src/Main.java
+8
-4
Response.java
src/Response.java
+50
-92
No files found.
src/Fire.java
0 → 100644
View file @
4aeae7c2
import
java.io.*
;
import
java.net.HttpURLConnection
;
import
java.net.URL
;
import
java.nio.charset.StandardCharsets
;
import
java.nio.file.Files
;
import
java.nio.file.Paths
;
import
java.util.Random
;
import
java.util.stream.Stream
;
public
class
Fire
{
GetResponse
savedRequests
;
File
[]
files
;
public
Fire
()
{
}
public
void
fire
(
int
i
)
{
i
--;
if
(
files
[
i
].
getName
().
contains
(
".txt"
))
{
BufferedReader
reader
=
null
;
try
{
reader
=
new
BufferedReader
(
new
FileReader
(
files
[
i
]));
}
catch
(
FileNotFoundException
e
)
{
e
.
printStackTrace
();
}
String
content
=
null
;
try
{
content
=
reader
.
readLine
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
try
{
reader
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
//System.out.println(content);
savedRequests
=
new
GetResponse
(
content
);
}
}
public
void
showRequest
(){
int
count
=
0
;
for
(
File
file
:
files
)
{
if
(
file
.
getName
().
contains
(
".txt"
)
&&
file
.
getName
().
contains
(
".txt"
))
{
count
++;
System
.
out
.
println
();
System
.
out
.
print
(
count
+
" . "
);
BufferedReader
reader
=
null
;
try
{
reader
=
new
BufferedReader
(
new
FileReader
(
file
));
}
catch
(
FileNotFoundException
e
)
{
e
.
printStackTrace
();
}
String
content
=
null
;
try
{
content
=
reader
.
readLine
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
try
{
reader
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
//System.out.println(content);
String
[]
contentCopy
=
content
.
trim
().
split
(
" "
);
for
(
int
i
=
0
;
i
<
contentCopy
.
length
;
i
++)
{
if
(
contentCopy
[
i
].
contains
(
"http://"
))
{
System
.
out
.
print
(
"url: "
+
contentCopy
[
i
]
+
"| "
);
}
if
(
contentCopy
[
i
].
equals
(
"-M"
)
||
contentCopy
[
i
].
equals
(
"--method"
))
{
System
.
out
.
print
(
"method: "
+
contentCopy
[
i
+
1
]
+
"| "
);
}
else
if
(
contentCopy
[
i
].
equals
(
"-H"
)
||
contentCopy
[
i
].
equals
(
"--headers"
))
{
//setting headers
String
[]
keyValue
=
contentCopy
[
i
+
1
].
trim
().
split
(
";"
);
//splitting headers from each other
System
.
out
.
print
(
"headers: "
);
for
(
int
j
=
0
;
j
<
keyValue
.
length
;
j
++)
{
String
[]
keyValueCopy
=
keyValue
[
j
].
trim
().
split
(
":"
);
//splitting key and value
String
key
=
keyValueCopy
[
0
];
String
value
=
keyValueCopy
[
1
];
System
.
out
.
print
(
key
+
": "
+
value
+
"/"
);
}
i
++;
}
else
if
(
contentCopy
[
i
].
equals
(
"-i"
))
{
System
.
out
.
println
(
"include headers: true"
+
"| "
);
}
else
if
(
contentCopy
[
i
].
equals
(
"-O"
)
||
contentCopy
[
i
].
equals
(
"--output"
))
{
System
.
out
.
println
(
"output: true"
+
"| "
);
}
else
if
(
contentCopy
[
i
].
equals
(
"-d"
)
||
contentCopy
[
i
].
equals
(
"--data"
))
{
String
cont
=
contentCopy
[
i
+
1
];
System
.
out
.
print
(
"messageBody: "
+
cont
+
"| "
);
}
}
}
}
System
.
out
.
println
();
}
}
\ No newline at end of file
src/GetResponse.java
View file @
4aeae7c2
import
javax.imageio.IIOException
;
import
javax.imageio.IIOException
;
import
java.io.IOException
;
import
java.io.*
;
import
java.net.HttpURLConnection
;
import
java.net.*
;
import
java.net.ProtocolException
;
import
java.net.http.HttpHeaders
;
import
java.net.URL
;
import
java.util.HashMap
;
import
java.util.HashMap
;
import
java.util.Scanner
;
import
java.util.Scanner
;
import
java.nio.file.Files
;
import
java.nio.file.Files
;
import
java.io.File
;
import
java.io.FileWriter
;
import
java.net.URLEncoder
;
import
java.util.Map
;
import
java.util.Map
;
import
java.io.*
;
import
javax.servlet.*
;
import
javax.servlet.http.*
;
import
java.util.*
;
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"
;
public
class
GetResponse
{
Map
<
String
,
String
>
params
=
new
HashMap
<>();
Fire
fire
=
new
Fire
();
//params.put("v", "dQw4w9WgXcQ");
Boolean
include
=
false
;
String
requestMethod
;
String
headers
;
Boolean
invisible
;
String
help
;
String
help
;
String
arg
;
String
arg
;
String
address
;
String
address
;
String
completeRequest
;
String
completeRequest
;
String
content
;
String
content
;
Scanner
scanner
=
new
Scanner
(
System
.
in
);
Scanner
scanner
=
new
Scanner
(
System
.
in
);
HttpURLConnection
con
;
URL
obj1
;
public
GetResponse
()
{
String
[]
copy
=
splitRequest
();
try
{
response
(
copy
);
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
public
GetResponse
(){
public
GetResponse
(
String
requestAgain
){
splitRequest
();
completeRequest
=
requestAgain
;
String
[]
copy
=
requestAgain
.
trim
().
split
(
" "
);
address
=
copy
[
0
];
try
{
try
{
response
();
response
(
copy
);
}
catch
(
IOException
e
)
{
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
e
.
printStackTrace
();
}
}
}
}
public
String
[]
splitRequest
()
{
public
String
[]
splitRequest
()
{
completeRequest
=
scanner
.
next
();
completeRequest
=
scanner
.
nextLine
();
String
[]
splittedRequest
=
completeRequest
.
split
(
"\\s"
);
String
[]
splitRequest
=
completeRequest
.
trim
().
split
(
" "
);
address
=
splittedRequest
[
0
];
address
=
splitRequest
[
0
];
return
splittedRequest
;
for
(
String
test:
splitRequest
)
{
System
.
out
.
println
(
test
);
}
return
splitRequest
;
}
public
void
save
(
String
fileName
,
String
str
)
throws
IOException
{
System
.
out
.
println
(
str
);
File
file
=
new
File
(
fileName
);
BufferedWriter
writer
=
new
BufferedWriter
(
new
FileWriter
(
file
));
writer
.
write
(
str
);
writer
.
close
();
File
temp
=
File
.
createTempFile
(
"file"
,
".txt"
,
file
.
getParentFile
());
String
delete
=
"-S"
;
String
deleteSave
=
"--save"
;
String
charset
=
"UTF-8"
;
BufferedReader
reader
=
new
BufferedReader
(
new
InputStreamReader
(
new
FileInputStream
(
file
),
charset
));
PrintWriter
writerForDelete
=
new
PrintWriter
(
new
OutputStreamWriter
(
new
FileOutputStream
(
temp
),
charset
));
for
(
String
line
;
(
line
=
reader
.
readLine
())
!=
null
;
)
{
line
=
line
.
replace
(
delete
,
""
);
line
=
line
.
replace
(
deleteSave
,
""
);
writerForDelete
.
println
(
line
);
}
reader
.
close
();
writerForDelete
.
close
();
file
.
delete
();
temp
.
renameTo
(
file
);
}
public
void
responseHeader
(){
con
.
getHeaderField
(
2
);
}
}
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
())
{
public
void
savedList
(){
if
(
requestData
.
length
()
!=
0
)
{
// try-catch block to handle exceptions
requestData
.
append
(
'&'
);
try
{
File
file
=
new
File
(
"/Users/kiana/Courses/2nd Semester/javva projects/CurlInsomnia"
);
fire
.
files
=
file
.
listFiles
();
fire
.
showRequest
();
}
catch
(
Exception
e
)
{
System
.
err
.
println
(
e
.
getMessage
());
}
}
public
boolean
isMethod
(
String
test
){
return
test
.
equals
(
"GET"
)
||
test
.
equals
(
"POST"
)
||
test
.
equals
(
"HEAD"
)
||
test
.
equals
(
"DELETE"
)
||
test
.
equals
(
"PUT"
)
||
test
.
equals
(
"CONNECT"
)
||
test
.
equals
(
"OPTIONS"
)
||
test
.
equals
(
"TRACE"
)
||
test
.
equals
(
"PATCH"
);
}
public
HttpURLConnection
connect
(
URL
url
,
String
requestType
,
HttpURLConnection
connection
)
{
try
{
URLConnection
urlConnection
=
url
.
openConnection
();
if
(
urlConnection
instanceof
HttpURLConnection
)
{
connection
=
(
HttpURLConnection
)
urlConnection
;
connection
.
setRequestMethod
(
requestType
);
}
}
// Encode the parameter based on the parameter map we've defined
return
connection
;
// and append the values from the map to form a single parameter
requestData
.
append
(
URLEncoder
.
encode
(
param
.
getKey
(),
"UTF-8"
));
}
catch
(
IOException
e
)
{
requestData
.
append
(
'='
);
System
.
out
.
println
(
"E"
+
e
.
getMessage
()
);
re
questData
.
append
(
URLEncoder
.
encode
(
String
.
valueOf
(
param
.
getValue
()),
"UTF-8"
))
;
re
turn
null
;
}
}
}
// Convert the requestData into bytes
public
void
post
(
HttpURLConnection
conn
,
String
content
){
byte
[]
requestDataByes
=
requestData
.
toString
().
getBytes
(
"UTF-8"
);
conn
.
setDoOutput
(
true
);
OutputStream
os
=
null
;
try
{
String
[]
splittedRequestCopy
=
splitRequest
();
os
=
conn
.
getOutputStream
();
for
(
int
i
=
0
;
i
<
splittedRequestCopy
.
length
-
1
;
i
++)
{
}
catch
(
IOException
e
)
{
if
(
splittedRequestCopy
[
i
].
equals
(
"-M"
)
||
splittedRequestCopy
[
i
].
equals
(
"--method"
))
{
e
.
printStackTrace
();
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"
)
OutputStreamWriter
osw
=
null
;
||
splittedRequestCopy
[
i
+
1
].
equals
(
"OPTIONS"
)
||
splittedRequestCopy
[
i
+
1
].
equals
(
"TRACE"
)
||
splittedRequestCopy
[
i
+
1
].
equals
(
"PATCH"
))
try
{
con
.
setRequestMethod
(
splittedRequestCopy
[
i
+
1
]);
osw
=
new
OutputStreamWriter
(
os
,
"UTF-8"
);
}
else
if
(
splittedRequestCopy
[
i
].
equals
(
"-H"
)
||
splittedRequestCopy
[
i
].
equals
(
"--header"
))
{
}
catch
(
UnsupportedEncodingException
e
)
{
con
.
setRequestProperty
(
"Content-Type"
,
"application/json"
);
e
.
printStackTrace
();
con
.
setRequestProperty
(
"Content-Length"
,
String
.
valueOf
(
requestDataByes
.
length
));
}
try
{
}
else
if
(
splittedRequestCopy
[
i
].
equals
(
"-i"
))
{
osw
.
write
(
content
);
invisible
=
true
;
}
catch
(
IOException
e
)
{
}
else
if
(
splittedRequestCopy
[
i
].
equals
(
"-O"
)
||
splittedRequestCopy
[
i
].
equals
(
"--output"
))
{
e
.
printStackTrace
();
try
{
}
content
=
con
.
getResponseMessage
();
try
{
}
catch
(
IOException
e
)
{
osw
.
flush
();
e
.
printStackTrace
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
try
{
osw
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
try
{
os
.
close
();
}
catch
(
IOException
e
)
{
}
}
public
void
response
(
String
[]
splitRequestCopy
)
throws
IOException
{
if
(!
completeRequest
.
contains
(
"http://"
)
&&
completeRequest
.
contains
(
"list"
)){
savedList
();
String
fireRequest
=
scanner
.
nextLine
();
String
[]
requestsAgain
=
fireRequest
.
split
(
" "
);
for
(
int
i
=
2
;
i
<
requestsAgain
.
length
;
i
++){
fire
.
fire
(
Integer
.
parseInt
(
requestsAgain
[
i
]));
}
return
;
}
try
{
String
link_url
=
address
;
obj1
=
new
URL
(
link_url
);
//URL Connection Created...
con
=
(
HttpURLConnection
)
obj1
.
openConnection
();
//Http URL Connection Created...
System
.
out
.
println
(
address
);
con
.
setRequestMethod
(
"GET"
);
for
(
int
i
=
0
;
i
<
splitRequestCopy
.
length
;
i
++)
{
if
(
splitRequestCopy
[
i
].
equals
(
"-M"
)
||
splitRequestCopy
[
i
].
equals
(
"--method"
))
{
if
(
isMethod
(
splitRequestCopy
[
i
+
1
]))
{
con
=
connect
(
obj1
,
splitRequestCopy
[
i
+
1
],
con
);
System
.
out
.
println
(
con
.
getRequestMethod
());
System
.
out
.
println
(
"44444444444"
);
i
++;
}
}
else
if
(
splitRequestCopy
[
i
].
equals
(
"-H"
)
||
splitRequestCopy
[
i
].
equals
(
"--headers"
))
{
//setting headers
String
[]
keyValue
=
splitRequestCopy
[
i
+
1
].
trim
().
split
(
";"
);
//splitting headers from each other
for
(
int
j
=
0
;
j
<
keyValue
.
length
;
j
++){
String
[]
keyValueCopy
=
keyValue
[
j
].
trim
().
split
(
":"
);
//splitting key and value
String
key
=
keyValueCopy
[
0
];
String
value
=
keyValueCopy
[
1
];
//con.setDoOutput(true);
con
.
setDoInput
(
true
);
con
.
setRequestProperty
(
key
,
value
);
System
.
out
.
println
(
"55555555"
);
}
i
++;
}
}
else
if
(
splitRequestCopy
[
i
].
equals
(
"-i"
))
{
System
.
out
.
println
(
"33333333"
);
include
=
true
;
try
{
}
File
newTextFile
=
new
File
(
"C:/thetextfile.txt"
);
else
if
(
splitRequestCopy
[
i
].
equals
(
"-S"
)
||
splitRequestCopy
[
i
].
equals
(
"--save"
)){
Random
random
=
new
Random
();
int
k
=
random
.
nextInt
(
100000
);
String
requestName
=
"savedRequest"
+
k
+
".txt"
;
save
(
requestName
,
completeRequest
);
//saving the whole request ????????
}
FileWriter
fw
=
new
FileWriter
(
newTextFile
);
else
if
(
splitRequestCopy
[
i
].
equals
(
"-O"
)
||
splitRequestCopy
[
i
].
equals
(
"--output"
))
{
fw
.
write
(
content
);
try
{
fw
.
close
();
if
(!
isMethod
(
splitRequestCopy
[
i
+
1
]))
{
content
=
con
.
getResponseMessage
();
}
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
catch
(
IOException
iox
)
{
try
{
iox
.
printStackTrace
();
File
newTextFile
=
new
File
(
"C:/thetextfile.txt"
);
FileWriter
fw
=
new
FileWriter
(
newTextFile
);
fw
.
write
(
content
);
fw
.
close
();
}
catch
(
IOException
iox
)
{
iox
.
printStackTrace
();
}
}
}
else
if
(
splitRequestCopy
[
i
].
equals
(
"-d"
)
||
splitRequestCopy
[
i
].
equals
(
"--data"
)){
String
cont
=
splitRequestCopy
[
i
];
post
(
con
,
cont
);
}
else
if
(
splitRequestCopy
[
i
].
equals
(
"-h"
)
||
splitRequestCopy
[
i
].
equals
(
"--help"
)){
String
help
=
(
"-d, --data DATA HTTP POST data (H)"
+
"\n"
+
"-H, --header LINE Pass custom header LINE to server (H)"
+
"\n"
+
"-h, --help This help text"
+
"\n"
+
"-i Include protocol headers in the output (H/F)"
+
"\n"
+
"-o, --output FILE Write to FILE instead of stdout"
+
""
);
System
.
out
.
println
(
help
);
return
;
}
}
con
.
setInstanceFollowRedirects
(
true
);
con
.
connect
();
//getting the response
BufferedReader
in
=
new
BufferedReader
(
new
InputStreamReader
(
con
.
getInputStream
()));
StringBuffer
responseBuffer
=
new
StringBuffer
();
String
lines
;
while
((
lines
=
in
.
readLine
())
!=
null
){
responseBuffer
.
append
(
lines
+
"\n"
);
}
}
content
=
con
.
getResponseMessage
();
in
.
close
();
System
.
out
.
println
(
content
);
int
code
=
con
.
getResponseCode
();
System
.
out
.
println
(
code
);
System
.
out
.
println
(
"Connection Response Message : "
+
con
.
getResponseMessage
());
System
.
out
.
println
(
"RESPONE:"
+
con
.
getRequestMethod
());
System
.
out
.
println
(
con
.
getResponseCode
());
System
.
out
.
println
(
responseBuffer
.
toString
());
if
(
include
)
responseHeader
();
con
.
disconnect
();
}
catch
(
Exception
e
)
{
System
.
out
.
println
(
e
);
}
}
}
}
}
}
src/Main.java
View file @
4aeae7c2
import
java.io.IOException
;
import
java.io.IOException
;
import
java.net.*
;
import
java.util.Scanner
;
import
java.util.Scanner
;
import
java.io.IOException
;
import
java.net.HttpURLConnection
;
import
java.net.URL
;
public
class
Main
{
public
class
Main
{
public
static
void
main
(
String
[]
args
){
public
static
void
main
(
String
[]
args
)
throws
IOException
{
// GetResponse getResponse = new GetResponse();
// getResponse.splitRequest();
// Response response = new Response();
GetResponse
response
=
new
GetResponse
();
GetResponse
response
=
new
GetResponse
();
}
}
}
}
src/Response.java
View file @
4aeae7c2
import
java.io.BufferedReader
;
// Import required java libraries
import
java.io.IOException
;
import
java.io.*
;
import
java.io.InputStreamReader
;
import
javax.servlet.*
;
import
java.io.OutputStream
;
import
javax.servlet.http.*
;
import
java.net.HttpURLConnection
;
import
java.util.*
;
import
java.net.URL
;
// Extend HttpServlet class
public
class
Response
{
public
class
Response
extends
HttpServlet
{
// Method to handle GET method request.
private
static
final
String
USER_AGENT
=
"Mozilla/5.0"
;
public
void
doGet
(
HttpServletRequest
request
,
HttpServletResponse
response
)
throws
ServletException
,
IOException
{
private
static
final
String
GET_URL
=
"https://localhost:9090/SpringMVCExample"
;
// Set refresh, autoload time as 5 seconds
private
static
final
String
POST_URL
=
"https://localhost:9090/SpringMVCExample/home"
;
response
.
setIntHeader
(
"Refresh"
,
5
);
private
static
final
String
POST_PARAMS
=
"userName=Pankaj"
;
// Set response content type
response
.
setContentType
(
"text/html"
);
public
Response
(){
// Get current time
try
{
Calendar
calendar
=
new
GregorianCalendar
();
sendGET
();
String
am_pm
;
}
catch
(
IOException
e
)
{
int
hour
=
calendar
.
get
(
Calendar
.
HOUR
);
e
.
printStackTrace
();
int
minute
=
calendar
.
get
(
Calendar
.
MINUTE
);
}
int
second
=
calendar
.
get
(
Calendar
.
SECOND
);
System
.
out
.
println
(
"GET DONE"
);
try
{
if
(
calendar
.
get
(
Calendar
.
AM_PM
)
==
0
)
sendPOST
();
am_pm
=
"AM"
;
}
catch
(
IOException
e
)
{
else
e
.
printStackTrace
();
am_pm
=
"PM"
;
}
System
.
out
.
println
(
"POST DONE"
);
String
CT
=
hour
+
":"
+
minute
+
":"
+
second
+
" "
+
am_pm
;
}
PrintWriter
out
=
response
.
getWriter
();
private
static
void
sendGET
()
throws
IOException
{
String
title
=
"Auto Refresh Header Setting"
;
URL
obj
=
new
URL
(
GET_URL
);
String
docType
=
HttpURLConnection
con
=
(
HttpURLConnection
)
obj
.
openConnection
();
"<!doctype html public \"-//w3c//dtd html 4.0 "
+
"transitional//en\">\n"
;
con
.
setRequestMethod
(
"GET"
);
con
.
setRequestProperty
(
"User-Agent"
,
USER_AGENT
);
out
.
println
(
docType
+
int
responseCode
=
con
.
getResponseCode
();
"<html>\n"
+
System
.
out
.
println
(
"GET Response Code :: "
+
responseCode
);
"<head><title>"
+
title
+
"</title></head>\n"
+
if
(
responseCode
==
HttpURLConnection
.
HTTP_OK
)
{
// success
"<body bgcolor = \"#f0f0f0\">\n"
+
BufferedReader
in
=
new
BufferedReader
(
new
InputStreamReader
(
"<h1 align = \"center\">"
+
title
+
"</h1>\n"
+
con
.
getInputStream
()));
"<p>Current Time is: "
+
CT
+
"</p>\n"
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
{
// Method to handle POST method request.
URL
obj
=
new
URL
(
POST_URL
);
public
void
doPost
(
HttpServletRequest
request
,
HttpServletResponse
response
)
HttpURLConnection
con
=
(
HttpURLConnection
)
obj
.
openConnection
();
throws
ServletException
,
IOException
{
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
();
doGet
(
request
,
response
);
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"
);
}
}
}
}
}
\ No newline at end of file
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