在Salesforce中从Web服务调用中设置cookie

问题描述 投票:1回答:2

有人可以在下面解释一下我吗?

stub.inputHttpheaders_x.put('Cookie', 'name = value');

在这种情况下,“名称=值”是什么?

我正在获得如下cookie:

stub.outputHttpheaders_x.get('Set-cookie');

如何在第一条语句中使用此Cookie?

提前感谢。

salesforce apex-code
2个回答
1
投票

当您掌握存根时,可以使用inputHttpheaders_x.put方法设置HTTP标头字段。

Wikipedia link很好地描述了可以在HTTP标头上设置的字段。要设置的这些字段之一是“ Cookie”。可以将其设置为“键=值”值,例如“现场=谷歌”。

代码块

stub.inputHttpheaders_x.put('Cookie', 'name = value');

将值'name = value'设置为Cookie标头字段。

类似地,您可以使用以下方法在响应对象上的HTTP标头中访问设置的cookie值:

String cookie = stub.outputHttpHeaders_x.get('Set-Cookie')

希望这很有道理!

阿努普附注:如果您尝试使用适当的集成设置来尝试此操作。尝试打印出值以了解输出的格式。


0
投票

准确地说:

您必须通过初始化地图拳头来“启用” outputHttpHeaders_x之后,您可以访问Set-Cookie。

可以在这里阅读:https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_callouts_wsdl2apex.htm

outputHttpHeaders_x的值默认为空。您必须先设置outputHttpHeaders_x,然后才能访问响应中标头的内容。

docSample.DocSamplePort stub = new docSample.DocSamplePort();
stub.outputHttpHeaders_x = new Map<String, String>();
String input = 'This is the input string';
String output = stub.EchoString(input);

//Getting cookie header
String cookie = stub.outputHttpHeaders_x.get('Set-Cookie');

//Getting custom header
String myHeader = stub.outputHttpHeaders_x.get('My-Header');
© www.soinside.com 2019 - 2024. All rights reserved.