Ir al contenido


Foto

Saber si hay conexión de internet


  • Por favor identifícate para responder
23 respuestas en este tema

#21 enecumene

enecumene

    Webmaster

  • Administrador
  • 7.419 mensajes
  • LocationRepública Dominicana

Escrito 14 abril 2009 - 01:21

Hola escafandra, me imagino que su uso serí­a de esta manera:


cpp
  1. InternetReadWeb('http://www.google.com', ?, ?)


Pero...¿qué valores se colocan en buffer y size?.

Saludos.
  • 0

#22 escafandra

escafandra

    Advanced Member

  • Administrador
  • 4.107 mensajes
  • LocationMadrid - España

Escrito 14 abril 2009 - 01:53


Hola escafandra, me imagino que su uso serí­a de esta manera:


cpp
  1. InternetReadWeb('http://www.google.com', ?, ?)


Pero...¿qué valores se colocan en buffer y size?.

Saludos.


Creas un buffer de valores tipo TCHAR o BYTE que es lo mismo y size es el tamaño de dicho buffer.

Ejemplo:


cpp
  1. char Buffer[10];
  2. InternetReadWeb("http://www.google.com", Buffer, 10)


Esto lee los 10 primeros Bytes de http://www.google.com.

Saludos.

 
  • 0

#23 escafandra

escafandra

    Advanced Member

  • Administrador
  • 4.107 mensajes
  • LocationMadrid - España

Escrito 14 abril 2009 - 02:05

Aquí­ he dejado un ejemplo para realizar un Ping sin componentes, sólo con API y en C. Aquí­ dejo otras funciones de apoyo.

Saludos.


  • 0

#24 escafandra

escafandra

    Advanced Member

  • Administrador
  • 4.107 mensajes
  • LocationMadrid - España

Escrito 07 septiembre 2011 - 03:53

Revivo este tema tras la pregunta realizada por mediazg en este otro hilo.

Para responder a la segunda pregunta que formulada mediazg acerca de cómo saber si tenemos conexión a Internet estando detrás de un proxy, he escrito un código y una aplicación de ejemplo que realiza esa labor:


delphi
  1.   uses Windows, Wininet;
  2.  
  3.  
  4. function Authenticate(URL, UserName, PassWord: String): boolean;
  5. var
  6.   hInet, hConnect, hRequest: HINTERNET;
  7.   dwStatus, StatusSize: DWORD;
  8.   n: integer;
  9.   URL_C: URL_COMPONENTS;
  10. begin
  11.   StatusSize:= sizeof(dwStatus);
  12.   ZeroMemory(@URL_C, sizeof(URL_COMPONENTS));
  13.   URL_C.dwStructSize:= sizeof(URL_COMPONENTS);
  14.   URL_C.dwHostNameLength:= INTERNET_MAX_HOST_NAME_LENGTH;
  15.   InternetCrackUrl(PCHAR(URL), Length(URL)+1, 0, URL_C);
  16.  
  17.   hInet:= InternetOpen('agent', INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
  18.   hConnect:= InternetConnect(hInet, URL_C.lpszHostName,
  19.                               INTERNET_INVALID_PORT_NUMBER, nil, nil,
  20.                               INTERNET_SERVICE_HTTP, 0,0);
  21.  
  22.   hRequest:= HttpOpenRequest(hConnect, 'GET', URL_C.lpszHostName,
  23.                               nil, nil, nil, INTERNET_FLAG_KEEP_CONNECTION, 0);
  24.  
  25.   for n:=0 to 1 do
  26.   begin
  27.       HttpSendRequest(hRequest, nil, 0, nil, 0);
  28.       HttpQueryInfo(hRequest, HTTP_QUERY_FLAG_NUMBER or
  29.                     HTTP_QUERY_STATUS_CODE, @dwStatus, StatusSize, PCardinal(0)^);
  30.       // Se requiere autentificación en el Proxy o en el servidor
  31.       if (dwStatus = HTTP_STATUS_PROXY_AUTH_REQ) or (dwStatus = HTTP_STATUS_DENIED) then
  32.       begin
  33.         // Introducimos UserName
  34.         InternetSetOption(hRequest, INTERNET_OPTION_PROXY_USERNAME, PCHAR(UserName), Length(UserName) + 1);
  35.  
  36.         // Introducimos el Password
  37.         InternetSetOption(hRequest, INTERNET_OPTION_PROXY_PASSWORD, PCHAR(PassWord), Length(PassWord) + 1);
  38.       end;
  39.   end;
  40.   InternetCloseHandle(hInet);
  41.   InternetCloseHandle(hConnect);
  42.   InternetCloseHandle(hRequest);
  43.   Result:= (dwStatus <> HTTP_STATUS_PROXY_AUTH_REQ) and (dwStatus <> HTTP_STATUS_DENIED);
  44. end;
  45.  
  46. function WebIng(URL: PCHAR): boolean;
  47. var
  48.   hNet, hUrl: Pointer;
  49.   BytesRead: DWORD;
  50.   Buffer: array [0..64] of char;
  51. begin
  52.   Result:= false;
  53.   BytesRead:= 0;
  54.  
  55.   if InternetAttemptConnect(0) <> ERROR_SUCCESS then exit;
  56.  
  57.   hNet:= InternetOpen('WebIng', INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
  58.   if hNet <> nil then
  59.   begin
  60.     hUrl:= InternetOpenUrl(hNet, URL, nil, 0, INTERNET_FLAG_RELOAD {or INTERNET_FLAG_NO_AUTH}, 0);
  61.     if hUrl <> nil then
  62.     begin
  63.       ZeroMemory(@Buffer[0], sizeof(Buffer));
  64.       Result:= InternetReadFile(hUrl, @Buffer[0], sizeof(Buffer), BytesRead);
  65.       Result:= Result and (BytesRead > 0);
  66.       InternetCloseHandle(hUrl);
  67.     end;
  68.     InternetCloseHandle(hNet);
  69.   end;
  70.   Result:= Result and (Pos('Access Denied', Buffer) = 0);
  71. end;


Ejemplo de uso:

delphi
  1.   Authenticate('[url=http://www.google.com]www.google.com[/url]',  'User', 'Password');
  2.   if WebIng('[url=http://www.google.com]www.google.com[/url]') then
  3.     Label1.Caption:= 'Exito'
  4.   else
  5.     Label1.Caption:= 'Fracaso';



Subo el código.

Espero que sea de utilidad.
  • 0




IP.Board spam blocked by CleanTalk.