有的时候直接就能看到正确的内容,有的时候需要加UTF8TOANSI解码,但其它类型的我都不会解啊。例如GB2312,unicode等。
希望大侠们帮帮忙啊。有没有直接的解码函数或负责解码的组件或类啊。。我的代码如下:
  gethttp:=tidhttp.Create(nil);
  response:=tstringstream.Create('');
  gethttp.HTTPOptions:=idhttp1.HTTPOptions+[hoKeepOrigProtocol];//关键这行
  gethttp.ProtocolVersion:=pv1_1;
  gethttp.Request.AcceptLanguage := 'zh-cn';
  gethttp.Request.UserAgent :='Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; Maxthon; InfoPath.2)';
  gethttp.Request.Connection:='Keep-Alive';
  gethttp.Request.Referer:=refer;
  gethttp.Host:=host;
  gethttp.Request.Accept :=accept;
  gethttp.Request.AcceptEncoding:='gzip, deflate';
  gethttp.Request.ContentType:='application/x-www-form-urlencoded';
  gethttp.Request.SetHeaders;
  gethttp.request.customheaders.add('x-flash-version:'+' 11,0,1,152');
  gethttp.Request.CustomHeaders.Add('cookie: '+cookie);
  try
    gethttp.Get(url,response);
    result:=cookiewrite(gethttp.Response.RawHeaders);//自定义的通用函数
    memo1.Lines.Add(response.datastring);//如数据为UTF8则memo1.Lines.Add(utf8toansi(response.datastring));
  finally
    gethttp.Free;
    response.Free;
  end;

解决方案 »

  1.   

    如果是unicode,就赋给宽字符串。
      

  2.   

    //------ IDHTTP GET网页通用函数
    function TForm1.IdHTTPGet(aURL: string; var sWeb: string): Boolean;
    var
      IdHTTP: TIdHTTP;
      SS_Response: TStringStream; // 提交后返回的数据
    begin
      Result := False;
      if aURL = '' then Exit;
      sWeb := '';
      IdHTTP := TIdHTTP.Create(nil);
      try
        try
          with IdHTTP do
          begin
            // IdHTTP 设置
            HTTPOptions := HTTPOptions + [hoKeepOrigProtocol];   //保持 并使用PV1_1
            ProtocolVersion := pv1_1;
            HTTPOptions := HTTPOptions - [hoForceEncodeParams];  //去掉自动编码
            AllowCookies := True;
            HandleRedirects := True;
            ConnectTimeout := 30000;
            ReadTimeout := 30000;
            // IdHTTP 提交信息的设置
            Request.Accept := 'application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5';
            Request.AcceptCharSet := 'GBK,utf-8;q=0.7,*;q=0.3';
            Request.AcceptEncoding := '';     //'gzip,deflate,sdch';
            Request.AcceptLanguage := 'zh-CN,zh;q=0.8';
            Request.Connection :='Keep-Alive';
            Request.ContentType := 'application/x-www-form-urlencoded';
            Request.UserAgent := 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.133 Safari/534.16';
            Request.Referer := aURL;        SS_Response := TStringStream.Create('', TEncoding.GetEncoding(936));  // TEncoding.UTF8
            try
              Get(aURL, SS_Response);
              if ResponseCode <> 200 then Exit;
              if SS_Response.Size <= 1 then Exit;
              sWeb := SS_Response.DataString;
              Result := True;
            finally
              FreeAndNil(SS_Response);
            end;
          end;
        except
          on E: Exception do
            Result := False;
        end;
      finally
        IdHTTP.Disconnect;
        FreeAndNil(IdHTTP);
      end;
    end;本人用的 跟LZ类似 稍有不同
      

  3.   


    //------ IDHTTP GET网页通用函数
    function TForm1.IdHTTPGet(aURL: string; var sWeb: string): Boolean;
    var
      IdHTTP: TIdHTTP;
      SS_Response: TStringStream; // 提交后返回的数据
    begin
      Result := False;
      if aURL = '' then Exit;
      sWeb := '';
      IdHTTP := TIdHTTP.Create(nil);
      try
        try
          with IdHTTP do
          begin
            // IdHTTP 设置
            HTTPOptions := HTTPOptions + [hoKeepOrigProtocol];   //保持 并使用PV1_1
            ProtocolVersion := pv1_1;
            HTTPOptions := HTTPOptions - [hoForceEncodeParams];  //去掉自动编码
            AllowCookies := True;
            HandleRedirects := True;
            ConnectTimeout := 30000;
            ReadTimeout := 30000;
            // IdHTTP 提交信息的设置
            Request.Accept := 'application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5';
            Request.AcceptCharSet := 'GBK,utf-8;q=0.7,*;q=0.3';
            Request.AcceptEncoding := '';     //'gzip,deflate,sdch';
            Request.AcceptLanguage := 'zh-CN,zh;q=0.8';
            Request.Connection :='Keep-Alive';
            Request.ContentType := 'application/x-www-form-urlencoded';
            Request.UserAgent := 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.133 Safari/534.16';
            Request.Referer := aURL;        SS_Response := TStringStream.Create('', TEncoding.GetEncoding(936)); //或者TEncoding.UTF8 
            try
              Get(aURL, SS_Response);
              if ResponseCode <> 200 then Exit;
              if SS_Response.Size <= 1 then Exit;
              sWeb := SS_Response.DataString;
              Result := True;
            finally
              FreeAndNil(SS_Response);
            end;
          end;
        except
          on E: Exception do
            Result := False;
        end;
      finally
        IdHTTP.Disconnect;
        FreeAndNil(IdHTTP);
      end;
    end;
    重贴一下 代码乱了
      

  4.   

    你这个不是DELPHI7版本吧
    SS_Response := TStringStream.Create('', TEncoding.GetEncoding(936)); 我无法创建如此格式的字符串流啊。。
    除了字符串流我没有改,其它设置都改成你这样了,但有的还是解不了码。。如下:
    try{LotteryActivityCallback_69({'retCode' : '-101','retInfo' : '%E6%82%A8%E8%BF%98%E6%B2%A1%E6%9C%89%E7%99%BB%E5%BD%95%EF%BC%8C%E8%AF%B7%E9%A6%96%E5%85%88%E7%99%BB%E5%BD%95','iActivityId' : '69','iRuleId' : '','iRuleSetId' : '','iPackageId' : '','sPackageName' : '','iPackageType' : '','sPackageOtherInfo' : '','sHugeTips' : ''});}catch(e){}
      

  5.   

    %E6%82%A8%E8%BF%98%E6%B2%A1%E6%9C%89%E7%99%BB%E5%BD%95%EF%BC%8C%E8%AF%B7%E9%A6%96%E5%85%88%E7%99%BB%E5%BD%95这个好像是用httpdecode解,忘记了。也可以自己解。
      

  6.   


    %E6%82%A8%E8%BF%98%E6%这种编码用utf8decode(httpdecode(recivedata.response))解码出来了,\u60a8\u7684\u9a8c\u8bc1这样的的编码还是解不出来。。
    SS_Response := TStringStream.Create('', TEncoding.GetEncoding(936))不是DELPHI7的函数吧,,我创建不了,,其它参数都修改得和2楼的差不多,但还是需要解码,真头疼啊大家教教我啊。。
      

  7.   

    使用TStringStream就可以了
    TEncoding.UTF8
    3楼的正确!
      

  8.   

    response:=TStringStream.Create('', TEncoding.GetEncoding(936));这个函数我都用不了啊提示我参数太多了
      

  9.   


    function Encode2(s:WideString):String;//完成\uXXXX方式的汉字转换
    var
     i,len:Integer;
        cur:Integer;
           t:String;
    begin
        Result:= '';
        len:=Length(s);   
        i:=1; 
      while   i <=len   do   
      begin
          cur:=ord(s[i]);   //BCD转换
          if ( cur<19968) or (cur>40869) then //单字不转
          begin
            Result:=Result+s[i];
          end
          else begin
            FmtStr(t, '\u%4.4X',[cur]);
            Result:=Result+t;
          end;
          inc(i);   
        end;   
    end; procedure TForm1.Button1Click(Sender: TObject);
    var
    s,str:string;
    i:integer;
    vScript: TScriptControl;
    begins:=Encode2(edit1.Text);
    memo1.Lines.Add(s);
    vScript:=TScriptControl.Create(nil);
    vScript.Language := 'JavaScript';
    str:=vScript.Eval('unescape('''+s+''')');
    memo1.Lines.Add(str);
    vScript.free;
    end;