[英]Post XML with TidHttp Invalid request
我正在嘗試使用 Delphi 2007 發布一個 XML 文件以與 WorldPay 進行交易,但我一直收到“無效請求”錯誤。
這是我的代碼,我不知道我做錯了什么。
Var
XDoc, ReturnStr:String;
XMLToSend, resp: TStringStream;
IdHTTP1:TIdHTTP;
IdSSLIOHandlerSocketOpenSSL1: TIdSSLIOHandlerSocketOpenSSL;
begin
with Datam1.CCSetupTbl do
begin
XDoc := '<?xml version="1.0" encoding="UTF-8"?>' +
'<TransactionSetup xmlns="https://certtransaction.elementexpress.com">' +
'<Credentials>' +
'<AccountID>' + FieldByName('M').AsString + '</AccountID>' +
'<AccountToken>' + FieldByName('MW').AsString + '</AccountToken>' +
'<AcceptorID>' + FieldByName('MP').AsString + '</AcceptorID>' +
'</Credentials>' +
'<Application>' +
'<ApplicationID>00000</ApplicationID>' +
'<ApplicationVersion>7.60.0</ApplicationVersion>' +
'<ApplicationName>CPOS</ApplicationName>' +
'</Application>' +
'<TransactionSetup>' +
'<TransactionSetupMethod>7</TransactionSetupMethod>' +
'<Embedded>1</Embedded>' +
'<CVVRequired>0</CVVRequired>' +
'<AutoReturn>1</AutoReturn>' +
'<ReturnURL>http://localhost</ReturnURL>' +
'<CustomCss>body{margin-left: 50px;}</CustomCss>' +
'</TransactionSetup>' +
'<Transaction>' +
'<TransactionAmount>1.00</TransactionAmount>' +
'<MarketCode>0</MarketCode>' +
'<ReferenceNumber>123456</ReferenceNumber>' +
'<TicketNumber>123456</TicketNumber>' +
'<PartialApprovedFlag>0</PartialApprovedFlag>' +
'</Transaction>' +
'<Terminal>' +
'<TerminalID>01</TerminalID>' +
'<TerminalType>0</TerminalType>' +
'<CardholderPresentCode>0</CardholderPresentCode>' +
'<CardInputCode>0</CardInputCode>' +
'<TerminalCapabilityCode>0</TerminalCapabilityCode>' +
'<TerminalEnvironmentCode>0</TerminalEnvironmentCode>' +
'<CardPresentCode>0</CardPresentCode>' +
'<MotoECICode>0</MotoECICode>' +
'<CVVPresenceCode>0</CVVPresenceCode>' +
'</Terminal>'+
'<PaymentAccount>' +
'<PaymentAccountType>1</PaymentAccountType>' +
'<PaymentAccountReferenceNumber>' + CusAccount + '</PaymentAccountReferenceNumber>' +
'</PaymentAccount>' +
'</TransactionSetup>';
end;
Try
IdHTTP1 := TIdHTTP.Create;
with IdHTTP1 do
begin
Request.Accept := 'text/html';
Request.ContentType := 'text/html';
Request.CharSet := 'utf-8';
Request.CacheControl := 'no-cache';
ReadTimeout := 30000;
ConnectTimeout := 30000;
Request.BasicAuthentication := False;
Request.UserAgent := 'Mozilla/3.0 (compatible; Indy Library)';
HTTPOptions := [hoForceEncodeParams];
end;
IdSSLIOHandlerSocketOpenSSL1:= TIdSSLIOHandlerSocketOpenSSL.Create;
IdSSLIOHandlerSocketOpenSSL1.SSLOptions.SSLVersions:= [sslvTLSv1_2];
IdSSLIOHandlerSocketOpenSSL1.SSLOptions.Method := sslvTLSv1_2;
IdHttp1.IOHandler := IdSSLIOHandlerSocketOpenSSL1;
XMLToSend := TStringStream.Create(XDoc);
ReturnStr := IdHTTP1.Post('https://certtransaction.elementexpress.com', XMLToSend);
finally
IdHTTP1.Free;
IdSSLIOHandlerSocketOpenSSL1.Free;
XMLToSend.Free;
end;
好吧,對於初學者來說,您是在告訴服務器您正在發布 HTML 而不是 XML。 'text/html'
是錯誤的 XML Content-Type
。
其次,如果插入到 XML 中的值碰巧包含任何保留字符,則不會對它們進行轉義。 您確實應該使用適當的 XML 庫來准備發布數據。
最后,您的異常處理結構不是很好。
試試這個:
Var
XDoc, ReturnStr: String;
XMLToSend: TStringStream;
IdHTTP1: TIdHTTP;
IdSSLIOHandlerSocketOpenSSL1: TIdSSLIOHandlerSocketOpenSSL;
begin
with Datam1.CCSetupTbl do
begin
XDoc := ...;
end;
IdHTTP1 := TIdHTTP.Create;
try
with IdHTTP1 do
begin
Request.Accept := 'text/html';
Request.ContentType := 'text/xml'; // or 'application/xml', or whatever the server actually expects...
Request.CharSet := 'utf-8';
Request.CacheControl := 'no-cache';
ReadTimeout := 30000;
ConnectTimeout := 30000;
Request.BasicAuthentication := False;
Request.UserAgent := 'Mozilla/3.0 (compatible; Indy Library)';
HTTPOptions := [hoForceEncodeParams];
end;
IdSSLIOHandlerSocketOpenSSL1 := TIdSSLIOHandlerSocketOpenSSL.Create(IdHTTP1);
IdSSLIOHandlerSocketOpenSSL1.SSLOptions.SSLVersions := [sslvTLSv1_2];
IdHTTP1.IOHandler := IdSSLIOHandlerSocketOpenSSL1;
XMLToSend := TStringStream.Create(XDoc);
try
ReturnStr := IdHTTP1.Post('https://certtransaction.elementexpress.com', XMLToSend);
finally
XMLToSend.Free;
end;
finally
IdHTTP1.Free;
end;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.