We help IT Professionals succeed at work.
Get Started

Delphi HTTPSend - HTTPMethod GET never completes

nickdelphi777
on
2,095 Views
Last Modified: 2015-04-30
Hey,

I have this code.. occasionally it just hangs forever.. on the HTTP.HTTPMethod get call.. been 10 minutes and it still hasn't timed out, or returned anything or even failed.  What would cause the method to do this?
This code:

      if HTTP.HTTPMethod('GET',url) then //<-----here
        begin
          addlog('GET ACTY');

Open in new window


function TBaseThread.doGet(url: string; useragent: string; UseProxy:Integer) : string;
var
  HTTP:THTTPSend;
  NewUrl:String;
  n: integer;
  SL,badproxy:TStringList;
  retries:integer;

begin
repeat
application.processmessages;
sleep(100);
until paused[myindex]=false;
  result:= ''; // or use 'error codes' in the result, similar to the debug messages
  retries := 1;
  addlog('get attempts max: '+inttostr(retryattempts));
  SL:=TStringList.Create;
  HTTP := THTTPSend.Create;

  repeat
  addlog('Do GET');
  sleep(100);
    http.Clear;
    http.sock.ResetLastError;
    CookieStream.Position := 0;
    HTTP.Cookies.LoadFromStream(CookieStream);
    HTTP.UserAgent := useragent;
    HTTP.KeepAlive := true;
//    HTTP.KeepAliveTimeout := connecttimeout;
    Http.Timeout := connecttimeout;
    http.Sock.SetRecvTimeout(readtimeout);
    http.Sock.SetSendTimeout(readtimeout);
    http.Sock.HTTPTunnelTimeout := connecttimeout;
    http.Sock.SocksTimeout := connecttimeout;
    http.Sock.SetTimeout(readtimeout);

    HTTP.TargetPort := '80';
    HTTP.TargetHost := url;
    HTTP.Protocol :='1.1';
    HTTP.MimeType :='application/x-www-form-urlencoded';

    if UseProxy = 1 then
      begin
        if(ptype = 'socks4')then
          begin
            if (pUser <> '') and (pPass <> '') then
              begin
            http.Sock.SocksType := ST_socks5;
            http.Sock.SocksUsername := pUser;
            http.Sock.SocksPassword := pPass;
            end;//if username and pass

            http.Sock.SocksIP := pHost;
            http.Sock.SocksPort := pPort;


          end
        else
          begin
            if (pUser <> '') and (pPass <> '') then
              begin
                HTTP.ProxyHost := pHost;
                HTTP.ProxyPort := pPort;
                HTTP.ProxyUser := pUser;
                HTTP.ProxyPass := pPass;
              end
            else
              begin
                HTTP.ProxyHost := pHost;
                HTTP.ProxyPort := pPort;
                HTTP.ProxyUser := '';
                HTTP.ProxyPass := '';
              end;
          end;
      end;

    repeat
    sleep(100);
    addlog(copy(url,1,40));
    addlog(copy(url,41,length(url)));
    http.clear;
      if HTTP.HTTPMethod('GET',url) then
        begin
          addlog('GET ACTY');
          n := FoundLocationStrNum(HTTP.Headers);
          if (n >= 0) and (n <= HTTP.Headers.count) then
            begin
              try
               addlog('BOOm GET');
                NewUrl := StringReplace(HTTP.Headers.Strings[n],'Location: ','',[]);
              except
                addlog('Error with headers stringreplace 3');
              end;
              newurl := StringReplace(newurl, ':443', '',[rfReplaceAll, rfIgnoreCase]);
              url:= newurl;
            end;//if > 0
        end else begin
      sl.Add(URL);
      sl.SaveToFile('debug/FAILEDGET.html');
        addlog('failed get');
        end;
        addlog('http result: ' +inttostr(http.resultcode));
    until not(HTTP.Resultcode = 301) and not(HTTP.Resultcode = 302) and not(HTTP.Resultcode = 307);  //and (HTTP.Resultcode = 200)
      addlog('middle get');
  //error checking and retrying
  if((http.Sock.LastError <> 0) or (http.Document.Size <= 0)) then begin

          //addlog(pHost+ ' ' + pPort + ' ' +puser+' '+ppass);
          addlog('GET WinSock Error: '+inttostr(http.Sock.LastError) + ' ' + http.Sock.GetErrorDescEx);
          http.Clear;
          inc(retries);
          addlog('Retrying connection...GET');
          sleep(1000);

    end else begin
      //Clear SL String List, and load http into it
      sl.Clear;
      http.Document.Position := 0;
      SL.LoadFromStream(HTTP.Document);
  if(pos('Redirecting...',sl.Text) > 0) then begin
    addlog('redirecting get');
      newurl := findvalue2('url=',sl.Text,'"');
      http.Clear;
      HTTP.HTTPMethod('GET',newurl);
      sl.Clear;
      http.Document.Position := 0;
      SL.LoadFromStream(HTTP.Document);
    end;//redirecting

    end;
  until ((http.Sock.LastError = 0) or (http.Document.Size > 0)) or (retries > retryattempts);


  //save new cookie to stream
  CookieStream.Position := 0;
  if CookieStream <> nil then
    HTTP.Cookies.SaveToStream(CookieStream);

  //write debug
  if(debugmode) then
    begin
      if(not(directoryexists('debug')))then
        begin
          createdir('debug');
        end;
     //   addlog('write debug');
   //   sl.Add(URL);
      sl.SaveToFile('debug/GET_'+email+'_Action_'+inttostr(debugpos)+'_.html');
      inc(debugpos);
    end;

  //return result text
  addlog('return get html');
  Result:= SL.Text;

  SL.Free;
  HTTP.Free;
end;

Open in new window

Comment
Watch Question
CERTIFIED EXPERT
Top Expert 2011
Commented:
This problem has been solved!
Unlock 1 Answer and 9 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE