Background: to open a web query page on the client (written in C ++), you must splice a query URL and open it in the browser.
For example, search for "horse" in Google"
Http://www.google.cn/search? Hl = ZH-CN & newwindow = 1 & Q = % E9 % A9 % AC & AQ = F & OQ =
Note that this "horse" is encoded with UTF-8 and then URL encoding, Which is used directly in Java, JS, PHP, and C # methods such as urlencode and escape. I have done it all, the C ++ client is left, but there is no ready-made API for direct use.
The following code uses C ++ to implement this function:
The first is to convert UTF-8 encoding, which requires the use of libiconv (http://gnuwin32.sourceforge.net/packages/libiconv.htm), the header file and library contains, you can use the convertcode Function
Followed by URL encoding.
The following code is shared:
/*************************************** **************************************
Module: urlencode. h
Notices: Written 2002 by chandrasekar vuppalapati
Description: h url Encoder
**************************************** *************************************/
# Ifndef _ curlencode_h _
# DEFINE _ curlencode_h _
# Include "stdafx. H"
Class curlencode
{
PRIVATE:
Static cstring csunsafestring;
Cstring dectohex (char num, int Radix );
Bool isunsafe (char comparechar );
Cstring convert (char Val );
Public:
Curlencode (){};
Virtual ~ Curlencode (){};
Cstring urlencode (cstring vdata );
Int convertcode (const char * inbuf, int inlen, char * outbuf, int outlen );
};
# Endif/_ curlencode_h _
/*************************************** **************************************
Module: urlencode. cpp
Notices: Written 2002 by chandrasekar vuppalapati
Description: cpp url Encoder
**************************************** *************************************/
# DEFINE _ crtdbg_map_alloc
# Include "stdafx. H"
# Include <math. h>
# Include <malloc. h>
# Include <memory. h>
# Include <New. h>
# Include <stdlib. h>
# Include <string. h>
# Include <wininet. h>
# Include "urlencode. H"
# Include "iconv. H"
# Define max_buffer_size 4096
# Ifdef _ debug
# UNDEF this_file
Static char this_file [] =__ file __;
# Define new debug_new
# Endif
# Include <stdlib. h>
# Include <crtdbg. h>
// 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
// 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 (double) (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;
}
// Convert gb2312 to UTF-8
Int curlencode: convertcode (const char * inbuf, int inlen, char * outbuf, int outlen)
{
Iconv_t handle;
Const char ** pin = & inbuf;
Char ** pout = & outbuf;
Handle = iconv_open ("UTF-8", "gb2312 ");
If (handle = 0)
Printf ("error! /N ");
Memset (outbuf, 0, outlen );
If (iconv (handle, pin, (size_t *) & inlen, pout, (size_t *) & outlen) =-1 ){
Printf ("iconv error! /N ");
Return-1;
}
Iconv_close (handle );
Return 0;
}
Main (){
Curlencode url_encode;
Int len1, len2 = 20;
Char * apin = "Seven Stars ";
Len1 = strlen (apin );
Char apout [20];
Url_encode.convertcode (apin, len1, apout, len2 );
Afxmessagebox (apout );
}