Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Resolving relative URLs in HttURLConnection response message  RSS feed

 
Anirudh Sonthalia
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys

I've a problem. I've to use java.net.HttpURLConnection to programmatically login to some web sites. Now the big problem that I'm facing is that if those websites have some relative URLs in their login pages then when I read that login page's code through HttpURLConnection, those URLs are not recognized and I get an error saying web group not found for those relative URLs. Now I checked out the java.net package, I couldn't find any solution to this, I hope I didn't overlooked anything here. Can you please suggest. Also, not just this, but for lot of web sites where I 've to achieve Single Sign On using WebSphere Portal Credential vault(which just stores user/password information), the loging is not having even though there are no relative URLs, I dont know why, lot of them have some active controls on their login pages, others have multiple screens to take user/password information, I'm trying to find a workaround.

Any help here woul dbe really appreciated. It's been long that I've been trying to find a solution to this.

The following is the code I'm using :

HttpURLConnection connection = getConnectionUsingPassiveObject(request, null, host, proxy, port, path, slotId, protocol);



if (connection != null)
{
System.out.println("Connected found not null");
connection.connect();
System.out.println( "Connected successfully" );

connection.setFollowRedirects( false );
connection.setInstanceFollowRedirects( false );

//connection.
System.out.println( "connection.getFollowRedirects() : " + connection.getFollowRedirects() );
System.out.println( "connection.getInstanceFollowRedirects() : " + connection.getInstanceFollowRedirects() );
System.out.println( "connection.usingProxy() : " + connection.usingProxy() );



Object content = connection.getContent();
String contentType = connection.getContentType();
FileNameMap fMap = connection.getFileNameMap();

Class class1 = content.getClass();
Field[] fields = class1.getDeclaredFields();
Method[] methods = class1.getDeclaredMethods();
for(int i=0;i<fields.length;i++)
{
System.out.println("field[" + i +"] : " + fields[i] );

}
for(int i=0;i<methods.length;i++)
{
System.out.println("method[" + i + "] : " + methods[i] );

}
//System.out.println("methods : " + methods );

connection.usingProxy();
//connection.

System.out.println("Content : " + content );
System.out.println("ContentType : " + contentType );
System.out.println("fMap : " + fMap);

String responseMessage = connection.getResponseMessage();
int responseCode = connection.getResponseCode();
System.out.println( " connection.getHeaderFields() : " + connection.getHeaderFields() );
//System.out.println( " connection.getRequestProperties() : " + connection.getRequestProperties() );
//connection.
//Were we successful?
if (HttpURLConnection.HTTP_OK == responseCode)
{
System.out.println("HttpResponseCode found OK");
result.append("<H4>Successfully connected!</H4>");
}
else
{
System.out.println("HttpResponseCode found not OK");
result.append(
"<P>Unable to successfully connect to back-end server, HTTP Response Code = "
+ responseCode + ", HTTP Response Message = \""
+ responseMessage + "\"</P>"
);
response.getWriter().write( result.toString() );

}

BufferedReader br =new BufferedReader(
new InputStreamReader(connection.getInputStream())
);
System.out.println( "br : " + br );

String line = null;
while ((line = br.readLine()) != null)
{
System.out.println( "br.readLine() : " + line );
result.append(line + "\n");
}

response.getWriter().write( result.toString() );

}//if (connection != null) ends
else
{
System.out.println("Connection found null.");
result.append("<h2>Credential not found. Please set it in edit mode!</h2>");
response.getWriter().write( result.toString() );
}


---------X---------X--------------X------------------X----------------

public static HttpURLConnection getConnectionUsingPassiveObject
(
PortletRequest portletRequest,
AveryCredVaultDemoPortletSessionBean sessionBean,
String host,
String proxy,
String port,
String path,
String slotID,
String protocol
)
{
String userId = null;
String password = null;
HttpURLConnection connection = null;

try
{
//getCredential(portletRequest, sessionBean, userid, password);

UserPasswordPassiveCredential cred =
(UserPasswordPassiveCredential) vaultService.getCredential( slotID, "UserPasswordPassive", null, portletRequest );

if ( cred != null )
{
System.out.println("Credential Slot Found successfully");
char[] charArr = cred.getPassword();
password = "";
for(int i=0; i<charArr.length; i++)
{
//System.out.println( "char[" + i + "]" + charArr[i] );
password = password + charArr[i];
}
//System.out.println( "password : " + password );
//password = cred.getPassword() != null ? cred.getPassword().toString() : "";
userId = cred.getUserId();
}
else
{
System.out.println("Credential Slot Couldn't be found");
}

if ( !"".equals( userId ) )
{
String userAndPassword = new String( userId.toString() + ":" + password.toString() );
byte[] userAndPasswordBytes = userAndPassword.getBytes();

//System.out.println( "user assword : " + userAndPassword );

BASE64Encoder encoder = new BASE64Encoder();

String basicAuth = new String( encoder.encode( userAndPasswordBytes ) );
basicAuth = "Basic " + basicAuth;

//System.out.println( "URL : " + protocol + host + ":" + port + path );

URL url = null;
System.out.println( "PortNo : " + port );

String portStr = "";

if( port != null && !"".equals( port.trim() ) && !"-1".equals( port.trim() ) )
{
portStr = ":" + port;
}

System.out.println( "URL : " + protocol + host + portStr + path );
url = new URL( protocol + host + portStr + path );

/*if( proxy != null && !"".equals( proxy.trim() ) && !"null".equals( proxy.trim() ) )
{
System.out.println( "URL : " + protocol + "," + proxy + "," + Integer.parseInt(port) + "," + host + path );
url = new URL( protocol, proxy, Integer.parseInt(port), host + path );
}
else
{
System.out.println( "URL : " + protocol + host + portStr + path );
url = new URL( protocol + host + portStr + path );
}*/


int proxyPortNo = 8080;
// "http://"
/*if( proxy != null && !"".equals( proxy.trim() ) && !"null".equals( proxy.trim() ) )
{
System.out.println("Opening URL Connection through Proxy");
connection = (HttpURLConnection) url.openConnection( new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxy, proxyPortNo) ) );
}
else
{
connection = (HttpURLConnection) url.openConnection();
}*/

connection = (HttpURLConnection) url.openConnection();

connection.setRequestProperty( "authorization", basicAuth );
}
}
catch (Exception e)
{
e.printStackTrace();
}

return connection;
}

----------------------X------------------------X----------------------
So, if somebody can comment on this it'll be great help. Also I couldn't find a way to make a URL connection through a proxy, I know that in JDK 5.0 we can do this, by using URL.openConnection(Proxy), but no soln in jdk 1.4, which is the version of our server where I've to do this thing.
So this is another problem.

I tried looking into Apache HttpClient, HttpCore, but no soln yet.

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65826
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please be sure to use UBB code tags when posting code to the forums. Unformatted code is extremely hard to read and many people that might be able to help you will just move along. Please read this for more information.

You can go back and change your post to add code tags by clicking the .
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's too much code to try to understand, especially is it's illegible, as Bear pointed out.

For programmatic access to web sites I recommend the jWebUnit library (on SourceForge). It solves all the HTTP request/response problems you're struggling with.
 
Anirudh Sonthalia
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bear

I'm sorry, I'm not sure what you mean by UBB code tags, anyways, it this post looks less readable I will definitely work on it from the next time.

Thanks for your input Ulf, I'll check out jWebUnit.
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Anirudh Sonthalia:
I'm not sure what you mean by UBB code tags

Did you read the link Bear posted? It explains this.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!