How set user-agent on URLOpenBlockingStream RRS feed

  • Question

  • I have this code and I need set custom user-agent
    auto URLDownloadBinary(LPCSTR lpszURL, BYTE **ppBuffer, DWORD *pdwSize) -> int
        LPSTREAM lpStream = 0;
        BYTE *pResult = 0;
        int nResult = 0;
        if (lpszURL && SUCCEEDED(URLOpenBlockingStream(NULL, lpszURL, &lpStream, 0, NULL)))
            STATSTG statStream;
            if (SUCCEEDED(lpStream->Stat(&statStream, STATFLAG_NONAME)))
                DWORD dwSize = statStream.cbSize.LowPart;
                pResult = (BYTE *)malloc(dwSize);
                if (pResult)
                    LARGE_INTEGER liPos;
                    ZeroMemory(&liPos, sizeof(liPos));
                    lpStream->Seek(liPos, STREAM_SEEK_SET, NULL);
                    lpStream->Read(pResult, dwSize, NULL);
                    *ppBuffer = pResult;
                    *pdwSize = dwSize;
                    nResult = 1;
        return nResult;
    How I can do this?
    Saturday, July 21, 2018 7:26 AM

All replies

  • On 7/21/2018 3:26 AM, MininDm wrote:

    I have this code and I need set custom user-agent

    The easiest way is UrlMkSetSessionOption(URLMON_OPTION_USERAGENT). This sets user agent globally for the process, for all URLMon functions.

    If you need it per-request, that's tricky. You need to implement a COM object supporting IBindStatusCallback and IServiceProvider, and pass it as the last parameter of URLOpenBlockingStream. IServiceProvider::QueryService should provide IHttpNegotiate interface. Finally, in IHttpNegotiate::BeginningTransaction you have a chance to set arbitrary HTTP headers, including User-Agent.

    Saturday, July 21, 2018 1:23 PM