none
Read cookie using InternetGetCookie

    Domanda

  • Since IE 9.0.2 update I'm unable reading IE cookie by guessing cookie file name. I know this was dirty method and I understand this IE update is a step forward. Anyway my IE button addon needs to send URL and title of the current tab to my web API plus Windows Live ID's webauthtoken which is stored in a cookie owned by domain where web API runs.

    The first option is find that cookie value in a cookie store. This is my code:

    void MyProject::Exec(String^ url, String^ title) {
        try {
            currentUrl = url;
            currentTitle = title;
    
            // read the token
            if (tokenValue == nullptr) {
    
                LPTSTR lpszData = NULL;   // buffer to hold the cookie data
                DWORD dwSize=0;           // variable to get the buffer size needed
    
                retry:
    
                // The first call to InternetGetCookie will get the required
                // buffer size needed to download the cookie data.
                if (!InternetGetCookie(L"http://my.domain.net/", NULL, lpszData, &dwSize)) {
                    // Check for an insufficient buffer error.
                    if (GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
                        // Allocate the necessary buffer.
                        lpszData = new TCHAR[dwSize];
    
                        // Try the call again.
                        goto retry;
                    } else {
                        // Insert error handling code.
                        throw gcnew Exception("Error");
                    }
                } else {
                    // Insert code to display the cookie data.
    
                    MessageBox(NULL, lpszData, _T("DATA"), 0);
    
                    String^ data = gcnew String(lpszData);
                    bool value = false;
                    for each (System::String^ line in data->Split('\n')) {
                        if (line == _T("webauthtoken")) {
                            value = true;
                        } else if (value && !String::IsNullOrEmpty(line)) {
                            tokenValue = line;
    
                            pin_ptr<const wchar_t> t = PtrToStringChars(tokenValue);
                            MessageBox(NULL, t, _T("TOKEN VALUE"), 0);
    
                            break;
                        } 
                    }
    
                    // Release the memory allocated for the buffer.
                    delete[]lpszData;
                }
            }
    
            // prepare web request
            HttpWebRequest^ request = static_cast<HttpWebRequest^>(WebRequest::Create(L"http://my.domain.net/api/handler.ashx"));
            request->Method = L"POST";
            request->ContentType = L"application/x-www-form-urlencoded";
            request->BeginGetRequestStream(gcnew AsyncCallback(GetRequestStreamCallback), request);
            
        } catch (System::Exception^ ex) {
            pin_ptr<const wchar_t> mgs = PtrToStringChars(ex->Message);
            MessageBox(NULL, mgs, _T("MyProject IE Toolbar Button"), 0);
        } catch (...) {
            MessageBox(NULL, _T("E"), _T("Error"), 0);
        }
    }

    It is just a copy & paste from MSDN, so please ignore goto stuff. The problem is that function InternetGetCookie returns an empty string.

    The second option is send the request via browser so it could send the request with cookies associated with the domain. I didn't find an API which can do this.

    What is the best way in this situation?

    lunedì 5 marzo 2012 18:30

Risposte

  • I found that if the cookie is longer than 4069 bytes, empty string is returned. So this code works:

    DWORD dwSize = 10240; lpszData = new TCHAR[dwSize];

    Is there any simple API to send a HTTP request via IE?

    lunedì 5 marzo 2012 20:24

Tutte le risposte

  • And what did you get in your GetLastError(). Did it shows any error code or anything else. May be it is due to insufficinet buffer . Check it out once again

    Thanks


    Rupesh Shukla

    lunedì 5 marzo 2012 19:04
  • I'm checking GetLastError and ERROR_INSUFFICIENT_BUFFER after the first call to set the buffer size. The second call is successful. MessageBox with caption DATA is shown, but it's content is blank because lpszData is an empty string.
    lunedì 5 marzo 2012 19:21
  • Try this

    LPSTR lpszData =  new char[512]

    DWORD dwsize =512

    instead of

    LPTSTR lpszData = NULL;   // buffer to hold the cookie data
     DWORD dwSize
    =0;           // variable to get the buffer size needed

    And check it once again.

    Thanks


    Rupesh Shukla

    • Proposto come risposta Pintu Shukla martedì 13 marzo 2012 15:27
    lunedì 5 marzo 2012 19:57
  • I found that if the cookie is longer than 4069 bytes, empty string is returned. So this code works:

    DWORD dwSize = 10240; lpszData = new TCHAR[dwSize];

    Is there any simple API to send a HTTP request via IE?

    lunedì 5 marzo 2012 20:24
  •  I have no idea about that or you can say i never tried that but may be you can use HttpOpenRequest and HttpSendRequest  etc. just google for it.

    Thanks


    Rupesh Shukla

    • Proposto come risposta Pintu Shukla martedì 13 marzo 2012 15:28
    lunedì 5 marzo 2012 20:35
  • I found that if the cookie is longer than 4069 bytes, empty string is returned. So this code works:

    DWORD dwSize = 10240; lpszData = new TCHAR[dwSize];

    Is there any simple API to send a HTTP request via IE?

    And i am not sure about this 4096 concept . But you always have to specify the size of the buffer . why not try it with less size like 512 etc i am sure it will work.

    Thanks


    Rupesh Shukla

    lunedì 5 marzo 2012 20:38