Skip to content
Snippets Groups Projects
Commit b2fbfc6e authored by Jueun Park's avatar Jueun Park
Browse files

Edited JastAdd Files and OpenAPIMain.java

parent 26e093f0
No related branches found
No related tags found
1 merge request!6Resolve "check and fix random request generator"
Pipeline #13266 passed
/*import com.fasterxml.jackson.databind.JsonNode; /*
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ArrayNode;
...@@ -144,60 +145,5 @@ aspect InferParameter{ ...@@ -144,60 +145,5 @@ aspect InferParameter{
writeDictionary(schema.schemaObject().getItemsSchema().getSchemaOb(), props.next().toString()); writeDictionary(schema.schemaObject().getItemsSchema().getSchemaOb(), props.next().toString());
} }
public void Get.connectGET(String path){
try {
URL url = new URL(path);
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
con.setRequestProperty("User-Agent", "Mozilla/5.0"); // add request header
con.setRequestMethod("GET"); // optional default is GET
int responseCode = con.getResponseCode();
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("Inferred path : " + path);
System.out.println("HTTP status code (GET) : " + responseCode);
} catch (Exception e) {
System.out.println(e.toString());
}
}
public void Post.connectPOST(String path){
try {
URL url = new URL(path);
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
con.setRequestMethod("POST"); // HTTP POST
con.setRequestProperty("User-Agent", "Mozilla/5.0"); // add request header
con.setDoOutput(true); // POST
int responseCode = con.getResponseCode();
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("Inferred path : " + path);
System.out.println("HTTP status code (POST) : " + responseCode);
} catch (Exception e) {
System.out.println(e.toString());
}
}
}
*/ */
\ No newline at end of file
/*
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
import java.util.Random;
import java.util.stream.IntStream;
aspect RandomRequestGenerator{ aspect RandomRequestGenerator{
syn String ParameterObject.randomPathParameter(String pathRef){ syn String ParameterObject.randomPathParameter(String pathRef){
Random rand = new Random(); Random rand = new Random();
SchemaObject s = getSchemaOb().schemaObject(); SchemaObject s = getSchema().schemaObject();
String pathPart = pathRef.substring(pathRef.indexOf("{"), pathRef.indexOf("}") + 1); String pathPart = pathRef.substring(pathRef.indexOf("{"), pathRef.indexOf("}") + 1);
if (s.getType().equals("string")) if (s.getType().equals("string"))
pathRef = pathRef.replace(pathPart, generateRandomString(rand, s.getEnumObjs())); pathRef = pathRef.replace(pathPart, generateRandomString(rand, s.getEList()));
else if (s.getType().equals("integer")) else if (s.getType().equals("integer"))
pathRef = pathRef.replace(pathPart, generateRandomInt(rand, pathRef = pathRef.replace(pathPart, generateRandomInt(rand,
-1, // s.getMinimum() != null ? s.getMinimum().intValue() : -1, -1, // s.getMinimum() != null ? s.getMinimum().intValue() : -1,
...@@ -27,19 +17,19 @@ aspect RandomRequestGenerator{ ...@@ -27,19 +17,19 @@ aspect RandomRequestGenerator{
syn String ParameterObject.randomQueryParameter(String pathRef){ syn String ParameterObject.randomQueryParameter(String pathRef){
Random rand = new Random(); Random rand = new Random();
SchemaObject s = getSchemaOb().schemaObject(); SchemaObject s = getSchema().schemaObject();
if (s.getType().equals("string")) if (s.getType().equals("string"))
pathRef = pathRef + "?" + getName() + "=" + generateRandomString(rand, s.getEnumObjs()); pathRef = pathRef + "?" + getName() + "=" + generateRandomString(rand, s.getEList());
else if (s.getType().equals("integer")) else if (s.getType().equals("integer"))
pathRef = pathRef + "?" + getName() + "=" + generateRandomInt(rand, pathRef = pathRef + "?" + getName() + "=" + generateRandomInt(rand,
-1, // s.getMinimum() != null ? s.getMinimum().intValue() : -1, -1, // s.getMinimum() != null ? s.getMinimum().intValue() : -1,
10); // s.getMaximum() != null ? s.getMaximum().intValue() : -1 10); // s.getMaximum() != null ? s.getMaximum().intValue() : -1
else if (s.getType().equals("array")) { else if (s.getType().equals("array")) {
if (s.getItemsSchema().getSchemaOb().schemaObject().getType().equals("string")) { if (s.getI().getSchema().schemaObject().getType().equals("string")) {
for (EnumObj e : s.getItemsSchema().getSchemaOb().schemaObject().getEnumObjs()) for (EnumObj e : s.getI().getSchema().schemaObject().getEList())
pathRef = rand.nextDouble() < 0.5 ? pathRef + "&" + getName() + "=" + e.getEnumOb() : pathRef; pathRef = rand.nextDouble() < 0.5 ? pathRef + "&" + getName() + "=" + e.getEnumOb() : pathRef;
} else if (s.getItemsSchema().getSchemaOb().schemaObject().getType().equals("integer")) { } else if (s.getI().getSchema().schemaObject().getType().equals("integer")) {
for (int i = 0; i < 5; i++) for (int i = 0; i < 5; i++)
pathRef = pathRef + "&" + getName() + "=" + generateRandomInt(rand, pathRef = pathRef + "&" + getName() + "=" + generateRandomInt(rand,
-1, // s.getMinimum() != null ? s.getMinimum().intValue() : -1, -1, // s.getMinimum() != null ? s.getMinimum().intValue() : -1,
...@@ -52,7 +42,7 @@ aspect RandomRequestGenerator{ ...@@ -52,7 +42,7 @@ aspect RandomRequestGenerator{
syn boolean OpenAPIObject.generateRequests(){ syn boolean OpenAPIObject.generateRequests(){
try { try {
for (PathsObject p : getPathsObjects()) for (PathsObject p : getPList())
p.generateUrl(); p.generateUrl();
} catch (Exception e) { } catch (Exception e) {
return false; return false;
...@@ -61,7 +51,8 @@ aspect RandomRequestGenerator{ ...@@ -61,7 +51,8 @@ aspect RandomRequestGenerator{
} }
inh boolean PathsObject.generateUrl(); inh boolean PathsObject.generateUrl();
eq OpenAPIObject.getPathsObject(int i).generateUrl(){ eq OpenAPIObject.getP(int i).generateUrl(){
/*
try { try {
PathItemObject p = getPathsObject(i).getPathItemObject(); PathItemObject p = getPathsObject(i).getPathItemObject();
String path = getServerObject(0).getUrl(); String path = getServerObject(0).getUrl();
...@@ -74,10 +65,12 @@ aspect RandomRequestGenerator{ ...@@ -74,10 +65,12 @@ aspect RandomRequestGenerator{
return true; return true;
} catch (Exception e) { } catch (Exception e) {
return false; return false;
} }*/
return true;
} }
syn boolean Get.generateRandomUrl(String pathRef){ syn boolean Get.generateRandomUrl(String pathRef){
/*
try { try {
for (ParameterOb o : getOperationObject().getParameterObs()) { for (ParameterOb o : getOperationObject().getParameterObs()) {
ParameterObject p = o.parameterObject(); ParameterObject p = o.parameterObject();
...@@ -88,25 +81,6 @@ aspect RandomRequestGenerator{ ...@@ -88,25 +81,6 @@ aspect RandomRequestGenerator{
} }
System.out.println("Generated path : " + pathRef); System.out.println("Generated path : " + pathRef);
URL url = new URL(pathRef);
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
con.setRequestProperty("User-Agent", "Mozilla/5.0"); // add request header
con.setRequestMethod("GET"); // optional default is GET
int responseCode = con.getResponseCode();
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("HTTP status code (GET) : " + responseCode);
//System.out.println("Response : " + response.toString());
for (ResponseTuple t : getOperationObject().getResponseTuples()) { for (ResponseTuple t : getOperationObject().getResponseTuples()) {
if (t.getKey().equals("200") && responseCode == 200) { if (t.getKey().equals("200") && responseCode == 200) {
//System.out.println("Response successfully saved!"); //System.out.println("Response successfully saved!");
...@@ -121,9 +95,11 @@ aspect RandomRequestGenerator{ ...@@ -121,9 +95,11 @@ aspect RandomRequestGenerator{
} catch (Exception e) { } catch (Exception e) {
System.out.println(e.toString()); System.out.println(e.toString());
return false; return false;
} }*/
return true;
} }
syn boolean Post.generateRandomUrl(String pathRef){ syn boolean Post.generateRandomUrl(String pathRef){
/*
try { try {
for (ParameterOb o : getOperationObject().getParameterObs()) { for (ParameterOb o : getOperationObject().getParameterObs()) {
ParameterObject p = o.parameterObject(); ParameterObject p = o.parameterObject();
...@@ -134,27 +110,6 @@ aspect RandomRequestGenerator{ ...@@ -134,27 +110,6 @@ aspect RandomRequestGenerator{
} }
System.out.println("Generated path : " + pathRef); System.out.println("Generated path : " + pathRef);
URL url = new URL(pathRef);
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
con.setRequestMethod("POST"); // HTTP POST
con.setRequestProperty("User-Agent", "Mozilla/5.0"); // add request header
con.setDoOutput(true); // POST
int responseCode = con.getResponseCode();
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("HTTP status code (POST) : " + responseCode);
//System.out.println("Response : " + response.toString());
for (ResponseTuple t : getOperationObject().getResponseTuples()) { for (ResponseTuple t : getOperationObject().getResponseTuples()) {
if (t.getKey().equals("200") && responseCode == 200) { if (t.getKey().equals("200") && responseCode == 200) {
//System.out.println("Response successfully saved!"); //System.out.println("Response successfully saved!");
...@@ -169,7 +124,8 @@ aspect RandomRequestGenerator{ ...@@ -169,7 +124,8 @@ aspect RandomRequestGenerator{
} catch (Exception e) { } catch (Exception e) {
System.out.println(e.toString()); System.out.println(e.toString());
return false; return false;
} }*/
return true;
} }
syn String ParameterObject.generateRandomString(Random rand,JastAddList<EnumObj> objs){ syn String ParameterObject.generateRandomString(Random rand,JastAddList<EnumObj> objs){
...@@ -193,5 +149,3 @@ aspect RandomRequestGenerator{ ...@@ -193,5 +149,3 @@ aspect RandomRequestGenerator{
return String.valueOf(rand.nextInt()); return String.valueOf(rand.nextInt());
} }
} }
\ No newline at end of file
*/
\ No newline at end of file
/*
import java.util.*; import java.util.*;
aspect Reference { aspect Reference {
...@@ -42,7 +41,7 @@ aspect Reference { ...@@ -42,7 +41,7 @@ aspect Reference {
eq ParameterReference.parameterObject() { eq ParameterReference.parameterObject() {
for (ParameterTuple t : root().parameterTuples()) { for (ParameterTuple t : root().parameterTuples()) {
if (t.getKey().equals(getRef().substring(getRef().lastIndexOf("/") + 1, getRef().length()))) if (t.getKey().equals(getRef().substring(getRef().lastIndexOf("/") + 1, getRef().length())))
return t.getParameterOb().parameterObject(); return t.getO().parameterObject();
} }
return new ParameterObject(); return new ParameterObject();
} }
...@@ -52,7 +51,7 @@ aspect Reference { ...@@ -52,7 +51,7 @@ aspect Reference {
eq SchemaReference.schemaObject() { eq SchemaReference.schemaObject() {
for (SchemaTuple t : root().schemaTuples()) { for (SchemaTuple t : root().schemaTuples()) {
if (t.getKey().equals(getRef().substring(getRef().lastIndexOf("/") + 1, getRef().length()))) if (t.getKey().equals(getRef().substring(getRef().lastIndexOf("/") + 1, getRef().length())))
return t.getSchemaOb().schemaObject(); return t.getO().schemaObject();
} }
return new SchemaObject(); return new SchemaObject();
} }
...@@ -62,7 +61,7 @@ aspect Reference { ...@@ -62,7 +61,7 @@ aspect Reference {
eq RequestBodyReference.requestBodyObject() { eq RequestBodyReference.requestBodyObject() {
for (RequestBodyTuple t : root().requestBodyTuples()) { for (RequestBodyTuple t : root().requestBodyTuples()) {
if (t.getKey().equals(getRef().substring(getRef().lastIndexOf("/") + 1, getRef().length()))) if (t.getKey().equals(getRef().substring(getRef().lastIndexOf("/") + 1, getRef().length())))
return t.getRequestBodyOb().requestBodyObject(); return t.getO().requestBodyObject();
} }
return new RequestBodyObject(); return new RequestBodyObject();
} }
...@@ -72,7 +71,7 @@ aspect Reference { ...@@ -72,7 +71,7 @@ aspect Reference {
eq ResponseReference.responseObject() { eq ResponseReference.responseObject() {
for (ResponseTuple t : root().responseTuples()) { for (ResponseTuple t : root().responseTuples()) {
if (t.getKey().equals(getRef().substring(getRef().lastIndexOf("/") + 1, getRef().length()))) if (t.getKey().equals(getRef().substring(getRef().lastIndexOf("/") + 1, getRef().length())))
return t.getResponseOb().responseObject(); return t.getO().responseObject();
} }
return new ResponseObject(); return new ResponseObject();
} }
...@@ -82,7 +81,7 @@ aspect Reference { ...@@ -82,7 +81,7 @@ aspect Reference {
eq CallbackReference.callbackObject() { eq CallbackReference.callbackObject() {
for (CallbackTuple t : root().callbackTuples()) { for (CallbackTuple t : root().callbackTuples()) {
if (t.getKey().equals(getRef().substring(getRef().lastIndexOf("/") + 1, getRef().length()))) if (t.getKey().equals(getRef().substring(getRef().lastIndexOf("/") + 1, getRef().length())))
return t.getCallbackOb().callbackObject(); return t.getO().callbackObject();
} }
return new CallbackObject(); return new CallbackObject();
} }
...@@ -92,7 +91,7 @@ aspect Reference { ...@@ -92,7 +91,7 @@ aspect Reference {
eq LinkReference.linkObject() { eq LinkReference.linkObject() {
for (LinkTuple t : root().linkTuples()) { for (LinkTuple t : root().linkTuples()) {
if (t.getKey().equals(getRef().substring(getRef().lastIndexOf("/") + 1, getRef().length()))) if (t.getKey().equals(getRef().substring(getRef().lastIndexOf("/") + 1, getRef().length())))
return t.getLinkOb().linkObject(); return t.getO().linkObject();
} }
return new LinkObject(); return new LinkObject();
} }
...@@ -102,7 +101,7 @@ aspect Reference { ...@@ -102,7 +101,7 @@ aspect Reference {
eq HeaderReference.headerObject() { eq HeaderReference.headerObject() {
for (HeaderTuple t : root().headerTuples()) { for (HeaderTuple t : root().headerTuples()) {
if (t.getKey().equals(getRef().substring(getRef().lastIndexOf("/") + 1, getRef().length()))) if (t.getKey().equals(getRef().substring(getRef().lastIndexOf("/") + 1, getRef().length())))
return t.getHeaderOb().headerObject(); return t.getO().headerObject();
} }
return new HeaderObject(); return new HeaderObject();
} }
...@@ -112,10 +111,9 @@ aspect Reference { ...@@ -112,10 +111,9 @@ aspect Reference {
eq SecuritySchemeReference.securitySchemeObject() { eq SecuritySchemeReference.securitySchemeObject() {
for (SecuritySchemeTuple t : root().securitySchemeTuples()) { for (SecuritySchemeTuple t : root().securitySchemeTuples()) {
if (t.getKey().equals(getRef().substring(getRef().lastIndexOf("/") + 1, getRef().length()))) if (t.getKey().equals(getRef().substring(getRef().lastIndexOf("/") + 1, getRef().length())))
return t.getSecuritySchemeOb().securitySchemeObject(); return t.getO().securitySchemeObject();
} }
return new SecuritySchemeObject(); return new SecuritySchemeObject();
} }
} }
\ No newline at end of file
*/
\ No newline at end of file
package de.tudresden.inf.st.openapi;
import javax.net.ssl.HttpsURLConnection;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpURLConnectionExample {
private final String USER_AGENT = "Mozilla/5.0";
public static void main(String[] args) throws Exception {
HttpURLConnectionExample http = new HttpURLConnectionExample();
System.out.println("Data call with GET");
http.sendGet("https://petstore.swagger.io/v2/pet/9222968140497310446");
//System.out.println("Data call with POST");
//String urlParameters = "";
//http.sendPost("https://petstore.swagger.io/v2/pet", urlParameters);
//System.out.println("Data call with DELETE");
//http.sendDelete("https://petstore.swagger.io/v2/pet/123123");
}
// HTTP GET request
private void sendGet(String targetUrl) throws Exception {
URL url = new URL(targetUrl);
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
con.setRequestMethod("GET"); // optional default is GET
con.setRequestProperty("User-Agent", USER_AGENT); // add request header
int responseCode = con.getResponseCode();
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("HTTP status code : " + responseCode);
System.out.println("HTTP body : " + response);
}
// HTTP POST request
private void sendPost(String targetUrl, String parameters) throws Exception {
URL url = new URL(targetUrl);
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
con.setRequestMethod("POST"); // HTTP POST
con.setRequestProperty("User-Agent", USER_AGENT);
//con.addRequestProperty("body", "");
//con.addRequestProperty("status", "available");
con.setDoOutput(true); // POST
// Send post request
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(parameters);
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
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("HTTP status code (POST) : " + responseCode);
System.out.println("HTTP body : " + response);
}
// HTTP DELETE request
private void sendDelete(String targetUrl) throws Exception {
URL url = new URL(targetUrl);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("DELETE"); // optional default is GET
con.setRequestProperty("User-Agent", USER_AGENT); // add request header
int responseCode = con.getResponseCode();
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("HTTP status code : " + responseCode);
//System.out.println("HTTP body : " + response.toString());
}
}
\ No newline at end of file
...@@ -2,6 +2,7 @@ package de.tudresden.inf.st.openapi; ...@@ -2,6 +2,7 @@ package de.tudresden.inf.st.openapi;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import de.tudresden.inf.st.openapi.ast.OpenAPIObject;
import io.swagger.models.reader.SwaggerParser; import io.swagger.models.reader.SwaggerParser;
import io.swagger.parser.OpenAPIParser; import io.swagger.parser.OpenAPIParser;
import io.swagger.report.MessageBuilder; import io.swagger.report.MessageBuilder;
...@@ -19,8 +20,6 @@ import org.apache.http.impl.client.HttpClientBuilder; ...@@ -19,8 +20,6 @@ import org.apache.http.impl.client.HttpClientBuilder;
import javax.net.ssl.HttpsURLConnection; import javax.net.ssl.HttpsURLConnection;
import java.io.*; import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
...@@ -31,43 +30,13 @@ public class OpenAPIMain { ...@@ -31,43 +30,13 @@ public class OpenAPIMain {
* main-method, calls the set of methods to test the OpenAPI-Generator with JastAdd * main-method, calls the set of methods to test the OpenAPI-Generator with JastAdd
**/ **/
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
List<String> filenames = new ArrayList<>();
String genDir = "./gen-api-ex/";
File genDirectory = new File(genDir);
File[] contents;
File resource = new File("./src/main/resources");
// init parser
String fileName = "./src/main/resources/3.0/petstore.yaml"; String fileName = "./src/main/resources/3.0/petstore.yaml";
/* OpenAPIObject jastAddObject;
ParseOptions options = new ParseOptions();
options.setResolve(true);
options.setResolveFully(true);
options.setAllowEmptyString(false);
*/
SwaggerParseResult result = new OpenAPIParser().readLocation(fileName, null, null); SwaggerParseResult result = new OpenAPIParser().readLocation(fileName, null, null);
String resultString;
OpenAPI openAPI = result.getOpenAPI(); OpenAPI openAPI = result.getOpenAPI();
List<String> generatedURLs = new ArrayList<>();
OpenAPIDeserializer deserializer = new OpenAPIDeserializer(); jastAddObject = OpenAPIObject.parseOpenAPI(openAPI);
String res = Json.mapper().writeValueAsString(openAPI);
ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.readTree(res);
//System.out.println(node.toPrettyString());
/** OpenAPI Validator! **/
List<String> validation = new OpenAPIV3Parser().readContents(res).getMessages();
for(String mes : validation)
System.out.println("message : " + mes);
//Json.mapper().createParser(Json.mapper().writeValueAsString(openAPI)).
// Yaml String
//System.out.println(Yaml.mapper().writerWithDefaultPrettyPrinter().writeValueAsString(openAPI));
//resultString = Yaml.mapper().writerWithDefaultPrettyPrinter().writeValueAsString(openAPI);
URL expUrl = OpenAPIMain.class.getClassLoader().getResource(fileName); URL expUrl = OpenAPIMain.class.getClassLoader().getResource(fileName);
File file = null; File file = null;
...@@ -85,6 +54,62 @@ public class OpenAPIMain { ...@@ -85,6 +54,62 @@ public class OpenAPIMain {
if (args.length > 0) { if (args.length > 0) {
fileName = args[0]; fileName = args[0];
} }
}
public void sendRandomRequests() {}
public void sendInferredRequests(List<String> randomRequests) {}
public void connectGET(String path){
try {
URL url = new URL(path);
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
con.setRequestProperty("User-Agent", "Mozilla/5.0"); // request header
con.setRequestMethod("GET"); // optional default is GET
int responseCode = con.getResponseCode();
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("connected path : " + path);
System.out.println("HTTP status code (GET) : " + responseCode);
} catch (Exception e) {
System.out.println(e.toString());
}
}
public void connectPOST(String path){
try {
URL url = new URL(path);
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
con.setRequestMethod("POST"); // HTTP POST
con.setRequestProperty("User-Agent", "Mozilla/5.0"); // request header
con.setDoOutput(true); // POST
int responseCode = con.getResponseCode();
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("connected path : " + path);
System.out.println("HTTP status code (POST) : " + responseCode);
} catch (Exception e) {
System.out.println(e.toString());
}
} }
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment