我使用的Wininet  hSession = InternetOpen(...)
hConn=InternetConnect(...)
hRequest =HttpOpenRequest(...)
.......
ret=InternetCloseHandle(hRequest )
ret=InternetCloseHandle(hConn)
ret=InternetCloseHandle(hSession )上面的三个ret都为TRUE,也就是说关闭成功了
但为什么和服务端的连接还没彻底关闭呢,
因为我发现session仍然保存了,并没有清除。现在的问题就是说,怎么样才能和服务器彻底断开连接,使session清除。

解决方案 »

  1.   

    补充下:如果关闭程序,再打开,session会清空
      

  2.   

    GetInetFile functionuses Wininet;function GetInetFile(const fileURL, FileName: String): boolean;
    const BufferSize = 1024;
    var
    hSession, hURL: HInternet;
    Buffer: array[1..BufferSize] of Byte;
    BufferLen: DWORD;
    f: File;
    sAppName: string;
    begin
    Result:=False;
    sAppName := ExtractFileName(Application.ExeName);
    hSession := InternetOpen(PChar(sAppName),
    INTERNET_OPEN_TYPE_PRECONFIG,
    nil, nil, 0);
    try
    hURL := InternetOpenURL(hSession,
    PChar(fileURL),
    nil,0,0,0);
    try
    AssignFile(f, FileName);
    Rewrite(f,1);
    repeat
    InternetReadFile(hURL, @Buffer,
    SizeOf(Buffer), BufferLen);
    BlockWrite(f, Buffer, BufferLen)
    until BufferLen = 0;
    CloseFile(f);
    Result:=True;
    finally
    InternetCloseHandle(hURL)
    end
    finally
    InternetCloseHandle(hSession)
    end
    end;Usage
    To call the GetInetFile function you could use the next peace of code: 
    var FileOnNet, LocalFileName: string
    begin
    FileOnNet:=
    'http://delphi.about.com/library/forminbpl.zip';
    LocalFileName:='File Downloaded From the Net.zip'if GetInetFile(FileOnNet,LocalFileName)=True then
    ShowMessage('Download successful')
    else
    ShowMessage('Error in file download')end; 
      

  3.   


    谢谢啦,我知道wininet的使用,我只是想知道怎么样能在关闭连接后清除session,
    另外tidhttp控件在销毁后可以清除session
      

  4.   

    1.把idhttp控制項的allowcookie設置為true,和cookiemanger接上。這個控制項會自動記錄cookies, 你post多少次都沒有問題...
    2.因為涉及到網站的登錄,WebBrowser與IE的Session因為屬於不同的進程,目前無法共用。所以沒法彈出。而有時彈出的原因是因為我打開了Web頁面登錄了.分給誰啊.如何讓我的Webbrowser與彈出的IE窗口共用同一Session啊如果不流覽網頁的話可以用TIdHttp進行登陸並偽造Session,流覽的話還是用一Timer刷新頁面這樣這個Session就可以不斷被保持...
      

  5.   

    看一下msdn上对与InternetCloseHandle 的解释,里面说了一些不能被立即关闭的可能,或许这是其中的原因吧
    http://msdn.microsoft.com/en-us/library/aa384350(VS.85).aspx
      

  6.   

    回复 5 楼 kye_jufei :弱弱的问题下,您到底有没有看我的要求啊我是问怎么样才能用wininet关闭连接后清空session,您发了两段代码,压根不着边啊
      

  7.   

    你自己也認真檢查一下嘛
    這樣再試試呢?
    ret=InternetCloseHandle(hRequest )
    ret=InternetCloseHandle(hConn)
    ret=InternetCloseHandle(hSession )如果上述代碼還不能close;那麼你關閉程序,再打開,看一下session是否能清空?
    補充一點的是:session是保存在server上的,有对象生存时间的(也就是它有生命周期)。并不是链接断開,session对象就销毁的我上面提到的delphi的TIdHttp控件可實現internetclosehandle並清空session,你看下它的demo...
      

  8.   

    很多網站使用了session來記錄使用者登錄狀態,idhttp本身是不記錄session的, 需要通過一些技術手段來偽造session。 以下希望對你有用:  
        
    以Get為例,idhttp偽造session 
    IdHttp1.Get(mURL1); 
    mStr := IdHttp1.Response.CustomHeaders.Values['Set-Cookie']; 
    if Pos(';',mStr)>0 then 
    Session_ID := LeftBStr(mStr,Pos(';',mStr)-1) 
    else 
    Session_ID := mStr; IdHttp1.Request.CustomHeaders.Add('Cookie:' + Session_ID); 
    IdHttp1.Get(mURL2);
    ----------------------------------------------
    目前很多網站如百度都禁止indy組 件訪問。因此必須注意UserAgent和http1.1的 設置。
    dHTTP1.Request.Connection:='Keep-Alive';    
    IdHTTP1.Request.UserAgent:='Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon)';    
    IdHTTP1.Request.ContentType:='application/x-www-form-urlencoded';    
    IdHTTP1.Request.Referer:='http://www.xxx.com';    
    IdHTTP1.Request.Accept:='image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/msword, */*';    
    IdHTTP1.Request.AcceptLanguage:='zh-cn';    
    IdHTTP1.Request.AcceptEncoding:='gzip, deflate';    
    IdHTTP1.Request.CacheControl:='no-cache';    
    IdHTTP1.ReadTimeout:=60000;    
    IdHTTP1.HTTPOptions:=IdHTTP1.HTTPOptions+[hoKeepOrigProtocol]; //關鍵這行    
    IdHTTP1.ProtocolVersion:=pv1_1; 
     
      

  9.   

    BOOL InternetCloseHandle(
      __in  HINTERNET hInternet
    );Parameters
    hInternet [in] 
    Handle to be closed.返回true如果成功关闭,否则返回FALSE或处理。为了获得更多的错误信息,调用GetLastError。备注
    该函数终止处理任何悬而未决的行动和抛弃任何悬而未决的数据。如果一个线程是调用Wininet.dll的阻塞,另一个线程可以调用该应用程序在互联网上InternetCloseHandle处理正在使用的第一个线程取消的运作和畅通的第一线。如果有一个状态回调的句柄被关闭注册和处理是一个非空方面的价值,创造一个INTERNET_STATUS_HANDLE_CLOSING将回调。这说明将是最后一个回调从提出和处理表明,正遭到破坏处理。如果是异步请求的处理或处理任何悬而未决的孩子,手柄不能立即关闭,但会失效。任何企图利用新的请求处理将返回一个ERROR_INVALID_HANDLE通知。异步请求将完成INTERNET_STATUS_REQUEST_COMPLETE。应用程序必须准备接受在处理前的最后INTERNET_STATUS_HANDLE_CLOSING作出指示,这表明任何INTERNET_STATUS_REQUEST_COMPLETE迹象表明,是完全封闭处理。应用程序可以调用GetLastError来确定请求是悬而未决。如果GetLastError返回ERROR_IO_PENDING,有优秀的处理时被关闭的要求。