Skip to content

Instantly share code, notes, and snippets.

@matheustardivo
Created October 29, 2012 13:00
Show Gist options
  • Save matheustardivo/3973396 to your computer and use it in GitHub Desktop.
Save matheustardivo/3973396 to your computer and use it in GitHub Desktop.
Exemplo de código para fazer o tracking do site dos Correios.
import java.io.DataInputStream;
import java.io.File;
import java.io.FileWriter;
import java.io.PrintStream;
import java.net.URL;
import java.net.URLConnection;
public class CorreiosCarrierTracker {
public static void main(String[] args) {
new CorreiosCarrierTracker().saveXML("correios.xml");
}
@SuppressWarnings("deprecation")
public void saveXML(String outputFile) {
try {
File outputXMLFile = new File(outputFile);
String trackingCodes = "SW371675211BR";
URL url = new URL(
"http://websro.correios.com.br/sro_bin/sroii_xml.eventos");
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
connection.setAllowUserInteraction(false);
PrintStream outStream = new PrintStream(
connection.getOutputStream());
outStream
.println("usuario=ECT&senha=SRO&tipo=L&resultado=T&objetos="
+ trackingCodes);
outStream.close();
DataInputStream inStream = new DataInputStream(
connection.getInputStream());
String inputLine;
FileWriter f = new FileWriter(outputXMLFile);
while ((inputLine = inStream.readLine()) != null) {
f.write(inputLine);
}
f.close();
inStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment