Java -> URL -> POST

4 respostas
T

Olá.

Eu queria fazer o seguinte:

Tenho um website onde existe uma tela de login, com 2 campos, o de login e senha. E se loga pelo método POST.

Quero logar no site através de um aplicativo Java, então preciso passar essas informações pelo método POST.

Como posso proceder?

Obrigado.

4 Respostas

_

Você precisa se conectar a um site através de sua aplicação desktop??

Já tentou usar o HttClient?

http://jakarta.apache.org/commons/httpclient/

T

Nem conheço! vou dar uma olhada, obrigado.

T

_Renatu… vc sabe se tenho que ter algo rodando na minha maquina pra funcionar aqueles exemplos? pq nenhum deles ta rodando…

geralmente fala assim:

run-single:

Exception in thread main java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory

at org.apache.commons.httpclient.HttpClient.<clinit>(HttpClient.java:65)

at despertador.LerURL.main(LerURL.java:28)

Java Result: 1
T

Outra coisa... alguem me ajude.. eu consegui este código, mas ele só da a mensagem "Bad post.."

import java.net.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class LerURL extends JPanel implements ActionListener &#123;
  JTextField nameField, passwordField;
  String postURL;

  GridBagConstraints constraints = new GridBagConstraints&#40;  &#41;;
  void addGB&#40; Component component, int x, int y &#41; &#123;
    constraints.gridx = x;  constraints.gridy = y;
    add &#40; component, constraints &#41;;
  &#125;

  public LerURL&#40;&#41; &#123;
    this.postURL = &quot;http&#58;//www.meusite/login.php&quot;;
    JButton postButton = new JButton&#40;&quot;Post&quot;&#41;;
    postButton.addActionListener&#40; this &#41;;
    setLayout&#40; new GridBagLayout&#40;  &#41; &#41;;
    addGB&#40; new JLabel&#40;&quot;Login&#58;&quot;&#41;, 0,0 &#41;;
    addGB&#40; nameField = new JTextField&#40;20&#41;, 1,0 &#41;;
    addGB&#40; new JLabel&#40;&quot;Password&#58;&quot;&#41;, 0,1 &#41;;
    addGB&#40; passwordField = new JPasswordField&#40;20&#41;,1,1 &#41;;
    constraints.gridwidth = 2;
    addGB&#40; postButton, 0,2 &#41;;
  &#125;

  public void actionPerformed&#40;ActionEvent e&#41; &#123;
    postData&#40;&#41;;
  &#125;

  protected void postData&#40;  &#41; &#123;
      try &#123;
    StringBuffer sb = new StringBuffer&#40;  &#41;;
    sb.append&#40; URLEncoder.encode&#40;&quot;login&quot;, &quot;UTF-8&quot;&#41; +&quot;=&quot; &#41;;
    sb.append&#40; URLEncoder.encode&#40;&quot;teste&quot;, &quot;UTF-8&quot;&#41; &#41;;
    //sb.append&#40; URLEncoder.encode&#40;nameField.getText&#40;&#41;, &quot;UTF-8&quot;&#41; &#41;;
    sb.append&#40; &quot;&amp;&quot; + URLEncoder.encode&#40;&quot;password&quot;, &quot;UTF-8&quot;&#41;+&quot;=&quot; &#41;;
    sb.append&#40; URLEncoder.encode&#40;&quot;teste&quot;, &quot;UTF-8&quot;&#41; &#41;;
    //sb.append&#40; URLEncoder.encode&#40;passwordField.getText&#40;&#41;, &quot;UTF-8&quot;&#41; &#41;;
    String formData = sb.toString&#40;  &#41;;


      URL url = new URL&#40; postURL &#41;;
      HttpURLConnection urlcon = &#40;HttpURLConnection&#41; url.openConnection&#40;  &#41;;
      urlcon.setRequestMethod&#40;&quot;POST&quot;&#41;;
      urlcon.setRequestProperty&#40;&quot;Content-type&quot;, &quot;application/x-www-form-urlencoded&quot;&#41;;
      urlcon.setDoOutput&#40;true&#41;;
      urlcon.setDoInput&#40;true&#41;;
      PrintWriter pout = new PrintWriter&#40; new OutputStreamWriter&#40;
          urlcon.getOutputStream&#40;  &#41;, &quot;8859_1&quot;&#41;, true &#41;;
      pout.print&#40; formData &#41;;
      pout.flush&#40;  &#41;;

      // read results...
      if &#40; urlcon.getResponseCode&#40;  &#41; != HttpURLConnection.HTTP_OK &#41;
        System.out.println&#40;&quot;Posted ok!&quot;&#41;;
      else &#123;
        System.out.println&#40;&quot;Bad post...&quot;&#41;;
        
      &#125;
      //InputStream in = urlcon.getInputStream&#40;  &#41;;
      // ...
      
        // Get the response
//        BufferedReader rd = new BufferedReader&#40;new InputStreamReader&#40;urlcon.getInputStream&#40;&#41;&#41;&#41;;
//        String line;
//        while &#40;&#40;line = rd.readLine&#40;&#41;&#41; != null&#41; &#123;
//            System.out.println&#40;line&#41;;
//        &#125;
	
    &#125; catch &#40;MalformedURLException e&#41; &#123;
      System.out.println&#40;e&#41;;     // bad postURL
    &#125; catch &#40;IOException e2&#41; &#123;
      System.out.println&#40;e2&#41;;    // I/O error
    &#125;
      System.exit&#40;0&#41;;
  &#125;

  public static void main&#40; String &#91;&#93; args &#41; &#123;
    JFrame frame = new JFrame&#40;&quot;SimplePost&quot;&#41;;
    frame.getContentPane&#40;&#41;.add&#40; new LerURL&#40;&#41;, &quot;Center&quot; &#41;;
    frame.pack&#40;&#41;;
    frame.setVisible&#40;true&#41;;
  &#125;
&#125;
Criado 31 de janeiro de 2007
Ultima resposta 2 de fev. de 2007
Respostas 4
Participantes 2