http://www.myexception.cn/delphi/225992.html
小弟我用IDHTTP的get和post接收到的数据如何解码啊
www.MyException.Cn 网友分享于:2013-03-27 浏览:243次
我用IDHTTP的get和post接收到的数据怎么解码啊。。
有的时候直接就能看到正确的内容,有的时候需要加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;
------解决方案--------------------
有的时候直接就能看到正确的内容,有的时候需要加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;
------解决方案--------------------
- Delphi(Pascal) code
//------ 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;
------解决方案--------------------
delphi lazarus opengl
网页操作自动化, 图像分析破解,游戏开发