permaculture playing cards*
The moose likes Sockets and Internet Protocols and the fly likes Automated HTTP Session Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Automated HTTP Session" Watch "Automated HTTP Session" New topic
Author

Automated HTTP Session

Stephen Tallamy
Greenhorn

Joined: Jan 08, 2002
Posts: 16
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
Frank Jacobsen
Ranch Hand

Joined: May 17, 2002
Posts: 348
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);
}

}



}
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
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.


I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
Frank Jacobsen
Ranch Hand

Joined: May 17, 2002
Posts: 348
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

Joined: Jun 13, 2000
Posts: 3340
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

Joined: May 17, 2002
Posts: 348
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

Joined: Jun 13, 2000
Posts: 3340
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

Joined: May 17, 2002
Posts: 348
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);
}

}



}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Automated HTTP Session
 
Similar Threads
downloading image in html unit
Any future in IT?
String to StringBuffer to synchronized
default page
ping a website