delphi函数,识别字符集编码

纪念不用 Delphi 开发7周年

函数,获取web page文本,识别字符集编码;

复制代码
1 function CreateHttpRequest(proxyServer: string = ''; proxyPort: integer = 0):
2 TIdHTTP;
3 function DecodeHttpRequestText(InString: string): string;
4 implementation
5
6 { $R *.dfm }
7
8 function DecodeHttpRequestText(InString: string): string;
9 begin
10 Result := UpperCase(InString);
11 //google,baidu,cnblogs,localhost等测试ok...
12 if (Pos( ' CHARSET= ', Result) = 0) or (pos( ' =UTF- ', Result) <> 0) then
13 InString := Utf8Decode(InString);
14 Result := InString;
15 end;
16
17 function CreateHttpRequest(proxyServer: string = ''; proxyPort: integer = 0):
18 TIdHTTP;
19 begin
20 Result := TIdHTTP.Create( nil);
21 with Result do
22 begin
23 HandleRedirects := true;
24 HTTPOptions := [hoForceEncodeParams];
25 ReadTimeout := 30000;
26 Request.Accept :=
27 ' image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, */* ';
28 Request.AcceptLanguage := ' zh-cn ';
29 Request.ContentType := ' application/x-www-form-urlencoded ';
30 Request.UserAgent :=
31 ' Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 4.0) ';
32 if (proxyServer <> '') then
33 proxyParams.ProxyServer := proxyServer; // ' 代理服务器地址 ';
34 if proxyPort <> 0 then
35 proxyParams.ProxyPort := proxyPort; // ' 代理服务器端口 ';
36 end;
37 end;
38
39 procedure TForm1.btn1Click(Sender: TObject);
40 var
41 getstr: string;
42 begin
43 mmo1.Lines.Clear;
44 getstr :=DecodeHttpRequestText( CreateHttpRequest().Get(edt1.Text));
45 mmo1.Lines.Add(getstr);
46
47 end;
复制代码




本文转自suifei博客园博客,原文链接:http://www.cnblogs.com/Chinasf/archive/2012/01/11/2319811.html,如需转载请自行联系原作者

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值