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) =  0or (pos( ' =UTF- ', Result) <>  0then
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;

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM