小弟想用C#实现一个代理google搜索的winform程序.处理英文搜索时候正常,但是中文就不行了。下面是用winsockExpert分析的结果首先分析用IE搜索“你好”:GET /search?hl=en&q=%E4%BD%A0%E5%A5%BD&btnG=Google+Search HTTP/1.0
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
Referer: http://www.google.com/webhp?hl=en
Accept-Language: zh-cn
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)
Host: www.google.com
Connection: Keep-Alive
Cookie: PREF=ID=ea45b4841bbff969:NW=1:TM=1126342814:LM=1126368075:GM=1:S=cYn4uU5fVNAxE93G得到的返回:
HTTP/1.0 200 OK
Cache-Control: private
Content-Type: text/html
Server: GWS/2.1
Date: Sun, 11 Sep 2005 05:44:11 GMT
Connection: Close<html><head><meta HTTP-EQUIV="content-type" CONTENT="text/html; charset=UTF-8"><title>浣犲ソ - Google Search </title><style><!--
body,td,div,.p,a{font-family:arial,sans-serif }
div,td{color:#000}
.f,.fl:link{color:#6f6f6f}于是按照上面的请求,我的程序关键代码如下:
string pattern = HttpUtility.UrlEncode(this.patternName.Text, System.Text.Encoding.UTF8);
string URL = "http://www.google.com/search?hl=en&q="+pattern+"&btnG=Google+Search";
HttpWebRequest searchRequest = (HttpWebRequest)WebRequest.Create(fullURL);然而,分析我的程序搜索“你好”时:
GET /search?hl=en&q=%e4%bd%a0%e5%a5%bd&btnG=Google+Search HTTP/1.1
Connection: Keep-Alive
Host: www.google.com得到的返回却是:
<html><head><meta HTTP-EQUIV="content-type" CONTENT="text/html; charset=ISO-8859-1"><title>浣犲ソ  - Google Search </title><style><!--
body,td,div,.p,a{font-family:arial,sans-serif }
div,td{color:#000}
.f,.fl:link{color:#6f6f6f}大家可以看到明显两个返回结果的charset不同,在我的程序得到的返回中,搜索url中"hl=en"即用英文搜索时,charset=ISO-8859-1;而“hl=zh-CN"时,charset=GB2312我估计这和Google处理编码的机制有关那么,怎样才能够得到和IE一样的结果即charset = UTF8呢?