簡體   English   中英

Delphi Indy發布錯誤501

[英]Delphi Indy post error 501

我在嘗試使用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

其他客戶端(例如普通curl,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的成員實例。

有人可以指出我做錯了什么嗎?

在發布流之前,您無需將流的“ 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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM