将PHP函数转换为Delphi EQUIVALENT idHTTP SSL和COOKIE组件

时间:2014-11-03 04:39:55

标签: php delphi ssl curl cookies

我尝试翻译这个PHP函数

function checkLogin($username, $password)
{
    $URL = "https://www.yookos.com/cs_login";
    $COOKIE = "cookie_".$username.".txt";

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $URL);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
    curl_setopt($ch, CURLOPT_TIMEOUT, 60);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $COOKIE);
    curl_setopt($ch, CURLOPT_REFERER, $URL);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, "username=".$username."&password=".$password);
    $result = curl_exec ($ch);

    $loggedIn = 0;
    if (strpos($result, "I agree to the Terms & Conditions"))
    {
        // ---- Accept terms and conditions
        curl_setopt($ch, CURLOPT_URL, 'https://www.yookos.com/terms-and-conditions.jspa');
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, "url=&agree=true&method%3Aexecute=Accept");
        $result = curl_exec ($ch);
        $loggedIn = 1;
    } elseif (strpos($result, "/people/".$username)) {
        $loggedIn = 2;
    } else {
        $loggedIn = 3;
    }

    curl_close($ch);
    unlink($COOKIE);

    return $loggedIn;
}

到Delphi XE6:

procedure TfrmMain.prcYOOKOS_AcceptTerms(User, Pass: string);
var
    sResponse: string;
    Param: TStringStream;
    HTTP: TidHTTP;
    SSL: TIdSSLIOHandlerSocketOpenSSL;
    Cookie: TIdCookieManager;
    //U : TidURI;
begin
    //U                  := TidURI.Create('www.yookos.com');
    HTTP               := TIdHTTP.Create(nil);
    SSL                := TIdSSLIOHandlerSocketOpenSSL.Create(HTTP); // nil
    Cookie             := TIdCookieManager.Create(HTTP);

    HTTP.IOHandler     := SSL;
    HTTP.AllowCookies  := True;
    HTTP.CookieManager := Cookie;

    //
    // ---- Login page
    //

    m.Lines.Add('------------------------------------');
    try
        sResponse := HTTP.Get('https://www.yookos.com/login.jspa');
    except
        on E: Exception do
        m.Lines.Add('Error on request 0: ' + e.Message);
    end;
    m.Lines.Add(HTTP.Response.RawHeaders.GetText);
    m.Lines.Add(HTTP.Response.Location);
    m.Lines.Add('------------------------------------');

    //
    // ---- Post DATA
    //

    m.Lines.Add('------------------------------------');
    try
        Param := TStringStream.Create('username='+User+'&password='+Pass, TEncoding.UTF8);
        HTTP.Request.ContentType := 'application/x-www-form-urlencoded';
        HTTP.Request.UserAgent := 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6';  //HTTP.Request.CustomHeaders.AddValue('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6');
        HTTP.Request.Referer := 'https://www.yookos.com/cs_login';
        HTTP.Post('https://www.yookos.com/cs_login', Param);
        FreeAndNil(Param);
    except
    on E: Exception do
        m.Lines.Add('Error on request 1: ' + e.Message);
    end;
    m.Lines.Add(HTTP.Response.RawHeaders.GetText);
    m.Lines.Add(HTTP.Response.Location);
    m.Lines.Add('------------------------------------');

    //
    // ---- Handle "Location:"
    //

    while (Pos('condition',HTTP.Response.Location)=0) and (Pos('login.jspa',HTTP.Response.Location)=0) do
    begin
        m.Lines.Add('------------------------------------');
        try
            //HTTP.CookieManager.AddServerCookie('', U);
             sResponse := HTTP.Get(HTTP.Response.Location);
        except
            on E: Exception do
                m.Lines.Add('Error on request 0: ' + e.Message);
        end;
        m.Lines.Add(HTTP.Response.RawHeaders.GetText);
        m.Lines.Add(HTTP.Response.Location);
        m.Lines.Add('------------------------------------');
    end;

    //
    // ---- ACCEPT TERMS
    //

    m.Lines.Add('------------------------------------');
    try
        Param := TStringStream.Create('url=&agree=true&method:execute=Accept', TEncoding.UTF8);
        sResponse := HTTP.Post('https://www.yookos.com/terms-and-conditions.jspa', Param);
        FreeAndNil(Param);
    except
        on E: Exception do
            m.Lines.Add('Error on request 2: ' + e.Message);
    end;
    m.Lines.Add(HTTP.Response.RawHeaders.GetText);
    m.Lines.Add(HTTP.Response.Location);
    m.Lines.Add('------------------------------------');

    //
    // ---- Free OBJECTS from memory
    //

    FreeAndNil(SSL);
    FreeAndNil(Cookie);
    FreeAndNil(HTTP);
end;

procedure TfrmMain.Button1Click(Sender: TObject);
begin
  prcYOOKOS_AcceptTerms(A.Text, B.Text);
end;        

但是当我测试它时,JSESSIONID不是由应用程序持有,我知道是关于Cookie管理的,但我没有在Delphi上使用cookie和IdHTTP的经验。

我需要知道如何实现EQUIVALENT CURL COOKIEJAR并访问TERMS-AND-CONDITIONS页面......

一些测试帐户:

bamidelebatigo21911
yookos1

ekeminibiney37611
yookos1

sojiedna3511
yookos1

我希望有人可以帮助我。

编辑:

网络活动:

Remote Address:23.216.189.124:443
Request URL:https://www.yookos.com/cs_login
Request Method:POST
Status Code:302 Moved Temporarily

Request Headersview source

Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip,deflate
Accept-Language:pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4
Cache-Control:max-age=0
Connection:keep-alive
Content-Length:49
Content-Type:application/x-www-form-urlencoded
Cookie:__gads=ID=ff29271d05b213c8:T=1414610836:S=ALNI_MYDwjPO57uPfIaGmdVfqnjoWHxsbg; JSESSIONID=4453E0FF613AD064007787E047D0DB01.; jive.security.context="RbpSeLe7t/WPkLYju8/vVkwHJGRKmHXKBce5/ZQD60vRMQ9fvd/9cUbo9eCMywxd.LTE="; BIGipServeryookos-bm-pool=171466762.20480.0000; _ga=GA1.2.1807302990.1414610813; __zlcmid=RceB82JzC9uSzH
Host:www.yookos.com
Origin:https://www.yookos.com
Referer:https://www.yookos.com/login.jspa
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.103 Safari/537.36

Form Dataview sourceview URL encoded

username:bamidelebatigo21911
password:yookos1

Response Headersview source

Cache-Control:no-store, no-cache, must-revalidate, private, max-age=0
Connection:keep-alive
Content-Encoding:gzip
Content-Length:20
Content-Type:text/plain
Date:Mon, 03 Nov 2014 03:06:08 GMT
Expires:Mon, 03 Nov 2014 03:06:08 GMT
Location:https://www.yookos.com/
P3P:CP="CAO PSA OUR"
Server:Apache-Coyote/1.1
Set-Cookie:jive.security.context="0acwivaPVzRzAfYY/qpfog==.NDQ0NzA4NzU="; Version=1; Max-Age=2592000; Expires=Wed, 03-Dec-2014 03:06:08 GMT; Path=/;HttpOnly
Set-Cookie:JSESSIONID=C363F991321B5C68D0A6B31C8600F98F.; Path=/;HttpOnly
Set-Cookie:jive.user.loggedIn=true; Path=/;HttpOnly
Set-Cookie:jive.server.info="serverName=www.yookos.com:serverPort=443:contextPath=:localName=localhost:localPort=9200:localAddr=127.0.0.1"; Version=1; Path=/;HttpOnly
Vary:Accept-Encoding
X-Cnection:close
X-JSL:D=20228 t=1414983968813575

--------------------------------------------------------------------------------

Remote Address:23.216.189.124:443
Request URL:https://www.yookos.com/
Request Method:GET
Status Code:302 Moved Temporarily

Request Headersview source

Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip,deflate
Accept-Language:pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4
Cache-Control:max-age=0
Connection:keep-alive
Cookie:__gads=ID=ff29271d05b213c8:T=1414610836:S=ALNI_MYDwjPO57uPfIaGmdVfqnjoWHxsbg; BIGipServeryookos-bm-pool=171466762.20480.0000; _ga=GA1.2.1807302990.1414610813; __zlcmid=RceB82JzC9uSzH; JSESSIONID=C363F991321B5C68D0A6B31C8600F98F.; jive.user.loggedIn=true; jive.server.info="serverName=www.yookos.com:serverPort=443:contextPath=:localName=localhost:localPort=9200:localAddr=127.0.0.1"; jive.security.context="0acwivaPVzRzAfYY/qpfog==.NDQ0NzA4NzU="
Host:www.yookos.com
Referer:https://www.yookos.com/login.jspa
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.103 Safari/537.36

Response Headersview source

Cache-Control:no-store, no-cache, must-revalidate, private, max-age=0
Connection:keep-alive
Content-Encoding:gzip
Content-Length:20
Content-Type:text/plain
Date:Mon, 03 Nov 2014 03:06:09 GMT
Expires:Mon, 03 Nov 2014 03:06:09 GMT
Location:https://www.yookos.com/terms-and-conditions!input.jspa
P3P:CP="CAO PSA OUR"
Server:Apache-Coyote/1.1
Vary:Accept-Encoding
X-Cnection:close
X-JSL:D=2883 t=1414983969114534

--------------------------------------------------------------------------------

Remote Address:23.216.189.124:443
Request URL:https://www.yookos.com/terms-and-conditions!input.jspa
Request Method:GET
Status Code:200 OK

Request Headersview source

Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip,deflate
Accept-Language:pt-BR,pt;q=0.8,en-US;q=0.6,en;q=0.4
Cache-Control:max-age=0
Connection:keep-alive
Cookie:__gads=ID=ff29271d05b213c8:T=1414610836:S=ALNI_MYDwjPO57uPfIaGmdVfqnjoWHxsbg; BIGipServeryookos-bm-pool=171466762.20480.0000; _ga=GA1.2.1807302990.1414610813; __zlcmid=RceB82JzC9uSzH; JSESSIONID=C363F991321B5C68D0A6B31C8600F98F.; jive.user.loggedIn=true; jive.server.info="serverName=www.yookos.com:serverPort=443:contextPath=:localName=localhost:localPort=9200:localAddr=127.0.0.1"; jive.security.context="0acwivaPVzRzAfYY/qpfog==.NDQ0NzA4NzU="
Host:www.yookos.com
Referer:https://www.yookos.com/login.jspa
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.103 Safari/537.36

Response Headersview source

Cache-Control:no-store, no-cache, must-revalidate, private, max-age=0
Connection:Transfer-Encoding
Connection:keep-alive
Content-Encoding:gzip
Content-Language:en-US
Content-Type:text/html;charset=UTF-8
Date:Mon, 03 Nov 2014 03:06:10 GMT
Expires:Mon, 03 Nov 2014 03:06:09 GMT
P3P:CP="CAO PSA OUR"
Server:Apache-Coyote/1.1
Transfer-Encoding:chunked
Vary:Accept-Encoding
X-Cnection:close
X-JIVE-USER-ID:44470875
X-JSL:D=161278 t=1414983969375279

0 个答案:

没有答案