2013-07-25 29 views
2

我已經成功地使用Delphi 2010來製作http獲取請求,但對於需要一個名爲'xml'的參數的一個服務,請求失敗並顯示'HTTP/1.1 400 Bad Request'錯誤。如何使用Tidhttp使用名爲xml的參數進行Get請求?

我注意到調用相同的服務,並省略'xml'參數的作品。

我曾嘗試沒有成功如下:

HttpGet('http://localhost/Service/Messaging.svc/SendReports/PDF?xml=<?xml version="1.0"?><email><message><to>[email protected]</to><from>[email protected]</from></message></email>&id=42&profile=A1'); 

...

function TReportingFrame.HttpGet(const url: string): string; 
var 
    responseStream : TMemoryStream; 
    html: string; 
    HTTP: TIdHTTP; 
begin 
    try 
     try 
     responseStream := TMemoryStream.Create; 
     HTTP := TIdHTTP.Create(nil); 
     HTTP.OnWork:= HttpWork; 
     HTTP.Request.ContentType := 'text/xml; charset=utf-8'; 
     HTTP.Request.ContentEncoding := 'utf-8'; 
     HTTP.HTTPOptions := [hoForceEncodeParams]; 
     HTTP.Request.CharSet := 'utf-8'; 
     HTTP.Get(url, responseStream); 
     SetString(html, PAnsiChar(responseStream.Memory), responseStream.Size); 
     result := html; 
     except 
     on E: Exception do 
      Global.LogError(E, 'ProcessHttpRequest'); 
     end; 
    finally 
     try 
     HTTP.Disconnect; 
     except 
     end; 
    end; 
end; 

調用帶有更名爲別的參數名 'XML' 相同的URL,比如 'XML2'或'名稱'與上面相同的值也適用。我也嘗試了字符集的多種組合,但我認爲indy組件正在內部改變它。

編輯

的服務要求:

[WebGet(UriTemplate = "SendReports/{format=pdf}?report={reportFile}&params={jsonParams}&xml={xmlFile}&profile={profile}&id={id}")] 

有沒有人有這樣的經驗嗎?

感謝

+0

OT :你可以調用'TIdHTTP.Get'重載,它返回字符串並將其直接賦值給'Result'。這將允許您刪除當前泄漏的流部分。 – TLama

+0

完成。關於text/xml字符集/錯誤請求問題的任何建議? – reckface

+0

1)顯示服務的規格,它如何期望參數通過? /// 2)你有沒有像WWW瀏覽器的任何參考演示程序可以調用成功的服務? /// 3)嘗試使用百分比十六進制編碼對所有無效字符進行編碼:http://en.wikipedia.org/wiki/Url#List_of_allowed_URL_characters /// 4)嘗試將xml作爲編碼詞base64流http://en.wikipedia.org/wiki/MIME#Encoded-Word –

回答

6

您需要編碼參數數據通過URL傳遞時,TIdHTTP不會編碼的網址給你,例如:

http.Get(TIdURI.URLEncode('http://localhost/Service/Messaging.svc/SendReports/PDF?xml=<?xml version="1.0"?><email><message><to>[email protected]</to><from>[email protected]</from></message></email>&id=42&profile=A1')); 

或者:

http.Get('http://localhost/Service/Messaging.svc/SendReports/PDF?xml=' + TIdURI.ParamsEncode('<?xml version="1.0"?><email><message><to>[email protected]</to><from>[email protected]</from></message></email>') + '&id=42&profile=A1'); 
+0

謝謝!那樣做了。事實證明,http 500錯誤真的是來自服務器:從客戶端檢測到潛在危險的Request.QueryString值。我已經相應地修改了web.config文件 – reckface