Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Gets a internet page through HTTP proxy

Gets a internet page through HTTP proxy

Scheduled Pinned Locked Moved C / C++ / MFC
securitycomsysadmintutorialquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    Eugene Pustovoyt
    wrote on last edited by
    #1

    Here sample of a code to gets a page through proxy with authorizing. First ::HttpSendRequest call returns success, but second call after proxy authorization is wedging. Why!? Where is mistake!?

    HINTERNET hOpenHandle, hConnectHandle, hResourceHandle;
    DWORD dwError, dwStatus;
    DWORD dwStatusSize = sizeof(dwStatus);
    hOpenHandle = ::InternetOpen("Example", INTERNET_OPEN_TYPE_PROXY, strProxyName, NULL, 0);
    if (NULL != hOpenHandle)
    {
    hConnectHandle = ::InternetConnect(hOpenHandle, "www.microsoft.com", INTERNET_INVALID_PORT_NUMBER, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
    if (NULL != hConnectHandle)
    {
    hResourceHandle = ::HttpOpenRequest(hConnectHandle, "GET", "", HTTP_VERSION, NULL, 0, INTERNET_FLAG_DONT_CACHE, 0);
    if (NULL != hResourceHandle)
    {
    resend:
    BOOL bSendRequest = ::HttpSendRequest(hResourceHandle, NULL, 0, NULL, 0);
    ::HttpQueryInfo(hResourceHandle, HTTP_QUERY_FLAG_NUMBER | HTTP_QUERY_STATUS_CODE, &dwStatus, &dwStatusSize, NULL);
    switch (dwStatus)
    {
    case HTTP_STATUS_PROXY_AUTH_REQ:
    //Proxy Authentication Required
    // Insert code to set strUsername and strPassword.
    InternetSetOption(hResourceHandle, INTERNET_OPTION_PROXY_USERNAME, (LPVOID)(LPCTSTR)strProxyUsername, strlen(strProxyUsername)+1);
    InternetSetOption(hResourceHandle, INTERNET_OPTION_PROXY_PASSWORD, (LPVOID)(LPCTSTR)strProxyPassword, strlen(strProxyPassword)+1);
    goto resend;
    break;
    case HTTP_STATUS_DENIED:
    //Server Authentication Required
    // Insert code to set strUsername and strPassword.
    InternetSetOption(hResourceHandle, INTERNET_OPTION_PROXY_USERNAME, (LPVOID)(LPCTSTR)strProxyUsername, strlen(strProxyUsername)+1);
    InternetSetOption(hResourceHandle, INTERNET_OPTION_PROXY_PASSWORD, (LPVOID)(LPCTSTR)strProxyPassword, strlen(strProxyPassword)+1);
    goto resend;
    break;
    }
    // Insert code to read the information from the hResourceHandle
    // at this point.
    if (HTTP_STATUS_OK == dwStatus)
    {
    //Gets a page
    }
    ::InternetCloseHandle(hResourceHandle);
    }
    ::InternetCloseHandle(hConnectHandle);
    }
    ::InternetCloseHandle(hOpenHandle);
    }

    Best regards, Eugene Pustovoyt

    Y 1 Reply Last reply
    0
    • E Eugene Pustovoyt

      Here sample of a code to gets a page through proxy with authorizing. First ::HttpSendRequest call returns success, but second call after proxy authorization is wedging. Why!? Where is mistake!?

      HINTERNET hOpenHandle, hConnectHandle, hResourceHandle;
      DWORD dwError, dwStatus;
      DWORD dwStatusSize = sizeof(dwStatus);
      hOpenHandle = ::InternetOpen("Example", INTERNET_OPEN_TYPE_PROXY, strProxyName, NULL, 0);
      if (NULL != hOpenHandle)
      {
      hConnectHandle = ::InternetConnect(hOpenHandle, "www.microsoft.com", INTERNET_INVALID_PORT_NUMBER, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
      if (NULL != hConnectHandle)
      {
      hResourceHandle = ::HttpOpenRequest(hConnectHandle, "GET", "", HTTP_VERSION, NULL, 0, INTERNET_FLAG_DONT_CACHE, 0);
      if (NULL != hResourceHandle)
      {
      resend:
      BOOL bSendRequest = ::HttpSendRequest(hResourceHandle, NULL, 0, NULL, 0);
      ::HttpQueryInfo(hResourceHandle, HTTP_QUERY_FLAG_NUMBER | HTTP_QUERY_STATUS_CODE, &dwStatus, &dwStatusSize, NULL);
      switch (dwStatus)
      {
      case HTTP_STATUS_PROXY_AUTH_REQ:
      //Proxy Authentication Required
      // Insert code to set strUsername and strPassword.
      InternetSetOption(hResourceHandle, INTERNET_OPTION_PROXY_USERNAME, (LPVOID)(LPCTSTR)strProxyUsername, strlen(strProxyUsername)+1);
      InternetSetOption(hResourceHandle, INTERNET_OPTION_PROXY_PASSWORD, (LPVOID)(LPCTSTR)strProxyPassword, strlen(strProxyPassword)+1);
      goto resend;
      break;
      case HTTP_STATUS_DENIED:
      //Server Authentication Required
      // Insert code to set strUsername and strPassword.
      InternetSetOption(hResourceHandle, INTERNET_OPTION_PROXY_USERNAME, (LPVOID)(LPCTSTR)strProxyUsername, strlen(strProxyUsername)+1);
      InternetSetOption(hResourceHandle, INTERNET_OPTION_PROXY_PASSWORD, (LPVOID)(LPCTSTR)strProxyPassword, strlen(strProxyPassword)+1);
      goto resend;
      break;
      }
      // Insert code to read the information from the hResourceHandle
      // at this point.
      if (HTTP_STATUS_OK == dwStatus)
      {
      //Gets a page
      }
      ::InternetCloseHandle(hResourceHandle);
      }
      ::InternetCloseHandle(hConnectHandle);
      }
      ::InternetCloseHandle(hOpenHandle);
      }

      Best regards, Eugene Pustovoyt

      Y Offline
      Y Offline
      YaronNir
      wrote on last edited by
      #2

      take a look at this example (listed at the bottom of the page) that is shown in MSDN: http://msdn.microsoft.com/en-us/library/aa384220%28VS.85%29.aspx[^]

      Interface basics click here : http://www.codeproject.com/com/COMBasics.asp don't forget to vote :)

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups