Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

HttpClient with Cookies

Posted on 2006-04-13
13
Medium Priority
?
953 Views
Last Modified: 2008-01-09
Hi,
I am writing an application that manages ebay auctions.  Part of the requirements can only be met by login into ebay site.  I have written this code to manage the login but it is not working.  The result is: "Browser Not Accepting Cookies"

Can anyone help?

   HttpClient client = new HttpClient();
   client.getParams().setParameter("http.protocol.single-cookie-header ", "true");
    PostMethod loginMethod = new PostMethod("https://signin.ebay.co.uk/ws/eBayISAPI.dllco_partnerid=2&siteid=3&UsingSSL=0");
    loginMethod.getParams().setCookiePolicy(CookiePolicy.RFC_2109);
    loginMethod.addParameter("userid", "expertsexchangetest");
    loginMethod.addParameter("pass", "experts123");
    loginMethod.addParameter("MfcISAPICommand", "SignInWelcome");
    loginMethod.addParameter("siteid", "3");
    loginMethod.addParameter("UsingSSL", "1");
    loginMethod.addParameter("ru", "");    
    loginMethod.addParameter("pp", "");    
    loginMethod.addParameter("pa1", "");
    loginMethod.addParameter("pa2", "");
    loginMethod.addParameter("pa3", "");
    loginMethod.addParameter("i1", "");
    loginMethod.addParameter("pageType", "");
    loginMethod.addParameter("rtmData", "");

    try
    {
      client.executeMethod(loginMethod);
      System.out.println("authenticaed...");
      System.out.println("*********************");
      System.out.println(loginMethod.getResponseBodyAsString());
    }
    catch (Exception ex)
    {
      ex.printStackTrace();
    }

Any help would be much appreciated

Cheers
Angus
0
Comment
Question by:amacfarl
  • 6
  • 6
13 Comments
 
LVL 92

Expert Comment

by:objects
ID: 16450502
try using CookiePolicy.BROWSER_COMPATIBILITY
and setting User-Agent to mimic a browser
0
 
LVL 2

Author Comment

by:amacfarl
ID: 16450615
I have change the CookiePolicy and unfortunately it did not work.

How do I set the User-Agent to mimic a browser?

The other thing that is concerning me is the FORM that I am copying the variable from.  It has the following:

<form method="post" name="SignInForm" onsubmit="setOptimCookie();" action="https://signin.ebay.co.uk/ws/eBayISAPI.dll?co_partnerid=2&siteid=3&UsingSSL=0">

I have not taken into consideration onsubmit="setOptimCookie();" , actually I cannot find any reference to it in the page.

Any hints?
0
 
LVL 92

Expert Comment

by:objects
ID: 16450655
it'll probably be in a js include, download the js and have a look what it does.
most likely its setting a cookie and is probably the cause of your problem
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 2

Author Comment

by:amacfarl
ID: 16450676
I assume by JS you mean:

https://secureinclude.ebaystatic.com/js/e453/uk/signin/signin_base_e4531uk.js

How can this be downloaded?

Cheers
Angus
0
 
LVL 92

Expert Comment

by:objects
ID: 16450688
same you you download any resource

the function does not appear to be in there, are there any othe js includes, or js on that page
0
 
LVL 2

Author Comment

by:amacfarl
ID: 16450719
There is this one

https://secureinclude.ebaystatic.com/js/e453/uk/signin/signin_body_e4531uk.js

and I have found some references to setOptimCookie(), however I am not sure what it is doing, and whether it is the cause of the problem

//44@@m9
ebay.oDocument.oPage.onBeforeLoad=function()

{var cfg=ebay.oDocument.addConfig(new EBayToolbarConfig("ebaytoolbar"));cfg.signinButtonID="signInButton";cfg.signinFormID="SignInForm";cfg.pageTypes="708,123,706,109,698,121,958,955,867,161,2002,846,1769,1001,697,1828,567,917";this.setOptimCookie();this.initFocus();this.signIntoToolbar();}

ebay.oDocument.oPage.signIntoToolbar=function()

{var cfg=this.parent.getConfig("ebaytoolbar");var toolbar=new EbayToolbar(this,"toolbar",cfg);var form=new EbayHTMLForm(this,cfg.signinFormID);form._registerEvent("onsubmit","signinToolbar");form.signinToolbar=function(){toolbar.onSignIn();}}

ebay.oDocument.oPage.initFocus=function(){var isUsernamePrepopulated=false;var cfg=this.parent.getConfig("signInConfig");if(cfg)

isUsernamePrepopulated=cfg.isUsernamePrepopulated;var de=document.forms["SignInForm"].elements;for(var i=0;i<de.length;i++){if(((de[i].type=="text")||(de[i].name=="pass"&&isUsernamePrepopulated))&&(de[i].value=="")){de[i].focus();break;}}}

ebay.oDocument.oPage.setOptimCookie=function()

{this.parent.oCookieJar.setOptimizationCookie();}

window.init=null;window.setOptimCookie=function(){ebay.oDocument.oPage.setOptimCookie()};

// b=2748702 -->
0
 
LVL 2

Author Comment

by:amacfarl
ID: 16450726
FYI the code that I included above is only the part where I found the references.
0
 
LVL 92

Expert Comment

by:objects
ID: 16450780
looks like they have done there best to hide whats they are doing :)

the following function appears to being used and writing some cookies, there may well be others written.

this.setOptimizationCookie=function()
{with(this.oGlobals.oClient)
{if(((bIE||bNav)&&iVer>=4)||bFirefox||bSafari)
this.writeCookielet("ebay","js","1");}}
this.getDate=function(pYears,pMonths,pDays)
{pYears=pYears?pYears:0;pMonths=pMonths?pMonths:0;pDays=pDays?pDays:0;var dt=new Date(),y=dt.getUTCFullYear(),m=dt.getMonth(),h=dt.getHours();y+=(y<1900)?1900:0;dt.setUTCFullYear(y+pYears);dt.setMonth(m+pMonths);dt.setHours(h+pDays*24);return dt;}
this.getBitFlag=function(pHex,pPos)
{var b=pHex.hex2Dec().toString(2);return pHex?b.charAt(b.length-pPos-1):"";}
this.setBitFlag=function(pHex,pPos,pVal)
{var b="",p,i;if(pHex)
{b=pHex.hex2Dec().toString(2);}
else
{for(i=0;i<=pPos;i++)
{b+="0";}}
p=b.length-pPos-1;return parseInt(b.substring(0,p)+pVal+b.substring(p+1),2).dec2Hex();}}
0
 
LVL 2

Author Comment

by:amacfarl
ID: 16451939
any ideas?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16452124
It would be a lot less time consuming for you simply to use a proxy with your browser and look at what's getting sent then mimic it. Eclipse Solex plugin is what i use
0
 
LVL 92

Accepted Solution

by:
objects earned 2000 total points
ID: 16452162
first load the form and then submit it in the same session that way the cookies should get looked after for you.
if they don't, then try another http client such as httpunit. You'll need to grab the rhino jar for javascript su[pport.

0
 
LVL 2

Author Comment

by:amacfarl
ID: 16452225
http://www.java-tips.org/other-api-tips/httpclient/how-to-use-http-cookies.html

In the link above is the answer!!  Two main problems with the code that I entered:

1) Load the form before hand
2) Set Cookie param at Client level, not Method Level
3) I am just dumb!!!

24 hours this one took for me to solve.  Tried everything.... even OpenSSL for the https connection which was not necessary.

However just a point of information.  OpenSSL has got to be the most unfriendly, screwed up piece of software there is around!!!!!  To install it you need a PHD in IT and be a complete geek.  I have a IT graduate degree and still it was impossible.  Sorry, just venting some anger!!!
Thanks
0
 
LVL 92

Expert Comment

by:objects
ID: 16459766
no worries :)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month20 days, 19 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question