uses Winapi.msxml, Winapi.ActiveX, Winapi.WinSock;
{獲取網頁原始碼}
function Getwebcode(url:string; var code:string):Boolean; //獲取網頁原始碼
var
http: IServerXMLHTTPRequest2;
vHtml: Variant;
begin
result:=false;
CoInitialize(nil);
code:='';
if Pos('http://',LowerCase(url))<=0 then url:='http://'+url;
try
http := CoServerXMLHTTP60.Create;
try
http.open('GET', Trim(url), false, null, null);
//http.setRequestHeader('Content-Type', 'text/xml;charset=gbk'); 無論加不加都無效
http.setTimeouts(10 * 1000, 10 * 1000, 10 * 1000, 10 * 1000);//
http.send(null);
if (http.readyState=4) and (http.status=200) then
begin
vHtml := Http.responsetext;
code := vHtml;
end;
if (code<>'null') and (Length(code)>10) then result:=true else result:=false;
finally
http := nil;
end;
except
on e:exception do
begin
code:='';
result:=false;
end;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
code:string;
begin
//Getwebcode('http://www.baidu.com/',code); //UTF8 正常
//memo1.Lines.Add(code);
Getwebcode('http://www.dedecms.com/',code); //GB2312或GBK 全亂碼
memo1.Lines.Add(code);
end;
VB方法
Option Explicit
Private Sub Form_Load()
'測驗
Text1.Text = GetHtml("http://www.NewXing.com")
End Sub
Public Function GetHtml(url As String)
Dim xmlHttp As Object
Set xmlHttp = CreateObject("Microsoft.XMLHTTP")
xmlHttp.open "GET", url, True
xmlHttp.send (Null)
While xmlHttp.ReadyState <> 4
DoEvents
Wend
GetHtml = BytesToBstr(xmlHttp.responseBody)
End Function
Private Function BytesToBstr(Bytes)
Dim Unicode As String
If IsUTF8(Bytes) Then '如果不是UTF-8編碼則按照GB2312來處理
Unicode = "UTF-8"
Else
Unicode = "GB2312"
End If
Dim objstream As Object
Set objstream = CreateObject("ADODB.Stream")
With objstream
.Type = 1
.Mode = 3
.open
.Write Bytes
.Position = 0
.Type = 2
.Charset = Unicode
BytesToBstr = .ReadText
.Close
End With
End Function
'判斷網頁編碼函式
Private Function IsUTF8(Bytes) As Boolean
Dim i As Long, AscN As Long, Length As Long
Length = UBound(Bytes) + 1
If Length < 3 Then
IsUTF8 = False
Exit Function
ElseIf Bytes(0) = &HEF And Bytes(1) = &HBB And Bytes(2) = &HBF Then
IsUTF8 = True
Exit Function
End If
Do While i <= Length - 1
If Bytes(i) < 128 Then
i = i + 1
AscN = AscN + 1
ElseIf (Bytes(i) And &HE0) = &HC0 And (Bytes(i + 1) And &HC0) = &H80 Then
i = i + 2
ElseIf i + 2 < Length Then
If (Bytes(i) And &HF0) = &HE0 And (Bytes(i + 1) And &HC0) = &H80 And (Bytes(i + 2) And &HC0) = &H80 Then
i = i + 3
Else
IsUTF8 = False
Exit Function
End If
Else
IsUTF8 = False
Exit Function
End If
Loop
If AscN = Length Then
IsUTF8 = False
Else
IsUTF8 = True
End If
End Function
查了幾個通宵資料,國外網站也都翻遍了。
原因:responsetext默認已經轉換成UTF8,導致其它編碼均亂碼。看來只能從 Responsebody入手
老外方法
vHtml := Http.responseBody;
for i:=0 to length(vhtml) do code:=code+chr(strtoint(inttostr(vHtml[i])));
這個方法,我之前試過成功了,GBK和GB2312都沒問題,但UTF-8亂碼。(不知是否和我新裝XE2關系)
網上查到VB的方法(已附)很多,但小弟不懂,望洋興嘆。求翻譯!!!
解決后,保證當天結貼。同叟無欺。
uj5u.com熱心網友回復:
不懂,幫頂
uj5u.com熱心網友回復:
如果沒有解決的途徑,那就自己寫解碼函式!uj5u.com熱心網友回復:
gb18030 編碼uj5u.com熱心網友回復:
If IsUTF8(xmlHTTP1.responseBody) ThenGetCode = xmlHTTP1.responsetext
Else
GetCode = BytesToBstr(xmlHTTP1.responseBody, "gb18030")
End If
uj5u.com熱心網友回復:
Function BytesToBstr(body, Cset)Dim objstream
Set objstream = CreateObject("adodb.stream")
objstream.Type = 1
objstream.Mode = 3
objstream.Open
objstream.Write body
objstream.position = 0
objstream.Type = 2
objstream.Charset = Cset
BytesToBstr = objstream.ReadText
objstream.Close
Set objstream = Nothing
End Function
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/91699.html
標籤:網絡通信/分布式開發
上一篇:經典的斜二測畫法y軸為什么成45度?如果改成其它角度該如何計算?
下一篇:求一個程式代碼
