블로그 이미지
서비
나의 삶을 디자인 한다.

calendar

      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29      

Notice

'HTTP'에 해당되는 글 2

  1. 2007/01/19 URL 인코딩
  2. 2007/01/19 HttpGet
2007/01/19 15:22 Code Library/C/C++

// HEX Values array
char hexVals[16] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};





// UNSAFE String
CString CURLEncode::csUnsafeString= "\"<>%\\^[]`+$,@:;/!#?=&"; // PURPOSE OF THIS FUNCTION IS TO CONVERT A GIVEN CHAR TO URL HEX FORM CString CURLEncode::convert(char val) { CString csRet; csRet += "%"; csRet += decToHex(val, 16); return csRet; } // THIS IS A HELPER FUNCTION. // PURPOSE OF THIS FUNCTION IS TO GENERATE A HEX REPRESENTATION OF GIVEN CHARACTER CString CURLEncode::decToHex(char num, int radix) { int temp=0; CString csTmp; int num_char; num_char = (int) num; // ISO-8859-1 // IF THE IF LOOP IS COMMENTED, THE CODE WILL FAIL TO GENERATE A // PROPER URL ENCODE FOR THE CHARACTERS WHOSE RANGE IN 127-255(DECIMAL) if (num_char < 0) num_char = 256 + num_char; while (num_char >= radix) { temp = num_char % radix; num_char = (int)floor(num_char / radix); csTmp = hexVals[temp]; } csTmp += hexVals[num_char]; if(csTmp.GetLength() < 2) { csTmp += '0'; } CString strdecToHex(csTmp); // Reverse the String strdecToHex.MakeReverse(); return strdecToHex; } // PURPOSE OF THIS FUNCTION IS TO CHECK TO SEE IF A CHAR IS URL UNSAFE. // TRUE = UNSAFE, FALSE = SAFE bool CURLEncode::isUnsafe(char compareChar) { bool bcharfound = false; char tmpsafeChar; int m_strLen = 0; m_strLen = csUnsafeString.GetLength(); for(int ichar_pos = 0; ichar_pos < m_strLen ;ichar_pos++) { tmpsafeChar = csUnsafeString.GetAt(ichar_pos); if(tmpsafeChar == compareChar) { bcharfound = true; break; } } int char_ascii_value = 0; //char_ascii_value = __toascii(compareChar); char_ascii_value = (int) compareChar; if(bcharfound == false && char_ascii_value > 32 && char_ascii_value < 123) { return false; } // found no unsafe chars, return false else { return true; } return true; }
// PURPOSE OF THIS FUNCTION IS TO CONVERT A STRING // TO URL ENCODE FORM. CString CURLEncode::URLEncode(CString pcsEncode) { int ichar_pos; CString csEncode; CString csEncoded; int m_length; int ascii_value; csEncode = pcsEncode; m_length = csEncode.GetLength(); for(ichar_pos = 0; ichar_pos < m_length; ichar_pos++) { char ch = csEncode.GetAt(ichar_pos); if (ch < ' ') { ch = ch; } if(!isUnsafe(ch)) { // Safe Character csEncoded += CString(ch); } else { // get Hex Value of the Character csEncoded += convert(ch); } } return csEncoded; } CString CURLDecoder::decode(CString str) { int len = str.GetLength(); char* buff = new char[len + 1]; strcpy(buff,str); CString ret = ""; for(int i=0;i<len;i++) { if(buff[i] == '+') { ret = ret + " "; }else if(buff[i] == '%') { char tmp[4]; char hex[4]; hex[0] = buff[++i]; hex[1] = buff[++i]; hex[2] = '\0'; //int hex_i = atoi(hex); sprintf(tmp,"%c",convertToDec(hex)); ret = ret + tmp; }else { ret = ret + buff[i]; } } delete[] buff; return ret; } int CURLDecoder::convertToDec(const char* hex) { char buff[12]; sprintf(buff,"%s",hex); int ret = 0; int len = strlen(buff); for(int i=0;i<len;i++) { char tmp[4]; tmp[0] = buff[i]; tmp[1] = '\0'; getAsDec(tmp); int tmp_i = atoi(tmp); int rs = 1; for(int j=i;j<(len-1);j++) { rs *= 16; } ret += (rs * tmp_i); } return ret; } void CURLDecoder::getAsDec(char* hex) { char tmp = tolower(hex[0]); if(tmp == 'a') { strcpy(hex,"10"); }else if(tmp == 'b') { strcpy(hex,"11"); }else if(tmp == 'c') { strcpy(hex,"12"); }else if(tmp == 'd') { strcpy(hex,"13"); }else if(tmp == 'e') { strcpy(hex,"14"); }else if(tmp == 'f') { strcpy(hex,"15"); }else if(tmp == 'g') { strcpy(hex,"16"); } }
posted by 서비
TAG HTTP, URL
2007/01/19 10:59 Code Library/VC++
const INT BUF_SIZE = 2048576;
TCHAR g_pBuffer[BUF_SIZE] = {0};

// 지정된 URL(strURL) 에서 소스코드를 얻는다(strSourceCode)

BOOL HttpGet(const CString& strURL, CString& strSourceCode)
{
    HINTERNET hInternet;
    hInternet = InternetOpen(_T("GBHConnect"), INTERNET_OPEN_TYPE_DIRECT, 0, 0, 0);

    if( hInternet == NULL )
    {
        ASSERT(0);
        return FALSE;
    }

    CString strHeader = _T("Accept: */*\'\r\n\'Accept-Charset: ks_c_5601-1987\'\r\n\'");
    strHeader += _T("Accept-Language: ko\'\r\n\'Content-Encoding: ks_c_5601-1987");

    HINTERNET hInternetRequest;
    hInternetRequest = InternetOpenUrl( hInternet, strURL, strHeader, 0, INTERNET_FLAG_RELOAD, 0 );
    if( NULL == hInternetRequest )
    {
        ASSERT(0);
        return FALSE;
    }

    strSourceCode = _T("");
    BOOL bOK;

    const INT READ_SIZE = 10240;

    DWORD dwTotalReadByte = 0;
    while( TRUE )
    {
        DWORD dwNumOfBytesRead;
        bOK = InternetReadFile( hInternetRequest, &g_pBuffer[dwTotalReadByte], READ_SIZE, &dwNumOfBytesRead);
        if( FALSE == bOK ||  0 == dwNumOfBytesRead )
        {
            break;
        }
        dwTotalReadByte += dwNumOfBytesRead;


        if( bOK == FALSE )
        {
            ASSERT(0);
            InternetCloseHandle( hInternetRequest );
            InternetCloseHandle( hInternet );
            return FALSE;
        }
    }
    InternetCloseHandle( hInternetRequest );
    InternetCloseHandle( hInternet );

    g_pBuffer[dwTotalReadByte+1] = '\0';
    strSourceCode = g_pBuffer;

    return TRUE;
}
posted by 서비
TAG HTTP
prev 1 next