idhttp ISO-8859-1多部分帖子

时间:2012-08-22 07:42:54

标签: encoding delphi-2010 indy indy10 multipartform-data

在试图猜测正确的方式2天后,我放弃了。 很多问题与主题但没有任何帮助我。

请告诉我我的错误。

任务:将multipart-form(字符串字段和文件)发送到服务器。 服务器等待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值,我需要ISO-8859-1。

UTF-8解决方案:

field := Stream.AddFormField(Values[i].Name, Values[i].Value, 'UTF-8');
field.ContentTransfer := '8bit';

2 个答案:

答案 0 :(得分:1)

假设您使用的是Indy 10的最新版本,那么TIdMultipartFormDataStream可以正常使用ISO-8859-1。只需指定您指定UTF-8的位置即可。您还需要修复Request.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;

或者:更符合您展示的“解决方案”:

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;

答案 1 :(得分:0)

<强>解

许多解决方案都在互联网上。雷米的很多解决方案。 但所有这些都是关于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;
相关问题