zoukankan      html  css  js  c++  java
  • Delphi

    Delphi:10.3

    Indy  :10.6.2

    尽情享受拿着好代码直接用的幸福吧!!!先直接上代码吧?下面代码已经经过充分测试,加入足够多的注释,包括各处关键代码如果不存在会发生什么类型的错误的注释!!!

    //需要引入的单元
    uses
    IdBaseComponent, IdComponent, IdServerIOHandler, IdSSL, IdSSLOpenSSL, IdTCPConnection, IdTCPClient, IdHTTP, IdIOHandler, IdIOHandlerSocket, IdIOHandlerStack, IdZLibCompressorBase, IdCompressorZLib;
    {
      IndyHttp Get方式访问网站
      参数:
      url:要访问的网站网址
      responseText:访问的返回值
      proxyServer:代理服务器Ip
      proxyPort:代理服务器端口
      返回值:
      访问是否成功的布尔值
    }
    function IndyGet(url: string; var responseText: string;
      proxyServer: string = ''; proxyPort: integer = 0): Boolean;
    var
      IdHTTPTemp: TIdHTTP; // http客户端对象
      IdSSLIOHandlerSocketOpenSSLTemp: TIdSSLIOHandlerSocketOpenSSL; // ssl对象
      IdCompressorZLibTemp: TIdCompressorZLib; // 数据压缩对象
      isSuccess: Boolean; // 是否访问成功的布尔值
    begin
    
      isSuccess := False; // 设置是否访问成功的布尔值默认值为false
    
      try
        try
    
          { 1, 创建TIdHTTP对象 }
          IdHTTPTemp := TIdHTTP.Create(nil);
    
          IdHTTPTemp.HandleRedirects := true; // 设置重定向属性,防止不能转发Url
          IdHTTPTemp.Request.BasicAuthentication := true; // 必须设置此项为true才能一次通过验证
    
          {
            这里设置很重要,如果不这样设置,会出错:
            HTTP/1.1 403 Bad Behavior
          }
          IdHTTPTemp.Request.Accept :=
            'text/html, image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*';
          IdHTTPTemp.Request.AcceptEncoding := 'gzip, deflate';
          IdHTTPTemp.Request.UserAgent := 'Mozilla/4.0';
    
          // 设置代理服务器
          if trim(proxyServer) <> '' then
          begin
            IdHTTPTemp.ProxyParams.proxyServer := trim(proxyServer); // 代理服务器IP
            IdHTTPTemp.ProxyParams.proxyPort := proxyPort; // 代理服务器端口
          end;
    
          { 2,创建SSL组件 }
          IdSSLIOHandlerSocketOpenSSLTemp :=
            TIdSSLIOHandlerSocketOpenSSL.Create(nil);
          {
            设置SSL组件使用的OpenSSL版本号,如果设置不正确,会出错:
            Error connectiong with ssl.
            error:1409442E:SSL routines:ssl3_read_bytes:tlsv1 alert protocal version
          }
          IdSSLIOHandlerSocketOpenSSLTemp.SSLOptions.Method := sslvTLSv1_2;
          // 设置indy的http控件使用的ssl依赖组件,必须设置,否则无法访问ssl网站
          IdHTTPTemp.IOHandler := IdSSLIOHandlerSocketOpenSSLTemp;
    
          { 3,创建传输数据用的压缩组件 }
          IdCompressorZLibTemp := TIdCompressorZLib.Create(nil);
          // 设置indy的http控件使用的数据压缩组件,如果不设置,那么得到的是压缩的数据,不报任何错误
          IdHTTPTemp.Compressor := IdCompressorZLibTemp;
    
          { 备用属性,暂时未用 }
          // // 设置身份验证帐号
          // IdHTTP1.Request.Username := Trim(account);
          // // 设置身份验证密码
          // IdHTTP1.Request.password := Trim(password);
    
          // 得到web回应
          responseText := IdHTTPTemp.Get(url);
    
          // 如果web相应正常,则设置判断请求是否成功的布尔值为true
          if IdHTTPTemp.ResponseCode = 200 then
          begin
            isSuccess := true; // 设置判断请求是否成功的布尔值为true
          end;
    
          // 关闭IdHTTP1连接
          IdHTTPTemp.Disconnect;
    
        except
          on e: Exception do
          begin
            // 调试使用
            // showMessage(e.ToString); //这里可以忽略错误,防止IdHTTP1无法访问的错误提示
    
            isSuccess := False;
          end;
        end;
      finally
        // 释放创建过的各个对象
        IdCompressorZLibTemp.Free;
        IdSSLIOHandlerSocketOpenSSLTemp.Free;
        IdHTTPTemp.Free;
      end;
    
      Result := isSuccess;
    end;

    不能再详细了,如果是Delphi开发人员,我想已经可以直接使用了!!!

    参考:

    http://ww2.indyproject.org/Sockets/Download/svn.EN.aspx

    https://github.com/IndySockets/OpenSSL-Binaries

    https://en.delphipraxis.net/topic/2814-tidhttp-ssl-and-error-http11-403-forbidden/

  • 相关阅读:
    在Struts2中使用ValueStack、ActionContext、ServletContext、request、session等 .
    Struts2 Action的访问路径
    Struts2 中的数据传输
    Struts2中的类型转换
    应用:ValueStack
    ValueStack基础:OGNL
    Struts2中的ModelDriven机制及其运用
    form表单中method的get和post区别
    el表达式跟ognl表达式的区别(转)
    Java ServletContext 详解
  • 原文地址:https://www.cnblogs.com/sunylat/p/13593960.html
Copyright © 2011-2022 走看看