简体   繁体   中英

Printing URLConnection request before sending

Java's URLConnection lets us form http requests. After forming a simple POST request like so:

URLConnection con = url.openConnection();
con.addRequestProperty("User-agent", "Mozilla");
con.setDoOutput(true);
String data = "text to send";
OutputStreamWriter wr = new OutputStreamWriter(con.getOutputStream());
wr.write(data);

Is there any way of checking what this request looks like before sending it?

You need to enable logging and in your logging.properties file you should have following property set

handlers= java.util.logging.ConsoleHandler
java.util.logging.ConsoleHandler.level = FINEST
sun.net.www.protocol.http.HttpURLConnection.level=ALL

And, set the property file as a JVM property

-Djava.util.logging.config.file=logging.properties

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM