2016-09-19 58 views
0

我有問題連接到使用Delphi XE的Indy客戶端的公共領域API。Delphi XE Indy連接到本地服務器API但不是公共服務器API

我可以成功連接到我的本地主機web服務器api(apache),但類似的嘗試遠程服務器(公共域)共享主機給了我一個禁止的403錯誤。

我可以使用cURL成功訪問相同的公共領域api。因此,我排除了共享託管服務器上任何與權限/防火牆有關的問題。

function CallService(ServiceID: string;payload:string): string; 
    var 
     JsonToSend: TStringStream; 
     ServerResponse,EndPointURL: string; 
     LastJSONArray: TStringList; 
     MyIndy : TIdHTTP; 
    begin 

    //Local connection WORKS :) 
    EndPointURL := 'http://localhost/api/index.php'; 

    //Remote/Public Domain connection FAILS :(
    EndPointURL := 'http://example.com/api/index.php'; 

    LastJSONArray := TStringList.Create(); 

    LastJSONArray.Values['service_id'] := ServiceID; 
    LastJSONArray.Values['payload'] := payload; 

    JsonToSend := TStringStream.Create(LastJSONArray.Text, TEncoding.UTF8); 

     MyIndy := TIdHTTP.Create; 

     try 

     try 

      MyIndy.Request.Accept := 'application/json'; 
      MyIndy.Request.ContentType := 'application/json'; 
      MyIndy.Request.ContentEncoding := 'utf-8';   

      ServerResponse := MyIndy.Post(EndPointURL, JsonToSend); 

      Result := ServerResponse; 

     except 
      on E: EIdHTTPProtocolException do 
      //status := http.ResponseText; 
      //code := E.ErrorCode; 
      if E.ErrorCode = 401 then ShowMessage('You are not authorised!') 
      else ShowMessage('Poor Connection '+E.Message); 

      on E: Exception do begin 
      //do something else 
      ShowMessage('Poor Connection - B'); 
      end; 

     end; 

     finally 
     MyIndy.Free(); 
     JsonToSend.Free(); 
     LastJSONArray.Free(); 
     end; 

    end; 

是否有物業/與TIdHTTP印組件,我需要設置/調用公共API之前調整設置?

+0

你的cURL代碼是什麼樣的? 403意味着你無法訪問URL,它可能需要你沒有給予'TIdHTTP'的認證證書。你給任何cURL?順便說一下,'utf-8'不是有效的'Request.ContentEncoding'值。你應該使用'Request.Charset',如果有的話(你不需要爲'application/json'指定一個字符集)。 –

+0

嘿雷米。我發現問題出在組件的UserAgent屬性上。似乎它正在被主機阻止。 – davykiash

回答