免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 9894 | 回复: 10
打印 上一主题 下一主题

用c++写CGI中文乱码 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-12-15 11:26 |只看该作者 |倒序浏览
在网上找了个cgi的例子
下面两个文件的编码都是gb2312
测试get时:
传进去的中文参数回显时显示乱码英文正常,而在cpp中写入的“中文”两个字可以正常显示。
我的系统是linux
服务器Apache/2.2.9(Ubuntu)
请个各位帮忙看一下,第一次接触cgi编程。谢谢
test-cgi.cpp

  1. #include <algorithm>
  2. #include <fstream>
  3. #include <iomanip>
  4. #include <iostream>
  5. #include <iterator>
  6. #include <list>
  7. #include <map>
  8. #include <set>
  9. #include <sstream>
  10. #include <string>
  11. #include <queue>
  12. #include <vector>

  13. int main( void )
  14. {
  15.         fprintf( stdout, "Content-type:text/html;charset=gb2312\n\n");
  16.         fprintf( stdout, "<html><title>get</title>\n");

  17.         if( getenv("QUERY_STRING" ) )
  18.         {

  19.                 fprintf( stdout, getenv("QUERY_STRING" ) );
  20.                 fprintf(stdout, "中文");


  21.         }
  22.         else
  23.         {
  24.                 fprintf( stdout, "(NULL)\n" );
  25.         }

  26.         fprintf( stdout, "</html>\n" );
  27.         fflush(stdout);

  28.         return 0;
  29. };
复制代码

testcgi.html

  1. <html>
  2. <head>
  3.   <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
  4.   <title>表单测试</title>
  5. </head>
  6. <body>
  7. <form method="get" name="test-get" action="./cgi-bin/test-get.cgi">
  8. <input name="name"><input name="pswd"><input type=submit value="get">
  9. </form>
  10. <br><br>
  11. <form method="post" name="test-post" action="./cgi-bin/test-post.cgi">
  12. <input name="name"><input name="pswd"><input type=submit value="post">
  13. </form>
  14. </body>
  15. </html>
复制代码

论坛徽章:
0
2 [报告]
发表于 2008-12-15 11:34 |只看该作者
看一下你的locale

论坛徽章:
0
3 [报告]
发表于 2008-12-15 12:45 |只看该作者
是不是显示 %CA%C7%B2%BB%CA%C7%CF%D4%CA%BE

论坛徽章:
0
4 [报告]
发表于 2008-12-15 15:10 |只看该作者
原帖由 aoegiss 于 2008-12-15 12:45 发表
是不是显示 %CA%C7%B2%BB%CA%C7%CF%D4%CA%BE

是的,用post请求返回的是这种形式的码。
post‘中文’ 返回 %D6%D0%CE%C4
get  ‘中文’返回 %D6%D0E

[ 本帖最后由 dealover 于 2008-12-15 15:20 编辑 ]

论坛徽章:
0
5 [报告]
发表于 2008-12-15 15:15 |只看该作者

回复 #2 cugb_cat 的帖子

zh_CN.UTF-8

论坛徽章:
0
6 [报告]
发表于 2008-12-15 18:50 |只看该作者
CGI现在还有人用么?

论坛徽章:
0
7 [报告]
发表于 2008-12-16 09:58 |只看该作者

回复 #6 gobbin 的帖子

看来是不太多了。

论坛徽章:
0
8 [报告]
发表于 2008-12-16 11:47 |只看该作者
大概还是字符不统一问题


如下几个部分必须统一,才能保证不乱码
编辑源代码环境
printf( stdout, "Content-type:text/html;charset=gb2312\n\n");
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
post或者get时候的字符集


如果涉及在创建服务器创建目录、文件,则web服务器的default 字符集也要统一

论坛徽章:
0
9 [报告]
发表于 2008-12-16 12:52 |只看该作者
原帖由 dealover 于 2008-12-15 15:10 发表
是的,用post请求返回的是这种形式的码。
post‘中文’ 返回 %D6%D0%CE%C4
get  ‘中文’返回 %D6%D0E


试试这个


  1. char* URL;
  2. URL=malloc(strlen(getenv("QUERY_STRING")+1));
  3. strcpy(URL,getenv("QUERY_STRING"));
  4. fprintf(stdout, URLDecode(URL));
  5. free(URL);
复制代码


  1. char* URLDecode(char* URL)
  2. {
  3.     char* pDecode    =URL;
  4.     char* pURL        =URL;
  5.    
  6.     while('\0'!=(*pDecode=*pURL))
  7.     {
  8.         ++pURL;
  9.         if('%'==*pDecode)
  10.         {
  11.             *pDecode=(*pURL>='A'?(*pURL-'A'+10):(*pURL-'0'));
  12.             ++pURL;
  13.             *pDecode=(*pDecode<<=4)|(*pURL>='A'?(*pURL-'A'+10):(*pURL-'0'));
  14.             ++pURL;
  15.         }
  16.         ++pDecode;
  17.     }
  18.    
  19.     return URL;
  20. }
复制代码

论坛徽章:
0
10 [报告]
发表于 2008-12-16 12:57 |只看该作者
不是UTF-8,就是gb2312,改下头吧,上面有人说过了.
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP