Delphi/Android 应用程序错误:“不支持的媒体类型”

问题描述 投票:0回答:1

我用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))
的身份发帖,但总是失败。
    

android rest delphi
1个回答
0
投票

    我将一个 php 文件(post2otherServer.php)保存到我的服务器,内容为:
  1. Response := HttpClient.Post(URL, s, nil, [TNetHeader.Create('Content-Type', 'text or application/xml')])

  2. 我执行命令
  3. <?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); ?>

  4. 工作完成!我的服务器充当代理,它似乎无法处理“不支持的媒体类型”问题。
© www.soinside.com 2019 - 2024. All rights reserved.