Encoding problem.
In the afternoon, I tried Google search's rest service. The interface is "http://ajax.googleapis.com/ajax/services/search/web? V = 1.0 & Q = <search item>. The process is also simple: replace <search item> with urlencode and then send a GET request, the JSON data of the search content should be obtained. for example, if I search for "create Chen", the following data is returned in the browser:
{"Responsedata": {"Results": [{"gsearchresultclass": "gwebsearch", "unescapedurl": "http://www.cnblogs.com/technology/", "url": "http://www.cnblogs.com/technology/", "visibleurl ": "www.cnblogs.com", "cacheurl": "http://www.google.com/search? Q \ u003dcache: wymioencuymj: www.cnblogs.com "," title ":" \ u003cb \ u003ecreate Chen \ u003c/B \ u003e-blog Garden "," titlenoformatting ": "Create Chen-blog", "content": "posted @ 2011-05-26 \ u003cb \ u003ecreate Chen \ u003c/B \ u003e read (1206) | comment (30) | edit \ u003cb \ u003e... \ u003c/B \ u003e posted @ 2011-05-22 20:49 \ u003cb \ u003ecreate Chen \ u003c/B \ u003e read (1781) | comment (26) | Edit
... Omit the following content...
The purpose of the last section is to show that in the browser, if the data contains Chinese, or display normal, the reason is very simple: My browser sets the default encoding is Unicode (UTF-8 ), of course, Chinese characters can be correctly displayed.
to better display data, follow the serialized the JSON data obtained in the Program . I did not expect that errors always occur during serialization, it runs normally only when the search results do not contain Chinese characters. Code :
//... // Some structures of googlesearchresponse //... protected void btnsearch_click (Object sender, eventargs e) {WebClient WC = new WebClient (); response. write (WC. encoding. tostring (); WC. headers. add (httprequestheader. referer, request. URL. tostring (); string url = string. format ("http://ajax.googleapis.com/ajax/services/search/web? V = 1.0 & Q = {0} ", server. urlencode (txtsearchfor. text); var JSON = WC. downloadstring (URL); googlesearchresponse sechresponse = NULL; using (memorystream MS = new memorystream (encoding. utf8.getbytes (JSON) {datacontractjsonserializer jsonserializer = new datacontractjsonserializer (typeof (googlesearchresponse); sechresponse = jsonserializer. readobject (MS) as googlesearchresponse;} stringbuilder sb = new stringbuilder (); foreach (googleresult res in sechresponse. responsedata. results) {sb. appendformat ("<a href = '{0}'> {1} </a> <br/>", Res. unescapedurl, Res. title);} lblresults. TEXT = sb. tostring ();}
After debugging, I found that the JSON string is garbled in the highlighted line of the Code.WebClientDownloadstring method:
This method retrieves the specified resource. After it downloads the resource, the method uses the encoding specified in the encoding property to convert the resource toString. This method blocks while downloading the resource. to download a resource and continue executing while waiting for the server's response, use one of the downloadstringasync methods.
The previous two sentences are about downloading resources (which should beByte []), This method will call the default encoding method to convert the resourceString. Okay, let's see if the default encoding method is UTF-8. often the default encoding method is determined by the operating system and region, after testing on my machine that the default encoding method is not UTF-8, isSystem. Text. dbcscodepageencodingAlthough the encoding method is not a UTF-8, but in addition to the collection characters, this encoding method can indeed identify the Unicode type of encoding, the premise is that the file must start with BOM information, which information can be obtained from Bom is as follows:
Unicode 0xff, 0xfe
Be-Unicode 0xfe, 0xff
Utf8 0xef, 0xbb, 0xbf
However, the JSON data returned by the server does not have such BOM information:
Therefore, I need to set it in the programWebClientSet itEncoding. utf8And runs normally.
But let's look at it. It seems that the program will be downloaded in the downloadstring method.Byte []Convert to oneString, And thenStringConvertByte []Stored in the memory, you can directly call the downloaddata method, store the data downloaded by the downloaddata Method to the memory, and then perform further serialization, this saves two type conversion operations.
I also saw a joke about encoding a few days ago. I don't know whether it is true or false:
take the Chinese version of Windows as an example. Porting from the English version to the Chinese version is not just as simple as translating menus. Many Source Code must be rewritten. For example, when a line of words in a word is entered, the line breaks automatically. However, English is a single byte, while Chinese is a double byte. If a "good" word is entered, it is very likely that "female" is at the end of the last line, "child" is at the beginning of the next line. -- Tang Jun p89