Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Automated HTTP Session

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Can anyone help me solve the following problem:
I need to connect to a website, login using a username and password, and then perform a number of actions on the website - e.g. navigate to a page & post some information. How do I do this using Java classes?
Stephen
 
Ranch Hand
Posts: 367
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have the same problem, i get a connection to the
login page, whit this code !
I got into the login page, but when i tried to login with this program, i dont get any exception, but the html i recieved at last is still from the login page !
import java.io.*;
import java.net.*;
import java.util.*;


public class SocketTest
{ public static void main(String[] args){

String ReturnBuffer = "";


try
{ String urlName;
if (args.length > 0)
urlName = args[0];
else
urlName = "http://someurl";



URL url = new URL(urlName);
HttpURLConnection connection =(HttpURLConnection) url.openConnection();

String body = "userid=usetdc100&password=poluli&send=Logon";



connection.setRequestMethod("POST");
connection.setAllowUserInteraction(false);
connection.setDoOutput(true);
connection.setRequestProperty("Content-length", Integer.toString(body.length()) );



//Send request

try {
PrintWriter out = new PrintWriter(connection.getOutputStream(),true);
out.print(body);
out.flush();
out.close();

}
catch(Exception e)
{
System.out.println("Exception ved send request: " + e);
}



//Recive the answer.
try
{
BufferedReader in;
in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
boolean more = true;

while (more)
{
char ch = (char) in.read() ;
ReturnBuffer += ch;
System.out.print(ch);
if(!in.ready()) more = false;
}
}
catch(IOException e)
{
System.out.println("Exceptionved send request: " + e);
}

connection.disconnect();


}
catch(Exception e)
{
System.out.println("Exception ved send request: " + e);
}

}



}
 
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Breifly, (and this actually gets easier in JDK 1.4)
When you make the first connection and get the result back. Loop through the header fields using getHeaderFieldKey(int pos) and getHeaderField(int pos) looking for a header field key named "Set-Cookie" with a value that starts "JSESSIONID=" Save that value. On your next request to that server, right after you call open connection but before you read anything call: setRequestProperty(String key, String value) and your next request is now part of the orginal session.
 
Frank Jacobsen
Ranch Hand
Posts: 367
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I still have the same problem, with ore without using cokies, the first output is:
cookie=:sesessionid=4NGBORAAAAKD5A3GCIG0AAA
null=:HTTP/1.1 200 ok
Server=:Netscape-Enterprise/3.5.1
Date=:Tue, 21 May 2002 11:40:05 GMT
Content-Type=:text/html
Set-Cookie=:sesessionid=4NGBORAAAAKD5A3GCIG0AAA;Path=/
Cache-Control=:no-cache="set-cookie,set-cookie2"
Expires=:Thu, 01 Dec 1994 16:00:00 GMT
Content-Language= a
Connection=:close
but the html i recieved in the buttom of the program, is still the html from the login page !

[ May 22, 2002: Message edited by: Carl Trusiak ]
 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see I miss spoke, The server sends Set-Cookie the first time it sends it. Communication thereafter sends the header name as "Cookie" It must caontain all the data passed with the set cookie (including path information)
 
Frank Jacobsen
Ranch Hand
Posts: 367
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As you se in my java code i tried to day, to make a cokie just like the one i recieved and send this back, but still i get the HTML from the login page !!!

[ May 22, 2002: Message edited by: Carl Trusiak ]
 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your application doen't look like it's following the flow of the web app!
1) you request the page and get the result page back (log-in) this may or may not have the session info
2) you submit the login form and get back the next page. This may or may not be the inital page you requested. If the set cookie wasn't returned in 1 it should be returned now.
3) if the page returned in 2 wasn't the page you wanted request the page and get the result back.
4) submit the request .....
Your app has to interact with the web app EXACTLY as a user browsing it with a browser. Your trying to grab the session, login and do the next set in one request. The webapp doesn't behave this way!
 
Frank Jacobsen
Ranch Hand
Posts: 367
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for helping out Carl, its have been a great help, here comes the code that works. The program log�s in to a webserver, and with that cokie i go to a other plase and "GET" a telephone number, and there i get the HTML from that page back.
I don�t use POST anymore !
Frank Jacobsen
Frja@tdk.dk
mport java.io.*;
import java.net.*;
import java.util.*;

public class SocketTest
{

public static void main(String[] args){

String ReturnBuffer = "";
String search_msisdn = "20100400" ;

try
{
System.setProperty("proxySet", "true");
System.setProperty("http.proxyHost", "192.168.190.31");
System.setProperty("http.proxyPort", "8080");

String urlName;
if (args.length > 0){
urlName = args[0];
}
else{
urlName = "http://someip/webapp/OCH2WebApp/index.jsp?userid=usetdc100&password=poluli&send=Login";
}
URL url = new URL(urlName);

HttpURLConnection connection =(HttpURLConnection) url.openConnection();

String cookie = connection.getHeaderField("Set-Cookie");
/*
if ( cookie != null ){
int idx = cookie.indexOf(";");
if ( idx >= 0 ){
cookie = cookie.substring(0, idx);
}
}
*/
/*
System.out.println("cookie=:" + cookie);
for (int i=0; ; i++){
String headerName = connection.getHeaderFieldKey(i);
String headerValue = connection.getHeaderField(i);

if ( headerName == null && headerValue == null ){
break;
}
if ( headerName == null ){
// The header value contains the server's HTTP version
}
System.out.println(headerName + "=:" + headerValue);
}
*/
connection =(HttpURLConnection) url.openConnection();
connection.setRequestProperty("Cookie", cookie);
// Simulate a communication:
try
{

BufferedReader in;
in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
}
catch(IOException e)
{
System.out.println("Exception ved get request: " + e);
}

urlName = "http://131.166.18.13/webapp/OCH2WebApp/telephonenumbercurrentstatus.jsp";
url = new URL(urlName);
connection =(HttpURLConnection) url.openConnection();
connection.setRequestProperty("Cookie", cookie);

// Simulate a communication:
try
{

BufferedReader in;
in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
}
catch(IOException e)
{
System.out.println("Exception ved get request: " + e);
}
// Do the search on the remote server:
urlName = "http://131.166.18.13/webapp/OCH2WebApp/telephonenumbercurrentstatus.jsp?telephoneNumberStart="
+ search_msisdn
+ "&Search=Search";
url = new URL(urlName);
connection =(HttpURLConnection) url.openConnection();
connection.setRequestProperty("Cookie", cookie);
//Receive the answer.
try
{

BufferedReader in;
in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
boolean more = true;

while (more)
{
char ch = (char) in.read() ;
ReturnBuffer += ch;
System.out.print(ch);
if(!in.ready()) more = false;
}
}
catch(IOException e)
{
System.out.println("Exceptionved send request: " + e);
}

connection.disconnect();


}
catch(Exception e)
{
System.out.println("Exception ved send request: " + e);
}

}



}
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic