Delphi Indy发布错误501

时间:2013-11-19 15:04:39

标签: xml delphi indy10 tastypie

我在尝试使用Indy将xml发布到Web服务时遇到了一些麻烦。

我的客户端是使用Indy 10.0.52的delphi中的一个类,我的api服务器是django-tastypie。 我在服务器上的堆栈是:

Django==1.5.1
defusedxml==0.4.1
django-tastypie==0.10.0
lxml==3.2.3
mimeparse==0.1.3

其他客户端(如普通卷曲,python,js)发布到相同的资源没有问题。 例如:

$ curl --dump-header - -H "Content-Type: application/xml" -H "Authentication: Basic 875488" -k -X POST --data '<object>...</object>' https://www....object/?format=xml
HTTP/1.1 201 CREATED
Date: Tue, 19 Nov 2013 10:28:01 GMT
Server: Apache/2.2.14 (Ubuntu)
Access-Control-Allow-Headers: Origin,Authorization,Content-Type,Accept
Vary: Accept,Accept-Encoding
Location: https://www....object/12
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST,GET,OPTIONS,PUT,DELETE
Content-Length: 0
Content-Type: text/html; charset=utf-8

但是使用Indy我总是得到:'501未实施'。 这是我的程序:

procedure TRestObject.post( postURL:String; doc:IXMLDocument );
var
  aStream : TMemoryStream;
  data : TStringStream;
  xmlString, urlString : String;
begin
  aStream := TMemoryStream.Create;
  data := TStringStream.Create('');
  try
    http.HandleRedirects := True;
    http.ReadTimeout := 50000;
    // encoding
    xmlString := doc.XML.Strings[1];
    urlString := URLencode(xmlString);
    showmessage(xmlString);
    //showmessage(urlString);
    data.WriteString( urlString );
    aStream.Write( urlString[1], Length(urlString) * SizeOf(Char) );
    with http do
    begin
      try
        Response.KeepAlive := False;
        //Post( postURL, data, aStream );
        Post( postURL, aStream );
      except
        on E: EIdException do
          showmessage('Exception (class '+ E.ClassName +'): ' + E.Message);
        on E: EIdHTTPProtocolException do
          showmessage('Protocol Exception (HTTP status '+ IntToStr(E.ErrorCode) +'): ' + E.Message);
        on E: EIdSocketError do
          showmessage('Socket Error ('+ IntToStr(E.LastError) +'): ' + E.Message);
      end;
    end;
  finally
    aStream.Free;
    data.Free;
  end;
end;

'http'是Indy TIdHTTP的成员实例。

有人可以指出我做错了吗?

1 个答案:

答案 0 :(得分:1)

在发布之前,您没有将流的Position重置为0,您甚至根本没有发布XML流(您已将其注释掉)。您也没有考虑字符编码,也没有复制传递给curl的相同属性。

试试这个:

procedure TRestObject.post( postURL:String; doc:IXMLDocument );
var
  aStream: TMemoryStream;
  data : TStringStream;
  xmlString : String;
begin
  aStream := TMemoryStream.Create;
  try
    http.HandleRedirects := True;
    http.ReadTimeout := 50000;
    // encoding
    xmlString := doc.XML.Text;
    ShowMessage(xmlString);
    data := TStringStream.Create(xmlString, TEncoding.UTF8);
    try
      http.Request.ContentType := 'application/xml';
      http.Request.Connection := 'close';
      try
        http.Post( postURL, data, aStream );
      except
        on E: EIdHTTPProtocolException do
          ShowMessage('Protocol Exception (HTTP status '+ IntToStr(E.ErrorCode) +'): ' + E.Message);
        on E: EIdSocketError do
          ShowMessage('Socket Error ('+ IntToStr(E.LastError) +'): ' + E.Message);
        on E: Exception do
          ShowMessage('Exception (class '+ E.ClassName +'): ' + E.Message);
      end;
    finally
      data.Free;
    end;
  finally
    aStream.Free;
  end;
end;