2016-09-19 59 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

回答