Solved

Posting on rawsugar - ciuly

Posted on 2006-07-01
9
225 Views
Last Modified: 2010-04-05
I`m trying to do an automatic process for this http://www.rawsugar.com/register/step1. There is an image there that I have to load in my app. I`ve done that. it works ok, but when I`m posting data it doesn`t work. and I`m not sure why :(((
if u can check my code and make another plsssssssss ?

var
 viewstate: String;
 count,i,poz:integer;
 cookies:tstringlist;
 Params: TStrings;
 image_url: String;
 temp: String;
 code: String;
 strStream: String;
 memStream: TMemoryStream;
 Image1: TImage;
 jpegimg: TJPEGImage;
 id: String;
 sunview: String;
 L: TStringList;

 procedure setcookies;
 var j:integer;
 begin
   count:=IdCookieManager1.CookieCollection.count;
   for j:=1 to count do
     IdHTTP1.Request.RawHeaders.Add('Cookie'+IdHTTP1.Request.RawHeaders.NameValueSeparator+IdCookieManager1.CookieCollection.Items[j-1].CookieText);
 end;

begin
  L := TStringList.Create;
  Params := TStringList.Create;
  Params.Clear;
  cookies:=tstringlist.Create;
  try

    idhttp1.AllowCookies:=true;
    idhttp1.HandleRedirects:=true;

    html:=idhttp1.Get('http://www.rawsugar.com/');// first get; get first cookie(s)
    count:=IdCookieManager1.CookieCollection.count;
    for i:=1 to count do
      cookies.Add(IdCookieManager1.CookieCollection.Items[i-1].CookieText);

//    HTML := IdHTTP1.Get('http://www.rawsugar.com/actions/logout');

    HTML := IdHTTP1.Get('http://www.rawsugar.com/register/step1');

    temp := HTML;
    i := pos('id="captchaId" value="', temp);
    temp := Copy(temp, i + 22, Length(temp));
    i := pos('"', temp);
    id := Copy(temp, 1, i - 1);
    image_url := 'http://www.rawsugar.com/captcha?c=' + id;
    temp := HTML;
    i := pos('name="com.sun.faces.VIEW" value="', temp);
    temp := Copy(temp, i + 33, Length(temp));
    i := pos('"', temp);
    sunview := Copy(temp, 1, i - 1);

    strStream := IdHTTP1.Get(image_url);
    memStream := TMemoryStream.Create;
    Image1 := TImage.Create(self);

   jpegimg   := TJPEGImage.Create;
   try
      memStream.Write(strStream[1], Length(strStream));
      memStream.Position := 0;
      jpegimg.LoadFromStream(memStream);
      Image1.Picture.Assign(jpegimg);
      Image1.Picture.SaveToFile(IMAGE2_PATH);
    finally
      memStream.Free;
      jpegimg.Free;
    end;
//    UrlDownloadToFile(nil, PChar(image_url), PChar(IMAGE_PATH), 0, nil);
    SecurityForm.file_ext := '.jpg';
    SecurityForm.ShowModal;
    SecurityForm.file_ext := '.png';
    code := SecurityForm.SecurityCode;

    Params.Values['frmRegister:username'] := Account.LoginName;//[put your user name here];
    Params.Values['frmRegister:passcode'] := '';
    Params.Values['frmRegister:fullName'] := Account.FirstName + ' ' + Account.SecondName;
    Params.Values['frmRegister:email'] := Account.Email;
    Params.Values['frmRegister:allowContactByEmail'] := 'on';
    Params.Values['frmRegister:password'] := Account.Password;
    Params.Values['frmRegister:passwordRetype'] := Account.Password;
    Params.Values['frmRegister:captchaUserInput'] := code;
    Params.Values['captchaId'] := id;
    Params.Values['frmRegister:joinIncentiveProgram'] := 'on';
    Params.Values['frmRegister:agreement'] := 'on';
    Params.Values['com.sun.faces.VIEW'] := sunview;
    Params.Values['frmRegister'] := 'frmRegister';
    Params.Values['frmRegister:_idcl'] := '';

  html := '';
    IdHTTP1.HandleRedirects:=false;// now this made the buzz, because the cookies were not set when following the redirect
    try
      HTML := IdHTTP1.Post('http://www.rawsugar.com/pages/register.faces', Params);// now do the log in
    except on e: EIdHTTPProtocolException do
    begin
      if e.ReplyErrorCode<>302 then
         raise e;
        // now this is the redirect
        count:=IdCookieManager1.CookieCollection.count;
        for i:=1 to count do
          cookies.Add(IdCookieManager1.CookieCollection.Items[i-1].CookieText);

        setcookies;
        html:=idhttp1.Get(IdHTTP1.Response.Location);// follow redirect
        Showmessage(IdHTTP1.Response.Location);
      end;
    end;
   finally
   end;
  Params.Free;
  cookies.free;
0
Comment
Question by:crystyan
  • 5
  • 4
9 Comments
 
LVL 28

Expert Comment

by:2266180
ID: 17026232
well, I see that you are not looking at the form code. you need to view source in your browser and then look at what the form has and use that data. in your case, the form is posting to
method="post" action="/pages/register.faces;jsessionid=2F48D892DA9129E48A6356B0E74578AA"
that is not visible in your case.
Also, you should install a tcp or at least a http sniffer to see the trafic with your browser and then make sure your application does the same trafic. (this is generally valid)

plus that I am not sure how moral/correct/etc is to make an auto submitter for registration forms. this is regarded in general as not accepted. why not simply use TWebBrowser or TEmmbedWB for registering?
0
 

Author Comment

by:crystyan
ID: 17026913
hi ciuly,

thanks for advicing me :-)
it still doesn`t work :((( that jsessionid doesn`t appear in the html code (only the first time!!) not sure how they did that, but I got it from the cookies. still when I`m posting to that address it doesn`t work :((. the prob I think is that I don`t post it to the right url as I get the same html as result each time :((

thanks
0
 

Author Comment

by:crystyan
ID: 17026936
lol ... in fact ... I`m not geting the right cookies ... not sure how`s that happening but in my list of cookies there are some missing ... like search.pref and ident . could u look on my code above and see if I`ve missed something ?
THANKS !
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:crystyan
ID: 17026948
I`m not getting all the cookies :(((((((((((
at least when posting from the browser the sniffer shows me more cookies sent there.

and it posts the data at http://www.rawsugar.com/pages/register.faces
0
 
LVL 28

Accepted Solution

by:
2266180 earned 500 total points
ID: 17027458
that is probably because you are getting the cookies only from the last url.
you need to save all coockies from all pages you visit until you get to that form. then you need to parse the form to get the action value wchich will hold a session id (should be different if you do a refresh each time, though I haven't tested that)

in your case you need to get the coockies from http://www.rawsugar.com/register/step1 and any other page it might load either from redirect or as a resource (I've seen cases when loading an image would set a coockie - it's a protection mechanism against crawlers/robots/etc since they don't load images)

if this is the case, that means that the autors of the page do not want any other software interfacing with the registration form in which case it's not morally correct to do what you want. Ands hence, I cannot give you assistance as it is against EE policy.

you still didn't tell me why isn't it ok to use a twebbrowser for the registration phase.
0
 

Author Comment

by:crystyan
ID: 17027491
this is for my personal use, but I don`t wanna cause u any problems. as I may to give it to some friends.

Thanks anyway if u can`t help me. you`re a TERRIFIC programmer ;)
0
 
LVL 28

Expert Comment

by:2266180
ID: 17027532
my best bet is that you see the traffic in the sniffer for all pages that you get and that you send fomr the browser and see whcih one what cookies set.

then make sure your appl;ication does the same.

then check the form source and see if there is anything you need to parse out and use (like viewstate, that session id thing, and maybe who knows what else). also make sure you send out all needed inputs.

from there on, it should work just fine.

that's the best I can give you beside the thing to use twebbrowser for the registration page.
0
 

Author Comment

by:crystyan
ID: 17027536
I don`t wanna user the twebbrowser. I still can`t get the right cookies from step1 page but anyway...I may quit the ideea :-) much more simple lol

THANKS A LOT !
0
 
LVL 28

Expert Comment

by:2266180
ID: 17027542
no problem. sorry I cannot help you. maybe next time :)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

820 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