Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I'm new in programming and i wanted to make my app using C++ download data behind proxy,
i just detect if the user is behind proxy by request like this code:

C++
int CWeatherInfoDlg::FindWebAddress(char *szAuthLine)
{
	DWORD dwRet = 0;
	BOOL bResult;
	char szTemp[256];
	struct in_addr inAddr;
	CString sAddr;
	DWORD dwIPNr;

	TRACE("FindWebAddress()\n");
	//	We want to open a session to checkip.dyndns.org, because in the HTML source this site returns
	//	it reports our hostname and IP-number as the site sees us.
	CInternetSession *pSession = new CInternetSession(_T("GetIPNr"), 1, INTERNET_OPEN_TYPE_PRECONFIG);
	CHttpConnection* pConnection = pSession->GetHttpConnection(_T("checkip.dyndns.com"));

	CHttpFile* pFile = pConnection->OpenRequest(CHttpConnection::HTTP_VERB_GET, _T("/"));

	//	Do we have proxy information to send with this request ?
	if(szAuthLine[0])				// Userid/ password specified?
	{
		pFile->AddRequestHeaders(szAuthLine, HTTP_ADDREQ_FLAG_ADD, strlen(szAuthLine)); 
	}

	//	Now see if we can connect to checkip.dyndns.org
	try
	{
		TRACE("Check Network Setting...\n");
		bResult = pFile->SendRequest(NULL);
		TRACE("SendRequest...\n");
	}
	catch (CInternetException* pEx)
	{
		pEx->Delete();

		pFile->Close();
		delete pFile;

		pConnection->Close();
		delete pConnection;

		delete pSession;

		return 0;			// Apparently no way to go to the Internet
	}

	pFile->QueryInfoStatusCode(dwRet);	

	//	dwRet now contains HTTP return code. 
	//	Return code in 2xx range means successful connection.
	//	Return code 407 means authorization is required.

	//	Request successful (returncode 200 through 299)?

	if(dwRet / 100 == 2) {              // Yes! 

		char *p = new char[1024];
		char *q = p;

		TRACE1("FindWebAddress - Return code is: %i\n", dwRet);
		pFile->Read(p, 1024);			// Now read first 1024 bytes of HTML. Should be enough.
		dwIPNr = 0;
		while(*p) {
			int n = strcspn(p, "1234567890");
			p += n;
			dwIPNr = GetValidIP(&p);	// Go find IP-number
			if(dwIPNr)
			{				// Found IP-number other than 0.0.0.0 ?
				memmove (&inAddr, &dwIPNr, 4);
				sAddr = inet_ntoa (inAddr);

				sprintf_s(szTemp, "Web address: \t%s\r\n", sAddr);
				TRACE2(szTemp, "Web address: \t%s\r\n", sAddr);
				szTotResult += szTemp;	
			}
		}
		delete q;
	}

	pFile->Close();
	delete pFile;

	pConnection->Close();
	delete pConnection;

	delete pSession;

	return dwRet;
}


So the return code is 407 when i`m behind normal proxy server, but i have to request:
1. How to detect Proxies types?

2. How to check if the user input correct UserID and Password when Authentication is needed...

Any Sample or advice will appreciated (I'm new in C/C++)
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900