如何用Delphi做一个服务程序读取网页中的一段文本
我想做一个Delphi的服务程序,它的功能是,读取一个特定的ip地址上的一个页面中的内容(如 http://192.168.0.3/ip/GetIp.aspx),然后返回这个页面的所有显示的字符串,如何操作?
本人没有做过网络Delphi,希望能具体说一下要放入什么组件来实现这个功能.还有就是如何创建成一个服务程序而不是应用程序.多谢!!!最好给我贴个源代码!
[解决办法]
服务是不能交互的……
[解决办法]
用Webbrowser
uses MSHTML, ActiveX,comobj;
function GetHtml(const WebBrowser:TWebBrowser): string;
const
BufSize = $10000;
var
Size: Int64;
Stream: IStream;
hHTMLText: HGLOBAL;
psi: IPersistStreamInit;
begin
if not Assigned(WebBrowser.Document) then Exit;
OleCheck(WebBrowser.Document.QueryInterface(IPersistStreamInit, psi));
try
hHTMLText := GlobalAlloc(GPTR, BufSize);
if 0 = hHTMLText then RaiseLastWin32Error;
OleCheck(CreateStreamOnHGlobal(hHTMLText, True, Stream));
try
OleCheck(psi.Save(Stream, False));
Size := StrLen(PChar(hHTMLText));
SetLength(Result, Size);
CopyMemory(PChar(Result), Pointer(hHTMLText), Size);
finally
Stream := nil;
end;
finally
psi := nil;
end;
end;
[解决办法]
如果只要html源码,下面这样就行
- Delphi(Pascal) code
procedure TForm1.Button1Click(Sender: TObject);var htmlStr: string;begin htmlStr := idhttp1.Get('http://192.168.0.3/ip/GetIp.aspx');end;
[解决办法]
如果要得到显示在页面上的文本,用下面的方法
- Delphi(Pascal) code
uses MSHTML, ActiveX;procedure TForm1.Button1Click(Sender: TObject);var HtmlStr: string; doc2: IHTMLDocument2; V: OleVariant;begin try try HtmlStr := IdHTTP1.Get('http://192.168.0.3/ip/GetIp.aspx'); except end; doc2 := CoHTMLDocument.Create as IHtmlDocument2; try V := VarArrayCreate([0, 0], varVariant); V[0] := HtmlStr; doc2.Write(PSafeArray(TVarData(v).VArray)); doc2.Close; HtmlStr := Trim(doc2.body.innerText); Caption := HtmlStr; finally end; doc2 := nil; except end;end;