HTTP redirect?

Hello all,

Im making a simple app that will fill out a login form to login to Yahoo! on their https://login.yahoo.com server. Then, I am trying to get it to redirect me to one of their game room servers. However, I keep getting a page that says, "your browser doesnt support redirects..." something along those lines. But if I open my browser and type in the proper link ( which I am trying to simulate using this app ) everything works.

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.net.CookieHandler;

import java.net.HttpURLConnection;

import java.net.URISyntaxException;

import java.net.URL;

import java.text.DateFormat;

import java.text.SimpleDateFormat;

import java.util.ArrayList;

import java.util.Date;

import java.util.HashMap;

import java.util.Iterator;

import java.util.List;

import java.util.Map;

import java.util.StringTokenizer;

import java.util.Vector;

publicclass Main

{

privatestaticfinal String LOGIN_SERVER ="https://login.yahoo.com";

privatestaticfinal String GAMES_SERVER ="http://games.yahoo.com";

privatestaticfinal String GAME_ROOM ="/games/ante?room=pool_aa&prof_id=chat_pf_1&nosignedcab=yes;

privatestaticfinal String NEW_REDIRECT ="&.done=" + GAME_SERVER + GAME_ROOM;

private String packet;

private ArrayList< String > hiddenInfo;

public Main( String userName, String password )

{

/* CookieManager is an API i got from a website about grabbing

and storing cookies from a connection header. Assume anything

it does is correct.

*/

CookieManager cm =new CookieManager ();

hiddenInfo =new ArrayList<String>();

try

{

URL url =new URL ( LOGIN_SERVER );

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

// Gets the "Set-Cookie" header and stores it

cm.storeCookies (conn);

/* Get the lines of the html code that contain hidden input fields

and stores them in the hiddenInfo arrayList.

*/

getHiddenInfo (conn);

//Takes the hidden fields and parses them into name=value pairs.

parseData ();

/* Sets the 'packet' to contain a url with all of the hidden items

along with the users name and password. A sample packet

would look like this:

"config/login?.tries=1&.src=&.md5=&.hash=&.js=&.last=&promo=

&.intl=us&.bypass=&.partner=&.u=7ti7tg93adntj&.v=0&

.challenge=Z7VIm0YWQyWL2yZ2I.GIn0HLGd_d&.yplus=&.emailCode=

&pkg=&stepid=&.ev=&hasMsgr=0&.chkP=Y&.done=http://my.yahoo.com

&.pd=_ver%3d0%26c=&login=" + yahooID + "&passwd=" + password

+"&.persistent=y"

*/

buildLoginPacket ( yahooID, password );

//Now replace the default redirect with my own

packet = packet.replaceAll ("&.done=http://my.yahoo.com", NEW_REDIRECT );

//Try connecting to the new URL to login and go to the game room.

url =new URL ( LOGIN_SERVER +"/"+ packet );

conn = (HttpURLConnection)url.openConnection ();

conn.setFollowRedirects(true );

/*Set the cookies previously stored or else yahoo sends

you right back to the login page.

*/

cm.setCookies (conn);

//Open an inputstream and print out the reply from the server.

outputResult( conn );

}

catch (IOException ioe)

{

ioe.printStackTrace ();

}

}

}

The server always returns this Page:

<html>

<head>

<script language="JavaScript">

<!--

window.location.replace("http://games.yahoo.com/games/ante?room=pool_aa");

// -->

</script>

<meta http-equiv="Refresh" content="0; url=http://games.yahoo.com/games/ante?room=pool_aa">

</head>

<body>

If you are seeingthis page, your browser settings prevent you

from automatically redirecting to anew URL.

Please <a href="http://games.yahoo.com/games/ante?room=pool_aa">click here</a> to continue.

</body>

</html>

Any ideas to get the redirect?

[6464 byte] By [smithdale87a] at [2007-11-27 11:38:10]
# 1

It might think your code is some kind of bot and is blocking it.

Did you try it on a different browser?

1cMas5_cowa at 2007-7-29 17:18:36 > top of Java-index,Core,Core APIs...
# 2

What I'm confused about is that can type the 'packet' into my browser and change some of the hidden info and it takes me where I want to go. However, that is not the case using the program.

Is this because of some cookie value?Or how might else I attempt this.

Any ideas? Or maybe try writing a simple login/redirect script of your own to try..

smithdale87a at 2007-7-29 17:18:36 > top of Java-index,Core,Core APIs...
# 3

Yahoo requires cookies enabled or it won't work.

Enable cookies in your code, if you can do that.

1cMas5_cowa at 2007-7-29 17:18:36 > top of Java-index,Core,Core APIs...
# 4

But it still could look like a bot to the server.

1cMas5_cowa at 2007-7-29 17:18:36 > top of Java-index,Core,Core APIs...
# 5

I realize that Yahoo requires cookies, hence the calls to cm.storeCookies( conn );

cm.setCookies( conn );

Without these two lines, I never could get past the login page. Everytime I tried to login, I would get the same page back as if my username/pw was incorrect. With the cookie thing enabled, Now when I use the right username/pw I get the page about error redirecting instead of the login page.

However, if I put in the wrong username/pw, i get the login page again ( which is what I expected ).

smithdale87a at 2007-7-29 17:18:36 > top of Java-index,Core,Core APIs...
# 6

i think that it is recognizing your auto login program and thinks it is a bot, thus blocking it.

1cMas5_cowa at 2007-7-29 17:18:36 > top of Java-index,Core,Core APIs...
# 7

Any suggestions on how I would attempt to fool it?

I tried the following:

private static final String USR_AGNT = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)";

//other code

conn.setRequestProperty( "User Agent:", USR_AGNT );

//more code

I hoped this would fool the login into thinking I was an acceptable browser, however I still get the same "cannot redirect" page...

Any other ideas?

smithdale87a at 2007-7-29 17:18:36 > top of Java-index,Core,Core APIs...
# 8

Pretend to be internet Explorer 7. That is the latest version, so perhaps it supports the features it wants.

1cMas5_cowa at 2007-7-29 17:18:36 > top of Java-index,Core,Core APIs...
# 9

You can try logging in through this bot disguising page...

http://www.josiahcole.com/2007/02/

1cMas5_cowa at 2007-7-29 17:18:36 > top of Java-index,Core,Core APIs...
# 10

Ok, Ive gotten my code so that It will get to the next page I have been expecting, after I have logged in. The solution was trivial...

After I made the call to setCookies(conn) to set the "Cookie:" header of the new page, I also needed to make a call to the storeCookies( conn ) to store some new cookies that had been sent.

This new page is one of those pages that has a 'secret word' to enter type thing.

I am able to parse the code to get the link to the image and display it so that I can then enter it into a textbox.

When i get ready to goto the next page, however, using the following link:

http://games.yahoo.com/games/ante?room=pool_19_r&prof_id=chat_pf_1&nosignedcab=yes&testword=" + word.getText ()+"&testdata=" + testData;

where word.getText() is the secret word entered in a text field, and testData is some other long string of characters also stripped from the html.

My feeling is that it has something to do with this, a piece of html from the page..

<input class="text" name="testword" maxlength="15" type="text" accesskey="t" name="p">

If you see, the input field above has two 'name=val' attributes.. This puzzles me.

I have tried using each one in the link I am sending, but neither have worked.. is there some combination I am supposed to use or what? I have never encountered this before, nor am I a html guru.

smithdale87a at 2007-7-29 17:18:36 > top of Java-index,Core,Core APIs...
# 11

Simple. Use them both.

1cMas5_cowa at 2007-7-29 17:18:36 > top of Java-index,Core,Core APIs...
# 12

> conn.setRequestProperty( "User Agent:", USR_AGNT );

Wouldn't the name of the property be "User Agent" and not "User Agent:"?

DrClapa at 2007-7-29 17:18:36 > top of Java-index,Core,Core APIs...
# 13

Alright, I asked one of my buddies who's pretty smart in html/php stuff and he said that the name="p" shouldnt matter, that it's deprecated. So, I continued on my business without using the name=p..

However, no matter what I do, I still cant get the form to go through, I always get sent back to the same page like I did something wrong. I know what i am including all of the proper name=value pairs in the URL and all of my cookies are getting set... what else could be there? =\

By the way, I know this is not the forum to discuss html/form related issues, but it's on the top of the initial post, so I thought id leave it here.

Thanks..

@Dr Clap, You are correct, it should be

conn.setRequestProperty( "User Agent", USR_AGNT );

smithdale87a at 2007-7-29 17:18:36 > top of Java-index,Core,Core APIs...
# 14

Have you tried the Yahoo API for java? It lets you do yahoo stuff in Java.

1cMas5_cowa at 2007-7-29 17:18:36 > top of Java-index,Core,Core APIs...
# 15

None of the Yahoo Api's Ive found have been useful, they are like for performing a yahoo web search, or using yahoo messenger, or yahoo maps, etc. Nothing pertaining to what I need.

smithdale87a at 2007-7-29 17:18:40 > top of Java-index,Core,Core APIs...