[BAEL-13505] Move articles out of core-java part 1
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
### Relevant Articles
|
||||
|
||||
- [Checking if a URL Exists in Java](https://www.baeldung.com/java-check-url-exists)
|
||||
- [Making a JSON POST Request With HttpURLConnection](https://www.baeldung.com/httpurlconnection-post)
|
||||
- [Using Curl in Java](https://www.baeldung.com/java-curl)
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.baeldung.curltojava;
|
||||
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
public class JavaCurlExamples {
|
||||
|
||||
public static String inputStreamToString(InputStream inputStream) {
|
||||
final int bufferSize = 8 * 1024;
|
||||
byte[] buffer = new byte[bufferSize];
|
||||
final StringBuilder builder = new StringBuilder();
|
||||
try (BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream, bufferSize)) {
|
||||
while (bufferedInputStream.read(buffer) != -1) {
|
||||
builder.append(new String(buffer));
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
Logger.getLogger(JavaCurlExamples.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
public static void consumeInputStream(InputStream inputStream) {
|
||||
inputStreamToString(inputStream);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.baeldung.urlconnection;
|
||||
|
||||
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 PostJSONWithHttpURLConnection {
|
||||
|
||||
public static void main (String []args) throws IOException{
|
||||
//Change the URL with any other publicly accessible POST resource, which accepts JSON request body
|
||||
URL url = new URL ("https://reqres.in/api/users");
|
||||
|
||||
HttpURLConnection con = (HttpURLConnection)url.openConnection();
|
||||
con.setRequestMethod("POST");
|
||||
|
||||
con.setRequestProperty("Content-Type", "application/json; utf-8");
|
||||
con.setRequestProperty("Accept", "application/json");
|
||||
|
||||
con.setDoOutput(true);
|
||||
|
||||
//JSON String need to be constructed for the specific resource.
|
||||
//We may construct complex JSON using any third-party JSON libraries such as jackson or org.json
|
||||
String jsonInputString = "{\"name\": \"Upendra\", \"job\": \"Programmer\"}";
|
||||
|
||||
try(OutputStream os = con.getOutputStream()){
|
||||
byte[] input = jsonInputString.getBytes("utf-8");
|
||||
os.write(input, 0, input.length);
|
||||
}
|
||||
|
||||
int code = con.getResponseCode();
|
||||
System.out.println(code);
|
||||
|
||||
try(BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(), "utf-8"))){
|
||||
StringBuilder response = new StringBuilder();
|
||||
String responseLine = null;
|
||||
while ((responseLine = br.readLine()) != null) {
|
||||
response.append(responseLine.trim());
|
||||
}
|
||||
System.out.println(response.toString());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package com.baeldung.curltojava;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class JavaCurlExamplesLiveTest {
|
||||
|
||||
@Test
|
||||
public void givenCommand_whenCalled_thenProduceZeroExitCode() throws IOException {
|
||||
String command = "curl -X GET https://postman-echo.com/get?foo1=bar1&foo2=bar2";
|
||||
ProcessBuilder processBuilder = new ProcessBuilder(command.split(" "));
|
||||
processBuilder.directory(new File("/home/"));
|
||||
Process process = processBuilder.start();
|
||||
InputStream inputStream = process.getInputStream();
|
||||
// Consume the inputStream so the process can exit
|
||||
JavaCurlExamples.consumeInputStream(inputStream);
|
||||
int exitCode = process.exitValue();
|
||||
|
||||
Assert.assertEquals(0, exitCode);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenNewCommands_whenCalled_thenCheckIfIsAlive() throws IOException {
|
||||
String command = "curl -X GET https://postman-echo.com/get?foo1=bar1&foo2=bar2";
|
||||
ProcessBuilder processBuilder = new ProcessBuilder(command.split(" "));
|
||||
processBuilder.directory(new File("/home/"));
|
||||
Process process = processBuilder.start();
|
||||
|
||||
// Re-use processBuilder
|
||||
processBuilder.command(new String[]{"newCommand", "arguments"});
|
||||
|
||||
Assert.assertEquals(true, process.isAlive());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenRequestPost_thenCheckIfReturnContent() throws IOException {
|
||||
String command = "curl -X POST https://postman-echo.com/post --data foo1=bar1&foo2=bar2";
|
||||
Process process = Runtime.getRuntime().exec(command);
|
||||
|
||||
// Get the POST result
|
||||
String content = JavaCurlExamples.inputStreamToString(process.getInputStream());
|
||||
|
||||
Assert.assertTrue(null != content && !content.isEmpty());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user