如何在 Delphi 中获取网页的源代码?

发布于 2024-12-11 00:29:57 字数 93 浏览 0 评论 0原文

我正在使用 IdHTTP Indy 组件获取网页源代码,但是在阅读了该组件的问题以及该网站上的一些问题后,它似乎并不总是最佳选择。

您对获取网页有什么建议?

I'm fetching Web page source with IdHTTP Indy component, but after reading about a problem with it and some questions on this Web site, it seems that it's not always the best choice.

What are your suggestions for fetching Web pages?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

苏佲洛 2024-12-18 00:29:57

使用印地。创建一个 TIdHTTP 对象并调用其 Get 方法。它将网页源作为字符串返回,或将其放入您提供的流中。

Use Indy. Create a TIdHTTP object and call its Get method. It returns the Web page source as a string, or places it in a stream that you provide.

风筝在阴天搁浅。 2024-12-18 00:29:57

对于 COM/ActiveX 对象,WinHttp.WinHttpRequest.5.1:

function getPostTranslateLibre3(feedstream, fromlang, tolang: string): string;
var
  Url,aAPI_KEY, source: string;
  jo, locate: TJSONObject;
  httpReq,hr: Olevariant;
  strm: TStringStream;
begin
  httpReq:= CreateOleObject('WinHttp.WinHttpRequest.5.1');
  // Open the HTTPs connection.  
  try              
    hr:= httpReq.Open('POST','https://libretranslate.pussthecat.org/translate', false);
    httpReq.setRequestheader('user-agent',CUSERAGENT  );
    httpReq.setRequestheader('content-type','application/x-www-form-urlencoded');  
             
    if hr= S_OK then HttpReq.Send('q='+HTTPEncode(feedstream)+
                                  '&source='+fromlang+'&target='+tolang);
    If HttpReq.Status = 200 Then
       result:= HttpReq.responseText
    Else result:= 'Failed at getting response:'+itoa(HttpReq.Status)+HttpReq.responseText;
    //writeln('debug response '+HttpReq.GetAllResponseHeaders);     
  finally
    httpreq:= unassigned;  
  end;                  
end; 

With COM/ActiveX objects, WinHttp.WinHttpRequest.5.1:

function getPostTranslateLibre3(feedstream, fromlang, tolang: string): string;
var
  Url,aAPI_KEY, source: string;
  jo, locate: TJSONObject;
  httpReq,hr: Olevariant;
  strm: TStringStream;
begin
  httpReq:= CreateOleObject('WinHttp.WinHttpRequest.5.1');
  // Open the HTTPs connection.  
  try              
    hr:= httpReq.Open('POST','https://libretranslate.pussthecat.org/translate', false);
    httpReq.setRequestheader('user-agent',CUSERAGENT  );
    httpReq.setRequestheader('content-type','application/x-www-form-urlencoded');  
             
    if hr= S_OK then HttpReq.Send('q='+HTTPEncode(feedstream)+
                                  '&source='+fromlang+'&target='+tolang);
    If HttpReq.Status = 200 Then
       result:= HttpReq.responseText
    Else result:= 'Failed at getting response:'+itoa(HttpReq.Status)+HttpReq.responseText;
    //writeln('debug response '+HttpReq.GetAllResponseHeaders);     
  finally
    httpreq:= unassigned;  
  end;                  
end; 
淡淡绿茶香 2024-12-18 00:29:57

另一种方法是调用 WinInet 函数,这是可行的,但需要大量工作。
如果您已经使用 COM/ActiveX 对象,则可以考虑使用 MSXML2 的 XmlHttpRequest 或 WinInet 组件:请参阅此处的示例 http://yoy.be/item.asp?i142

或者,如果您想在屏幕上显示网页,您可以使用 TWebBrowser 组件:http://yoy.be/item.asp?i598

An alternative is calling the WinInet functions, which is doable but a lot of work.
If you already use COM/ActiveX objects, you could consider using MSXML2's XmlHttpRequest or the WinInet component: see here for an example http://yoy.be/item.asp?i142

Or if you want to show the web-page on screen, you could use the TWebBrowser component: http://yoy.be/item.asp?i598

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文