亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
12下一頁
最近訪問板塊 發(fā)新帖
查看: 10136 | 回復(fù): 10
打印 上一主題 下一主題

用c++寫CGI中文亂碼 [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2008-12-15 11:26 |只看該作者 |倒序瀏覽
在網(wǎng)上找了個cgi的例子
下面兩個文件的編碼都是gb2312
測試get時:
傳進(jìn)去的中文參數(shù)回顯時顯示亂碼英文正常,而在cpp中寫入的“中文”兩個字可以正常顯示。
我的系統(tǒng)是linux
服務(wù)器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. };
復(fù)制代碼

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>
復(fù)制代碼

論壇徽章:
0
2 [報告]
發(fā)表于 2008-12-15 11:34 |只看該作者
看一下你的locale

論壇徽章:
0
3 [報告]
發(fā)表于 2008-12-15 12:45 |只看該作者
是不是顯示 %CA%C7%B2%BB%CA%C7%CF%D4%CA%BE

論壇徽章:
0
4 [報告]
發(fā)表于 2008-12-15 15:10 |只看該作者
原帖由 aoegiss 于 2008-12-15 12:45 發(fā)表
是不是顯示 %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 [報告]
發(fā)表于 2008-12-15 15:15 |只看該作者

回復(fù) #2 cugb_cat 的帖子

zh_CN.UTF-8

論壇徽章:
0
6 [報告]
發(fā)表于 2008-12-15 18:50 |只看該作者
CGI現(xiàn)在還有人用么?

論壇徽章:
0
7 [報告]
發(fā)表于 2008-12-16 09:58 |只看該作者

回復(fù) #6 gobbin 的帖子

看來是不太多了。

論壇徽章:
0
8 [報告]
發(fā)表于 2008-12-16 11:47 |只看該作者
大概還是字符不統(tǒng)一問題


如下幾個部分必須統(tǒng)一,才能保證不亂碼
編輯源代碼環(huán)境
printf( stdout, "Content-type:text/html;charset=gb2312\n\n");
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
post或者get時候的字符集


如果涉及在創(chuàng)建服務(wù)器創(chuàng)建目錄、文件,則web服務(wù)器的default 字符集也要統(tǒng)一

論壇徽章:
0
9 [報告]
發(fā)表于 2008-12-16 12:52 |只看該作者
原帖由 dealover 于 2008-12-15 15:10 發(fā)表
是的,用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);
復(fù)制代碼


  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. }
復(fù)制代碼

論壇徽章:
0
10 [報告]
發(fā)表于 2008-12-16 12:57 |只看該作者
不是UTF-8,就是gb2312,改下頭吧,上面有人說過了.
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP