我用Delphi 11.3做了一个多平台小程序。我正在使用
TNetHTTPClient
组件将 XML 格式的文本发送到 REST API。在 Win32 模式下运行它工作正常。在 Android 模式下运行会抛出错误:
不支持的媒体类型这是代码:
POST
我尝试以
procedure TForm13.Button1Click(Sender: TObject);
var
HttpClient: TNetHTTPClient;
Response: IHTTPResponse;
URL,user,key,XML, AFM: string;
s, F : TstringStream;
FormData: TMultipartFormData;
XMLData: TBytesStream;
PostData: TBytes;
begin
HttpClient := TNetHTTPClient.Create(nil);
try
URL := 'https://www1.gsis.gr/wsaade/RgWsPublic2/RgWsPublic2?WSDL';
XML := '<env:Envelope xmlns:env="http://www.w3.org/2003/05/soap-envelope" xmlns:ns1="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:ns2="http://rgwspublic2/RgWsPublic2Service" xmlns:ns3="http://rgwspublic2/RgWsPublic2">'
+' <env:Header>'
+' <ns1:Security>'
+' <ns1:UsernameToken>'
+' <ns1:Username>username</ns1:Username>'
+' <ns1:Password>key</ns1:Password>'
+' </ns1:UsernameToken>'
+' </ns1:Security>'
+' </env:Header>'
+' <env:Body>'
+' <ns2:rgWsPublic2AfmMethod>'
+' <ns2:INPUT_REC>'
+' <ns3:afm_called_by/>'
+' <ns3:afm_called_for>'+AFM+'</ns3:afm_called_for>'
+' <ns3:as_on_date>'+formatDateTime('yyyy-mm-dd',date)+'</ns3:as_on_date>'
+' </ns2:INPUT_REC>'
+' </ns2:rgWsPublic2AfmMethod>'
+' </env:Body>'
+'</env:Envelope>';
s := TstringStream.create(UTF8encode(XML));
Response := HttpClient.Post(URL, s, nil); // succeeds in win32, fails in Android
// Response := HttpClient.Post(URL, s, nil, [TNetHeader.Create('Content-Type', 'text/xml')]); // fails in both platforms
if Response.StatusCode = 200 then
begin
Memo1.Lines.Text := Response.ContentAsString();
end
else
begin
ShowMessage('Error: ' + Response.StatusCode.ToString + ' - ' + Response.StatusText);
end;
finally
HttpClient.Free;
end;
end;
或
TMultipartFormData
或 XMLData := TBytesStream.Create(TEncoding.UTF8.GetBytes(XML))
的身份发帖,但总是失败。
Response := HttpClient.Post(URL, s, nil, [TNetHeader.Create('Content-Type', 'text or application/xml')])
<?php
$url = $_SERVER['HTTP_URL'];
$username = $_SERVER['HTTP_NAME'];
$password = $_SERVER['HTTP_KEY'];
$XML = file_get_contents('php://input');
$ch = curl_init($url);
//curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $XML);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/xml', // Inform the server the data is XML
'Content-Length: ' . strlen($XML),
'aade-user-id: '.$username, // Custom header for username
'Ocp-Apim-Subscription-Key: '.$password // Custom header for password
));
$response = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
} else {
echo htmlspecialchars($response);
}
curl_close($ch);
?>