有的时候直接就能看到正确的内容,有的时候需要加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;
希望大侠们帮帮忙啊。有没有直接的解码函数或负责解码的组件或类啊。。我的代码如下:
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;
解决方案 »
- 在vista home basic上安装delphi 7,0的方法
- 求助,如何用Clientsocket 传送一个结构体
- 初学接口,不懂ING
- 关于fastReport的应用: 如何使Master data band 中数据隔行显示不同的底色???
- 又一个ADO问题,用ADO编程的朋友请进!
- 请教各位大虾,谁能给一个在D6中实现接收组播的例程,我手头的不能正常结束?
- TREEVIEW的权限问题?
- Indy9传文件
- 各位高人,请指教
- 让程序绝对崩溃的方法
- 求助。。刚学习delphi 求一个dbgrid数据导出HTML文件的例子。要求HTML格式与excel格式一样!
- 用IDHTTP怎么把带分行的数据发送出去呢。。。。
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类似 稍有不同
//------ 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;
重贴一下 代码乱了
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){}
%E6%82%A8%E8%BF%98%E6%这种编码用utf8decode(httpdecode(recivedata.response))解码出来了,\u60a8\u7684\u9a8c\u8bc1这样的的编码还是解不出来。。
SS_Response := TStringStream.Create('', TEncoding.GetEncoding(936))不是DELPHI7的函数吧,,我创建不了,,其它参数都修改得和2楼的差不多,但还是需要解码,真头疼啊大家教教我啊。。
TEncoding.UTF8
3楼的正确!
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;