Hola de nuevo,
yelian.
Si tu programa parece "colgado" durante la descarga, quizás deberías pensar en colocar tu código de descarga en un hilo a parte. Otra posibilidad sería establecer un IBindStatusCallback y colocar Application->ProcessMessages(); en el evento OnProgress.
Para el problema del proxy, debes realizar una autentificación previa a la descarga. Te dejo un ejemplo:
Código:
//---------------------------------------------------------------------------
bool Authenticate(HWND hWnd, char *URL)
{
HINTERNET hOpenHandle, hConnectHandle, hResourceHandle;
DWORD dwError, dwErrorCode;
char c;
URL_COMPONENTS URL_C = {sizeof(URL_COMPONENTS)};
URL_C.lpszScheme = &c;
URL_C.dwSchemeLength = 1;
URL_C.lpszHostName = &c;
// URL_C.dwHostNameLength = 0;
URL_C.lpszUserName = &c;
// URL_C.dwUserNameLength = 0;
URL_C.lpszPassword = &c;
// URL_C.dwPasswordLength = 0;
URL_C.lpszUrlPath = &c;
// URL_C.dwUrlPathLength = 0;
URL_C.lpszExtraInfo = &c;
// URL_C.dwExtraInfoLength = 0;
InternetCrackUrl(URL, strlen(URL), ICU_DECODE, &URL_C);
URL_C.lpszScheme = new char[URL_C.dwSchemeLength];
URL_C.lpszHostName = new char[URL_C.dwHostNameLength];
URL_C.lpszUserName = new char[URL_C.dwUserNameLength];
URL_C.lpszPassword = new char[URL_C.dwPasswordLength];
URL_C.lpszUrlPath = new char[URL_C.dwUrlPathLength];
URL_C.lpszExtraInfo = new char[URL_C.dwExtraInfoLength];
InternetCrackUrl(URL, strlen(URL), ICU_DECODE, &URL_C);
hOpenHandle = InternetOpen("Agent", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
hConnectHandle = InternetConnect(hOpenHandle,
URL_C.lpszHostName,
INTERNET_INVALID_PORT_NUMBER,
NULL,
NULL,
INTERNET_SERVICE_HTTP,
0,0);
hResourceHandle = HttpOpenRequest(hConnectHandle, "GET",
URL_C.lpszUrlPath,
NULL, NULL, NULL,
INTERNET_FLAG_KEEP_CONNECTION, 0);
delete URL_C.lpszScheme;
delete URL_C.lpszHostName;
delete URL_C.lpszUserName;
delete URL_C.lpszPassword;
delete URL_C.lpszUrlPath;
delete URL_C.lpszExtraInfo;
for(int n=0; n<2; n++){
HttpSendRequest(hResourceHandle, NULL, 0, NULL, 0);
dwErrorCode = hResourceHandle ? ERROR_SUCCESS : GetLastError();
dwError = InternetErrorDlg(hWnd, hResourceHandle, dwErrorCode,
FLAGS_ERROR_UI_FILTER_FOR_ERRORS |
FLAGS_ERROR_UI_FLAGS_CHANGE_OPTIONS |
FLAGS_ERROR_UI_FLAGS_GENERATE_DATA,
NULL);
if (dwError != ERROR_INTERNET_FORCE_RETRY)
break;
}
return (dwError != ERROR_INTERNET_FORCE_RETRY);}
Y como usarlo en una descarga:
Código:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
char File[] = "http://www.terawiki.clubdelphi.com/CPPBuilderWin32/Ejemplos/?download=Otro+Usuario.rar";
HRESULT R;
Authenticate(Handle, File);
R = URLDownloadToFile(0, File, "C:\\File.rar", 0, 0);
}
Saludos.
PD He optimizado un poco el código.