// Test.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdlib.h>
#include <windows.h>
#include <wininet.h>
BOOL ReadUrlFile(TCHAR* const strServerIPAddress, TCHAR* const szPort)
{
// Open Internet session.
HINTERNET hSession = ::InternetOpen("ReadUrlFile",
//PRE_CONFIG_INTERNET_ACCESS,
INTERNET_OPEN_TYPE_DIRECT,
NULL,
INTERNET_INVALID_PORT_NUMBER,
0) ;
// Connect to www.microsoft.com.
HINTERNET hConnect = ::InternetConnect(hSession,
//"support.microsoft.com",
"www.cnblogs.com",
INTERNET_INVALID_PORT_NUMBER,
"",
"",
INTERNET_SERVICE_HTTP,
0,
0) ;
// Request the file /MSDN/MSDNINFO/ from the server.
TCHAR szObjectName[MAX_PATH];
strcpy (szObjectName, "huqingyu/category/9701.html/rss");
//strcpy (szObjectName, "GetIP.aspx?address=");
//strcat (szObjectName, strServerIPName);
HINTERNET hHttpFile = ::HttpOpenRequest(hConnect,
"GET",
szObjectName,
HTTP_VERSION,
NULL,
0,
INTERNET_FLAG_DONT_CACHE,
0) ;
// Send the request.
BOOL bSendRequest = ::HttpSendRequest(hHttpFile, NULL, 0, 0, 0);
DWORD dwError = 0;
if (!bSendRequest)
{
dwError = GetLastError();
return FALSE;
}
/*
// Get the length of the file.
char bufQuery[32] = {0};
DWORD dwLengthBufQuery = sizeof(bufQuery);
BOOL bQuery = ::HttpQueryInfo(hHttpFile,
HTTP_QUERY_CONTENT_LENGTH,
bufQuery,
&dwLengthBufQuery,NULL) ;
if (!bQuery)
{
return FALSE;
}
*/
// Convert length from ASCII string to a DWORD.
DWORD dwFileSize = 3000;// (DWORD)atol(bufQuery) ;
//DWORD dwFileSize = (DWORD)atol(bufQuery) ;
// Allocate a buffer for the file.
char* buffer = new char[dwFileSize+1] ;
// Read the file into the buffer.
DWORD dwBytesRead = 0;
BOOL bRead = ::InternetReadFile(hHttpFile,buffer,dwFileSize+1,&dwBytesRead);
if (!bRead)
return FALSE;
// Put a zero on the end of the buffer.
buffer[dwBytesRead] = 0 ;
// Close all of the Internet handles.
::InternetCloseHandle(hHttpFile);
::InternetCloseHandle(hConnect) ;
::InternetCloseHandle(hSession) ;
TCHAR* p;
TCHAR szTok[] = ":";
p = strtok(buffer,szTok);
if (p == NULL)
{
return FALSE;
}
strcpy(strServerIPAddress, p);
p = strtok(NULL,szTok);
if (p == NULL)
{
return FALSE;
}
strcpy(szPort,p);
//m_dwPort = (DWORD)atol(szPort);
delete[] buffer;
return TRUE;
}
int main(int argc, char* argv[])
{
TCHAR ip[100] = "192.168.0.1";
TCHAR port[100] = "80";
ReadUrlFile(ip, port);
getchar();
return 0;
}