0

我是一个新手,正在编写一个 SOAP Web 服务(用于集成目的),为了执行 SOAP 调用,我需要首先对用户进行身份验证(标准集成用户)。

以下是它的代码片段。但是,当我执行调用时,它会为基本 Http 请求抛出错误代码 500,而为第二个 Http 请求抛出错误代码 401。

这是正确的方法吗?

HTTP auth = new HTTP();
HTTPRequest r = new HTTPRequest();
r.setEndpoint('https://domainname.net/enterprise/soap?ServiceName=IntegrationManagementService');
Blob headerValue = Blob.valueOf(username+':'+password);
String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue);
r.setHeader('Authorization', authorizationHeader);
r.setMethod('POST');

try
{
 HTTPResponse authresp = auth.send(r);
 if(authresp.getStatusCode() == 200)
       { 
           system.debug('Authentication success!!!' + authresp);
       }
       else
       {system.debug('Authentication failed!!!' + authresp + authresp.getStatusCode());}    
         }catch(exception e){}

   //construct http request
    string endpointURL = 'https://doaminname.net/enterprise/soap?ServiceName=IntegrationManagementService';
   HttpRequest req = new HttpRequest();
   req.setMethod('POST');

   req.setEndpoint(endpointURL);
   req.setHeader('Content-Type','application/xml');
   req.setBody(TaleoXML);

   //send http request
   Http http = new Http();
   try
   {
       HttpResponse res = http.send(req);

       //check the response
       if(res.getStatusCode() == 200)
       { 
           system.debug('Callout success!!!' + res);
       }
       else
       {system.debug('Callout failed!!!' + res + res.getStatusCode());}    
   }catch(exception e){}  
4

2 回答 2

0

我不熟悉您在这里使用的库,但我可以建议一些调查的可能性:

  • 基本身份验证是一种无状态方法。您没有登录并保持登录状态。这意味着没有单独的初始身份验证请求(正如您的代码所暗示的那样);您在Authorization每个请求中都包含标头。这就是为什么您在第二个请求中获得401的原因。

  • 在第一个请求中,您提供了凭据并且服务器遇到了意外的内部错误(这就是500的含义)。如果它包含带有错误响应的正文,则它可能包含更多信息。我猜这与您没有为您的 POST 提供正文并且服务器没有预料到这一事实有关。

如果这是您第一次使用 SOAP,则最好使用专用的 SOAP 库,而不是尝试自己构造请求。

于 2013-07-31T18:39:33.307 回答
0

在第二个请求中,您不包含身份验证,这就是您收到 401(未经授权)错误的原因。

在第一个请求中,您的身份验证似乎正常,但服务器无法处理该请求。我认为您错过了参考您要使用的 IntegrationManagementService 网络服务的功能/操作。或者您正在使用需要启用 MTOM 的功能/操作。

于 2018-03-06T13:06:36.613 回答