encoding - How to get web content that is in a language other than English by use C# HttpClient -
i learning web programming, writing small program requests web site return page. after getting the web page, want extract information it.
i use ie access web page, , found language in chinese simplified (gb2312). corresponding microsoft encoding 936.
my program got web site response. extract information web page, convert content xdocument. convertion (xdocument xmlinput = xdocument.parse(xml);) incurred error message: "'', hexadecimal value 0x1f, invalid character. line 1, position 1." debug, save content text file, found content of text file not in chinese.
the following code. thank helping.
private void button1_click(object sender, eventargs e) { string s = await(new updatecategory()).tostring(); } class updatecategory { public async task<string> submitjrjasync() { httpclient client = null; try { client = new httpclient(); client.baseaddress = new uri("http://stock.jrj.com.cn/concept/conceptpage.shtml"); client.defaultrequestheaders.add("contenttype", "application/xml"); client.defaultrequestheaders.add("accept-encoding", "gzip, deflate, sdch"); client.defaultrequestheaders.add("accept-language", "en-us,en; q=0.8"); client.defaultrequestheaders.accept.clear(); client.defaultrequestheaders.accept.add(new mediatypewithqualityheadervalue("application/xml")); client.defaultrequestheaders.connection.clear(); client.defaultrequestheaders.connection.add("keep-alive"); client.defaultrequestheaders.cachecontrol = new cachecontrolheadervalue() { nocache = true }; client.defaultrequestheaders.pragma.clear(); client.defaultrequestheaders.pragma.add(new namevalueheadervalue("no-cache")); client.defaultrequestheaders.useragent.clear(); client.defaultrequestheaders.useragent.add(new productinfoheadervalue("java", "1.6.0_20")); client.defaultrequestheaders.host = "stock.jrj.com.cn"; client.defaultrequestheaders.accept.add(new mediatypewithqualityheadervalue("text/html")); httpcontent _content = new stringcontent(""); _content.headers.contenttype = new mediatypeheadervalue("application/xml"); httpresponsemessage response = await client.getasync(client.baseaddress); if (response.issuccessstatuscode) { stream result = response.content.readasstreamasync().result; streamreader stream = new streamreader(result, encoding.getencoding(936)); string x = stream.readtoend(); system.io.file.writealltext(@"d:\test.txt", x, encoding.getencoding(936)); string xml = httputility.urldecode(x); xdocument xmlinput = xdocument.parse(xml); } } catch (exception ex) { messagebox.show(ex.message + "\r\n" + ex.innerexception); } { } return ""; } }
Comments
Post a Comment