gpt4 book ai didi

java - 在Azure Function Java中,如何使用HttpResponseMessage设置cookie

转载 作者:行者123 更新时间:2023-12-02 00:08:59 26 4
gpt4 key购买 nike

在 Azure Function Java 中,如何使用 HttpResponseMessage 设置 cookie,因为我在 2.0 版本中没有看到任何支持该方法的方法。

最佳答案

正如 official documentation 中所建议的,如果可能的话,函数应该是无状态和幂等的。

Associate any required state information with your data. For example, an order being processed would likely have an associated state member. A function could process an order based on that state while the function itself remains stateless.

通过检查HttpRequestMessageHttpRequestMessage.Builder接口(interface),很抱歉没有直接的方法来设置cookie。

解决方法是创建您自己的 MyHttpResponseMessage 类,该类实现 HttpResponseMessage 接口(interface)。

这只是一个快速示例:

HttpResponseMessageBuilder 类

public class HttpResponseMessageBuilder {

HttpStatusType status;
Map<String,String> headers;
Object body;

private HttpResponseMessageBuilder(){};

public static HttpResponseMessageBuilder newResponseMessage(){
return new HttpResponseMessageBuilder();
}

public HttpResponseMessageBuilder status(HttpStatusType type){
status = type;
return this;
}

public HttpResponseMessageBuilder header(String key, String value){
if(headers == null) headers = new HashMap<>();
headers.put(key, value);
return this;
}

public HttpResponseMessageBuilder body(Object object){
this.body = object;
return this;
}

public HttpResponseMessageBuilder setCookie(String name,String value, String expire, String path, String domain, Boolean isSecure, Boolean isHttpOnly) {
StringBuilder sb = new StringBuilder();
sb.append(name).append("=").append(value);
if(expire != null)
sb.append(";expires=").append(expire);

if(path != null)
sb.append(";path=").append(path);
else
sb.append(";path=").append("/");

if(domain != null)
sb.append(";domain=").append(domain);

if(isSecure)
sb.append(";Secure");

if(isHttpOnly)
sb.append(";HttpOnly");

header("Set-Cookie", sb.toString());

return this;
}

public HttpResponseMessage build(){
MyHttpResponseMessage myHttpResponseMessage = new MyHttpResponseMessage();
if(status == null) status = HttpStatusType.custom(200);
myHttpResponseMessage.setStatus(status);
if(headers == null) headers = new HashMap<>();
myHttpResponseMessage.setHeaders(headers);
if(body == null) body = "";
myHttpResponseMessage.setBody(body);
return myHttpResponseMessage;
}
}

MyHttpResponseMessage 类

public class MyHttpResponseMessage implements HttpResponseMessage {

HttpStatusType status;
Map<String,String> headers;
Object body;

@Override
public HttpStatusType getStatus() {
return status;
}

@Override
public String getHeader(String key) {
return headers.get(key);
}

@Override
public Object getBody() {
return body;
}

protected void setStatus(HttpStatusType status) {
this.status = status;
}


protected void setHeaders(Map<String, String> headers) {
this.headers = headers;
}

protected void setBody(Object body) {
this.body = body;
}

}

最后,您可以在您的函数中使用它:

return HttpResponseMessageBuilder.newResponseMessage()
.setCookie("SessionID", "1F5E409AE7020538130D15ACC54530F9", null, "/", ".domian.com", true, false)
.body("123456789").build();

关于java - 在Azure Function Java中,如何使用HttpResponseMessage设置cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58136629/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com