首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Delphi XE Indy连接到本地服务器api,但不连接公共服务器api。

Delphi XE Indy连接到本地服务器api,但不连接公共服务器api。
EN

Stack Overflow用户
提问于 2016-09-19 13:21:38
回答 1查看 240关注 0票数 0

我在使用Delphi与Indy客户端连接到公共域api时遇到了问题。

我可以成功地连接到本地主机web服务器api (apache),但是在共享主机上对远程服务器(公共域)的类似尝试会导致一个禁止的403错误。

我可以成功地使用cURL访问相同的公共域api。因此,我排除了共享主机服务器上的任何权限/防火墙问题。

代码语言:javascript
运行
复制
    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;

在调用公共api之前,是否需要设置/调整TIdHTTP Indy组件的属性/设置?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-09-20 09:03:30

经过相当多的研究,我在印第知识库上找到了解决问题的方法。

http://www.indyproject.org/KB/iamgettinga403forbiddene.htm

我将indy组件的UserAgent属性从默认值更改为

Mozilla/3.0 (兼容;印第图书馆)

而且很管用!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39574291

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档