Last active
November 15, 2023 21:54
In another window, run `nc -l 8000` (mac) or `nc -l -p 8000` (linux) to listen on port 8000. Then run this class with the otel java agent.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.opentelemetry.javaagent; | |
import java.io.OutputStream; | |
import java.io.OutputStreamWriter; | |
import java.net.HttpURLConnection; | |
import java.net.URL; | |
import java.util.concurrent.TimeUnit; | |
import static java.nio.charset.StandardCharsets.UTF_8; | |
public class HttpUrlConnectionCheck { | |
private HttpUrlConnectionCheck() {} | |
public static void main(String[] args) throws Exception { | |
while (true) { | |
doPost(); | |
TimeUnit.SECONDS.sleep(5); | |
} | |
} | |
private static void doPost() throws Exception { | |
// URL url = new URL("http://example.com"); | |
URL url = new URL("http://localhost:8000"); | |
System.out.println("Opening connection..."); | |
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); | |
conn.setDoOutput(true); | |
conn.setRequestMethod("POST"); | |
OutputStream out = conn.getOutputStream(); | |
OutputStreamWriter writer = new OutputStreamWriter(out, UTF_8); | |
writer.write("this is a test\n\n"); | |
writer.flush(); | |
writer.close(); | |
out.close(); | |
System.out.println("All done."); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment