Encoding idhttp ISO-8859-1多部分post
经过两天的猜测,我放弃了。 这方面有很多问题,但对我毫无帮助 请告诉我我的错误 任务:将多部分表单(字符串字段和文件)发送到服务器。 服务器等待ISO-8859-1编码Encoding idhttp ISO-8859-1多部分post,encoding,delphi-2010,indy,indy10,multipartform-data,Encoding,Delphi 2010,Indy,Indy10,Multipartform Data,经过两天的猜测,我放弃了。 这方面有很多问题,但对我毫无帮助 请告诉我我的错误 任务:将多部分表单(字符串字段和文件)发送到服务器。 服务器等待ISO-8859-1编码 http.Request.Host := fHost; http.Request.AcceptEncoding := '*'; http.Request.UserAgent := HTTPUserAgent; http.Request.ContentEncoding := 'ISO-8859-1'
http.Request.Host := fHost;
http.Request.AcceptEncoding := '*';
http.Request.UserAgent := HTTPUserAgent;
http.Request.ContentEncoding := 'ISO-8859-1';
// http.Request.CharSet := 'ISO-8859-1';
if HTTPProxyActive then
http.Request.ProxyConnection := 'close'
else
http.Request.Connection := 'close';
http.Request.ContentType := 'text/plain';
addr := 'https://'+Host+URL;
if ValCount>0 then begin
Stream := TIdMultipartFormDataStream.Create;
for i:=0 to ValCount-1 do begin
if Values[i].Name<>'' then
begin
field := Stream.AddFormField(Values[i].Name, Values[i].Value, 'ISO-8859-1');
// field.Charset := 'ISO-8859-1';
// field.ContentTransfer := '7bit';
end;
if Values[i].Filename<>'' then
Stream.AddFile(Values[i].FileName, Values[i].Value, 'text/plain');
end;
resp := TStringStream.Create;
http.Post(addr, Stream, resp);
st := resp.DataString;
resp.Destroy;
Stream.Destroy;
end
解决方案 互联网上有很多解决方案。雷米提供了很多解决方案。 但它们都是关于UTF-8的。 服务器无法获取UTF-8字符串。。。我无法访问服务器端脚本来纠正这种情况。 因此,我在不使用TIdMultipartFormDataStream类的情况下手动创建请求 我想我的解决方案对下一个会很有用。 祝你好运
http.Request.ContentType := 'multipart/form-data; boundary=' + FBound;
if HTTPProxyActive then
http.Request.ProxyConnection := 'close'
else
http.Request.Connection := 'close';
addr := 'http://'+Host+URL;
resp := TStringStream.Create;
if ValCount>0 then
begin
cont := '';
for i:=0 to ValCount - 1 do
begin
cont := cont + '--' + FBound + #13#10;
cont := cont + 'Content-Disposition: form-data';
if Values[i].Filename<>'' then
cont := cont + '; filename="' + Values[i].Filename + '"';
if Values[i].Name<>'' then
cont := cont + '; name="' + Values[i].Name + '"';
cont := cont + #13#10+#13#10;
cont := cont + Values[i].Value;
cont := cont + #13#10;
end;
cont := cont + '--' + FBound + '--' + #13#10#13#10;
http.Request.ContentLength := Length(cont);
req := TStringStream.Create(cont);
http.Post(addr, req, resp);
FreeAndNil(req);
end
else
begin
http.Get(addr, resp);
end;
st := resp.DataString;
resp.Destroy;
http.Request.ContentType:='多部分/表单数据;边界='+fBond;
如果HTTPProxyActive处于活动状态,则
http.Request.ProxyConnection:=“关闭”
其他的
http.Request.Connection:=“关闭”;
地址:='http://'+主机+URL;
resp:=TStringStream.Create;
如果ValCount>0,则
开始
续:='';
对于i:=0到ValCount-1 do
开始
续:=cont+'-'+fBond+#13#10;
cont:=cont+'Content Disposition:form data';
如果值为[i]。文件名为“”,则
cont:=cont+';filename=“”+值[i]。filename+”;
如果值为[i]。名称为“”,则
cont:=cont+';name=“”+值[i]。name+”;
cont:=cont+#13#10+#13#10;
cont:=cont+值[i]。值;
续:=续+#13#10;
结束;
cont:=cont+'-'+FBound+'-'+#13#10#13#10;
http.Request.ContentLength:=长度(续);
请求:=TStringStream.Create(cont);
http.Post(地址、请求、响应);
自由零(req);
结束
其他的
开始
http.Get(addr,resp);
结束;
st:=相应的数据字符串;
分别销毁;
假设您使用的是最新版本的Indy 10,那么TIdMultipartFormDataStream
在ISO-8859-1上运行良好。只要指定UTF-8即可。您还需要修复请求中的错误。ContentEncoding
分配-字符串字符集不是有效的内容编码。这是HTTP的一个完全不同的特性。您需要去掉TStringStream
,因为它会阻止TIdHTTP
为您解码响应字符串数据
试试这个:
if ValCount > 0 then
begin
http.Request.AcceptEncoding := '*';
http.Request.UserAgent := HTTPUserAgent;
if HTTPProxyActive then
http.Request.ProxyConnection := 'close'
else
http.Request.Connection := 'close';
addr := 'https://'+Host+URL;
Stream := TIdMultipartFormDataStream.Create;
try
for i := 0 to ValCount-1 do begin
if Values[i].Name <> '' then
begin
field := Stream.AddFormField(Values[i].Name, Values[i].Value, 'ISO-8859-1');
field.ContentTransfer := '8bit';
end;
if Values[i].FileName <> '' then
Stream.AddFile(Values[i].Name, Values[i].FileName, 'text/plain');
end;
st := http.Post(addr, Stream);
finally
Stream.Free;
end;
end;
如果ValCount>0,则
开始
http.Request.AcceptEncoding:='*';
http.Request.UserAgent:=HTTPUserAgent;
如果HTTPProxyActive处于活动状态,则
http.Request.ProxyConnection:=“关闭”
其他的
http.Request.Connection:=“关闭”;
地址:='https://'+主机+URL;
Stream:=TIdMultipartFormDataStream.Create;
尝试
对于i:=0到ValCount-1,开始
如果值为[i]。名称为“”,则
开始
字段:=Stream.AddFormField(值[i]。名称,值[i]。值“ISO-8859-1”);
field.ContentTransfer:=“8位”;
结束;
如果值为[i]。文件名为“”,则
Stream.AddFile(值[i].Name,值[i].FileName,'text/plain');
结束;
st:=http.Post(地址,流);
最后
免费;
结束;
结束;
或者:更符合您展示的“解决方案”:
if ValCount > 0 then
begin
http.Request.AcceptEncoding := '*';
http.Request.UserAgent := HTTPUserAgent;
if HTTPProxyActive then
http.Request.ProxyConnection := 'close'
else
http.Request.Connection := 'close';
addr := 'https://'+Host+URL;
Stream := TIdMultipartFormDataStream.Create;
try
for i := 0 to ValCount-1 do begin
if Values[i].Name <> '' then
begin
field := Stream.AddFormField(Values[i].Name, Values[i].Value, 'ISO-8859-1');
field.ContentTransfer := '8bit';
field.FileName := Values[i].FileName;
end;
end;
st := http.Post(addr, Stream);
finally
Stream.Free;
end;
end;
如果ValCount>0,则
开始
http.Request.AcceptEncoding:='*';
http.Request.UserAgent:=HTTPUserAgent;
如果HTTPProxyActive处于活动状态,则
http.Request.ProxyConnection:=“关闭”
其他的
http.Request.Connection:=“关闭”;
地址:='https://'+主机+URL;
Stream:=TIdMultipartFormDataStream.Create;
尝试
对于i:=0到ValCount-1,开始
如果值为[i]。名称为“”,则
开始
字段:=Stream.AddFormField(值[i]。名称,值[i]。值“ISO-8859-1”);
field.ContentTransfer:=“8位”;
field.FileName:=值[i]。文件名;
结束;
结束;
st:=http.Post(地址,流);
最后
免费;
结束;
结束;
Remy,谢谢。但我尝试了提供的方式,正如我所记得的那样,在???方面有更长的问题。您使用的是最新版本的Indy 10吗?获取字符的唯一方法是输入数据使用ISO-8859-1中不存在的非ASCII字符。您的代码没有处理这种可能性。您的代码完全忽略字符集,并使用TEncoding.Default
对字符串数据进行编码(如果您不告诉它使用不同的编码,则TStringStream
会在内部使用该编码)。而对于Indy,它会将字符串数据精确地编码到您指定的任何字符集,在本例中为ISO-8859-1。非常感谢。我需要非常快速的解决方案,不幸的是,我花了3天的时间,我没有时间尝试其他方法。我的客户有工作代码。如果我们将来有问题,我会按照你的方式去做。再次感谢你的伟大图书馆。
if ValCount > 0 then
begin
http.Request.AcceptEncoding := '*';
http.Request.UserAgent := HTTPUserAgent;
if HTTPProxyActive then
http.Request.ProxyConnection := 'close'
else
http.Request.Connection := 'close';
addr := 'https://'+Host+URL;
Stream := TIdMultipartFormDataStream.Create;
try
for i := 0 to ValCount-1 do begin
if Values[i].Name <> '' then
begin
field := Stream.AddFormField(Values[i].Name, Values[i].Value, 'ISO-8859-1');
field.ContentTransfer := '8bit';
field.FileName := Values[i].FileName;
end;
end;
st := http.Post(addr, Stream);
finally
Stream.Free;
end;
end;