URGENT!!How to use the "POST" method to connect to server
3 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   sabu_vs
Posted On:   Friday, December 14, 2001 12:06 AM

hi I have a progarm that uses HttpURLConnection to connect to server. I want to use "Post" method to connect ;so I used the setRequestMethod for that purpose. Now I want to pass 2 variables. ie name="sa" and Age="26" to the server. If it is a "Get" method I can use the code below Url url=new URL("http://www.abc.com/servlet/Test?Name=sa&Age=26"). Could you pls tell me how can I pass the variable using post. Could you pls include a sample code thanks sabu    More>>

hi



I have a progarm that uses HttpURLConnection to connect to server.


I want to use "Post" method to connect ;so I used the setRequestMethod for that purpose.


Now I want to pass 2 variables. ie name="sa" and Age="26" to the server.



If it is a "Get" method I can use the code below




Url url=new URL("http://www.abc.com/servlet/Test?Name=sa&Age=26").




Could you pls tell me how can I pass the variable using post.


Could you pls include a sample code



thanks


sabu

   <<Less

Re: URGENT!!How to use the "POST" method to connect to server

Posted By:   sam_pitroda  
Posted On:   Monday, December 17, 2001 08:56 PM

use HttpServletRequest.getParameter() method..thats all !

Re: URGENT!!How to use the "POST" method to connect to server

Posted By:   Lisa_Javamint  
Posted On:   Sunday, December 16, 2001 02:35 AM

I think that email account I cannot access this week, so you can try me at this forum for now. Sorry for the bad formatting , I can send it to you in an email - the formatting is better and I want to give you a different version of the script.
Lisa

Re: URGENT!!How to use the "POST" method to connect to server

Posted By:   Lisa_Javamint  
Posted On:   Sunday, December 16, 2001 02:09 AM

Sabu,
I am having the same problem,
Below please find the source code for a program I have started to work on - I am practicing on a firend`s site called nokri.com, it compiles and runs but all beginners at this find that when they try to post login data they get in a resonse all the time the actual login page. This is where I am at right now.
I hope we can cooperate on what we learn in this area to nail it down. I have done this in VB but I want to master it in Java and do as muc of my network programming as I can in Java.
Lisa
Here is the source code:
//import java.text.*;
//import java.util.*;
import java.net.*;
import java.io.*;
/*
Writing to a URLConnection:
1. Create a URL.
2. Open a connection to the URL.
3. Set output capability on the URLConnection.
4. Get an output stream from the connection.
This output stream is connected to
the standard input stream of
the cgi-bin script on the server.
5. Write to the output stream.
6. Close the output stream.
*/
public class Webcrawler6 {
public static void main(String[] args){
//get logged in first!
URL url = null; // why do I need to declare it null first?
try{
url = new URL("http://www.nokri.com/default.asp");
}
catch(java.net.MalformedURLException e){System.out.println("URL is wrong - 1");}
catch(IOException e){System.out.println("URL katachi is okay but still wrong - 2");}
// System.out.println(urlString); // for testing only
URLConnection urlConnection = null;
try{
urlConnection = url.openConnection();
}
catch(IOException e){
}
urlConnection.setDoOutput(true);
try{
((HttpURLConnection)urlConnection).setRequestMethod("POST");
}
catch (Exception e){}
PrintWriter out = null;
try{
out = new PrintWriter(urlConnection.getOutputStream());
}
catch(IOException e){}
out.print("login=zinzalisa&password=vaiovaio&action=login_chk.asp");
// URLEncode.encode("Submit Logon");
out.flush();
out.close();
//

// end of logging in
String urlBase = "http://www.nokri.com/clvi_app.asp?app_id=";
int id_being_tested = 0;
int number_of_failures = 0;
int number_of_successes = 0;


//nokriDataStream.open();
// heading: id firstname lastname city state country email ziporpostalcode industry jobfunction yearsexperience
while (id_being_tested < 10){
String urlString = urlBase + id_being_tested;
URL url2 = null;
try{
url2 = new URL(urlString);
}
catch(java.net.MalformedURLException e){System.out.println("URL2 is fucked - 1");}
catch(IOException e){System.out.println("URL2 katachi is okay but still fucked - 2");}
//System.out.println(urlString); // for testing only
URLConnection urlConnection2 = null;
try{
urlConnection2 = url2.openConnection(); // makes a socket connection to the server
}

catch(IOException e){}
//urlConnection.setAllowUserInteraction(false);
File nokriData = new File("nokriData" + id_being_tested + ".txt" );
FileOutputStream nokriDataStream = null;
try{
nokriDataStream = new FileOutputStream(nokriData);
}
catch(FileNotFoundException e){}
try{
InputStream urlStream = url.openStream();
int b;
while ((b = urlStream.read()) != -1) {
System.out.write(b);
}
// do all the work here, putting the entire page into a
// variable or perhaps something smaller, choppping to save memory
// usage and parsing time
//String id =
//String firstname =
//String lastname =
//String city =
//String state =
//String country =
//String email =
//String ziporpostalcode =
//String industry =
//String jobfunction =
//String yearsexperience =
urlStream.close();
++number_of_successes;
}
catch(IOException e) {
++number_of_failures;
continue;
}
if ((id_being_tested % 100) == 0){
System.out.println("Current id: " + id_being_tested + " Successes: " + number_of_successes +
" failures: " + number_of_failures);
}
id_being_tested = id_being_tested + 1;
} //end infinite while loop
/*
try{
if(nokriDataStream !=null){
nokriDataStream.close(); //close file
}
}
catch(java.io.IOException e){}
*/
}//end main
}//end program
About | Sitemap | Contact