suppose we know the name of textfields on a html page and we wish to write a java prog. to automatically login on website with our given predefined details.

i mean sending an http request with values for login fields through a prog. and then handle the response.

Yes that is possible.

how do we handle the response sent by server

After we read it, we look at it and do what ever is required for what was contained in the response.

The response from the server would depend on what was sent, the server itself, the time of day, or ???

This is all sort of general BS. You're going to have to ask more specific questions to get more specific answers.

i was working to make a web app scanner , scanning for vulnerabilities like xss , sql injection . so was thinking to try to fire some script and sql quries in data fields of a web page and then again scanning the response of server if it produces any change in page .

may be i need some concept of networking i suppose.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * NewFrame.java
 *
 * Created on Jul 29, 2010, 1:53:05 PM
 */

package desktopapplication3;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

/**
 *
 * @author ashish
 */
public class NewFrame extends java.awt.Frame {
 public static String str="the begining ";
    /** Creates new form NewFrame */
    public NewFrame() {
        initComponents();
         System.out.println (str);
        textArea1.setText(str);
        textArea1.setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        textArea1 = new java.awt.TextArea();

        setName("Form"); // NOI18N
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                exitForm(evt);
            }
        });

        textArea1.setName("textArea1"); // NOI18N
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(desktopapplication3.DesktopApplication3.class).getContext().getResourceMap(NewFrame.class);
        textArea1.setText(resourceMap.getString("textArea1.text")); // NOI18N
        add(textArea1, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>                        

    /** Exit the Application */
    private void exitForm(java.awt.event.WindowEvent evt) {                          
        System.exit(0);
    }                         

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) throws MalformedURLException, IOException {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NewFrame().setVisible(false);
            }
        });

        URL                 url;
    HttpURLConnection   urlConn;
    DataOutputStream    printout;
    DataInputStream     input;
    // URL of CGI-Bin script.
    url = new URL ("http://www.facebook.com/index.php?eu=RNdllprq22cfuutJtgzU_Q");
    // URL connection channel.
    urlConn = (HttpURLConnection)url.openConnection();
    // Let the run-time system (RTS) know that we want input.
    urlConn.setDoInput (true);
    // Let the RTS know that we want to do output.
    urlConn.setDoOutput (true);
    urlConn.setRequestMethod("GET");
    // No caching, we want the real thing.
    urlConn.setUseCaches (false);
    // Specify the content type.
    urlConn.setRequestProperty
    ("User-agent","Mozilla/4.0");
    // Send POST output.
    HttpURLConnection.setFollowRedirects(true);
    printout = new DataOutputStream (urlConn.getOutputStream ());
    String content ="email="+URLEncoder.encode("tyagiabhishek789@gmail.com")+"&pass="+URLEncoder.encode("lolo");
   // "q=" + URLEncoder.encode ("Buford Early") ;
    //"&password=" + URLEncoder.encode ("buford@known-space.com");
    printout.writeBytes (content);
    printout.flush ();
    printout.close ();
    // Get response data.
    input = new DataInputStream (urlConn.getInputStream ());
     String str1;
    while (null != ((str1 = input.readLine())))
    {

    str = str+"\n"+str1;

    }
    System.out.println (str);
    java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NewFrame().setVisible(true);
            }
        });
    input.close ();

    }
       public void textAppend(String str)
    {
        textArea1.append(str);

       }

    // Variables declaration - do not modify                     
    private java.awt.TextArea textArea1;
    // End of variables declaration                   

}

I AM TRYING WITH THIS CODE BUT STILL IAM NOT GETTING THE NEXT WEBPAGE LIKE HOMEPAGE OR WRONGPASSWORD WHATEVER .IT ALWAYS SENDS ME THE SAME WEBPAGE THAT I HAV MADE CONNECTION

Can you explain the communication that takes place? Something like the following except show the contents of the ...req... and ...resp... messages.

You send a request to a server: ....req1....
The server sends you a response: ...resp1....
You read the response and send another resquest ...req2.. to the server
The server sends you a response: ....resp2....

public static void main(String[] args) throws Exception {


      HttpClient client = new HttpClient();

     GetMethod getPage = new GetMethod("http://www.fbook.com");

       client.executeMethod(getPage);

       PostMethod postLogin = new PostMethod("http://www.fbook.com/login.php");

        NameValuePair[] data = {

            new NameValuePair("email", "989@gmail.com"),
          new NameValuePair("pass", "password"),
          new NameValuePair("submit","Login")

         };

         postLogin.setRequestBody(data);

         client.executeMethod(postLogin);


        //this is a request to a page that requires authentication
        GetMethod getPage2 = new GetMethod("http://www.fbook.com/home.php");

        client.executeMethod(getPage2);

        System.out.println(getPage2.getResponseBodyAsString());


     }

i think it s good to use external library here .this code works but so much of libraries download and importing is required . still cookies hav to be taken care of

Have you solved your problem?

hey

well the problem has been solved now .

i used "htmlunit" api to get login into site s like gmail,yahoo etc.

the above code works on some sites too where javascript is not interupting.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.