• Fuentes

 #389217  por adwind
 
Código simple que conecta a una pagina gratuita para checar puertos online. Se envía como parámetro solo el puerto. Mediante una petición POST
Código: Seleccionar todo
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Pattern;
import javax.swing.UIManager;
public class te{

public static void main(String []argumentos){
 try {
            HttpURLConnection c = (HttpURLConnection) new URL("http://www.pcwintech.com/port_test.php").openConnection();
            c.setInstanceFollowRedirects(true);
            c.setRequestMethod("POST");
            c.setDoInput(true);
            c.setDoOutput(true);
            c.setUseCaches(false);
            c.setRequestProperty("Accept", "text/xml,text/plain,text/html");
            c.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

            OutputStreamWriter out = new OutputStreamWriter(c.getOutputStream());
            out.write("port=" + "80"+ "&CheckPort=Check Port");
            out.close();

            BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
            while (br.ready()) {
                String tmp = br.readLine();
                if (tmp.contains("Your Internet IP Address:")) {
                    ip.setText(tmp.split(Pattern.quote(": "))[1].split(Pattern.quote("</"))[0]);
                }

                if (tmp.contains("<script>showresults")) {
                    String datos[] = tmp.split("','");

                    /*Estado*/
                    String estados[] = datos[0].split(Pattern.quote("('"));
                    System.out.println(estados[1]);
  

                    String estados2[] = datos[1].split(Pattern.quote("');"));
                    System.out.println(estados2[0]);

                }
            }

        } catch (Exception ex) {
//            Logger.getLogger(JavaApplication2.class.getName()).log(Level.SEVERE, null, ex);
        }}}