procedure TForm1.Button1Click(Sender: TObject);const
  BaseUrl ='URL;
var strMsg:string;
    sParams:TStringStream;
    checkbox5:string;
    Params:TStrings;
    str1:string;
    str2:string;
    str3:string;  HTML:String;
beginstr1:='checkbox5='+'%BD%AD%CE%F7%CA%A1%C9%F3%BC%C6%CC%FC%BB%FA%B9%D8';
str2:='&name='+httpencode(memo2.Lines.Strings[i]);
str3:='&nums='+memo3.Lines.Strings[i];
i:=i+1;//label1.Caption:=inttostr (i);IdHTTP1.HandleRedirects :=true;
sParams:=TStringStream.Create('');
IdHTTP1.Request.Accept:='application/x-shockwave-flash, image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*';
IdHTTP1.Request.Referer:='URL';
IdHTTP1.Request.AcceptLanguage:='zh-cn';
IdHTTP1.Request.UserAgent:='Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)';
IdHTTP1.Request.ContentType:='application/x-www-form-urlencoded';
IdHTTp1.Request.AcceptEncoding:='gzip, deflate';
IdHTTP1.Request.Host:='ip';
IdHTTP1.Request.Connection:='Keep-Alive'  ;
IdHTTP1.Request.CacheControl:='no-cache';
IdHTTP1.HTTPOptions:=IdHTTP1.HTTPOptions+[hoKeepOrigProtocol];  //使用HTTP1.1协议最关键的一步
IdHTTP1.ProtocolVersion:=pv1_1;
//IdHTTP1.Request.ContentLength :=length(Edit1.Text );
//sParams.Add(edit1.Text+edit2.Text+edit3.text+'&Submit=++++++%CE%D2%D2%AA%CD%B6%C6%B1++++++');
sParams.WriteString(str1+str2+str3+'&Submit=++++++%CE%D2%D2%AA%CD%B6%C6%B1++++++');
IdHTTP1.HTTPOptions:=[hoForceEncodeParams];
try
  HTML :=IdHTTP1.Post(BaseURL+'URL',sParams);
  except
  showmessage('error');
  exit;
  end;
  Memo1.text:=IdHTTP1.ResponseText;
  IdHTTP1.Disconnect;
  FreeAndNil(IdHTTP1);
  FreeAndNil(Params);
  end;i在interface中定义了,为什么我点第二次的时候会出错呢?

解决方案 »

  1.   

    你首先要确定出错在什么地方,你可以试着每次先把IdHTTP1给disconnect
      

  2.   


    IdHTTP1:=TIdHttp.Create();
    idHttp1.xxx:=;
      try
        HTML :=IdHTTP1.Post(BaseURL+'URL',sParams);
      except
        showmessage('error');
      exit;
      end;
      Memo1.text:=IdHTTP1.ResponseText;
      IdHTTP1.Disconnect;
      FreeAndNil(IdHTTP1);
      FreeAndNil(Params);
      end
      

  3.   

    要么你在过程中每次动态创建HTTP,要么你在过程中就别释放HTTP,在窗体关闭时再释放
      

  4.   

    procedure TForm1.Button1Click(Sender: TObject);const
      BaseUrl ='URL;
    var strMsg:string;
      sParams:TStringStream;
      checkbox5:string;
      Params:TStrings;
      str1:string;
      str2:string;
      str3:string;  HTML:String;
    beginstr1:='checkbox5='+'%BD%AD%CE%F7%CA%A1%C9%F3%BC%C6%CC%FC%BB%FA%B9%D8';
    str2:='&name='+httpencode(memo2.Lines.Strings[i]);
    str3:='&nums='+memo3.Lines.Strings[i];
    i:=i+1;//label1.Caption:=inttostr (i);IdHTTP1.HandleRedirects :=true;
    sParams:=TStringStream.Create('');
    IdHTTP1.Request.Accept:='application/x-shockwave-flash, image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*';
    IdHTTP1.Request.Referer:='URL';
    IdHTTP1.Request.AcceptLanguage:='zh-cn';
    IdHTTP1.Request.UserAgent:='Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0)';
    IdHTTP1.Request.ContentType:='application/x-www-form-urlencoded';
    IdHTTp1.Request.AcceptEncoding:='gzip, deflate';
    IdHTTP1.Request.Host:='ip';
    IdHTTP1.Request.Connection:='Keep-Alive' ;
    IdHTTP1.Request.CacheControl:='no-cache';
    IdHTTP1.HTTPOptions:=IdHTTP1.HTTPOptions+[hoKeepOrigProtocol]; //使用HTTP1.1协议最关键的一步
    IdHTTP1.ProtocolVersion:=pv1_1;
    //IdHTTP1.Request.ContentLength :=length(Edit1.Text );
    //sParams.Add(edit1.Text+edit2.Text+edit3.text+'&Submit=++++++%CE%D2%D2%AA%CD%B6%C6%B1++++++');
    sParams.WriteString(str1+str2+str3+'&Submit=++++++%CE%D2%D2%AA%CD%B6%C6%B1++++++');
    IdHTTP1.HTTPOptions:=[hoForceEncodeParams];
    try
      HTML :=IdHTTP1.Post(BaseURL+'URL',sParams);
      except
      showmessage('error');
      exit;
      end;
      Memo1.text:=IdHTTP1.ResponseText;
      IdHTTP1.Disconnect;
    //  FreeAndNil(IdHTTP1);//不应该被释放
      FreeAndNil(sParams); //关键是这里,你释放错了
      end;