gpt4 book ai didi

java - GWT RequestBuilder setHeader 不起作用

转载 作者:行者123 更新时间:2023-12-01 10:11:04 26 4
gpt4 key购买 nike

我正在尝试使用以下类连接到 OAuth2 服务器实现(为了完整性而发布完整内容):

import com.google.gwt.http.client.*;
import com.googlecode.gwt.crypto.bouncycastle.util.encoders.Base64;

import java.util.Map;
import java.util.function.Consumer;

public class HttpManager {

private static String toFormData(Map<String, String> data) {

StringBuilder sb = new StringBuilder();

for (Map.Entry<String, String> entry : data.entrySet())
sb.append("&")
.append(entry.getKey())
.append("=")
.append(entry.getValue());

sb.deleteCharAt(0);
return sb.toString();
}

public void postFormData(String url, Map<String, String> data, Consumer<Response> onSuccess, Consumer<Response> onFail) throws RequestException {

final String dataAsString = toFormData(data);
RequestBuilder builder = new RequestBuilder(RequestBuilder.POST, url);
builder.setHeader("Content-Type", "application/x-www-form-urlencoded");
builder.setHeader("Accept", "application/x-www-form-urlencoded");
builder.setHeader("Authorization", "Basic " + Base64.encode(("appId" + ":" + "oauthSecret").getBytes()));

builder.sendRequest(dataAsString, new RequestCallback() {
@Override
public void onResponseReceived(Request request, Response response) {
if(response.getStatusCode() >= 400) {
onFail.accept(response);
} else {
onSuccess.accept(response);
}
}

@Override
public void onError(Request request, Throwable throwable) {
onFail.accept(null);
}
});
}
}

如您所见,我清楚地将 Content-TypeAcceptAuthorization 附加到 header 。此外,我还发布了一些数据。

但实际上,请求如下所示:

OPTIONS /oauth/token HTTP/1.1
Host: 127.0.0.1:8080
Connection: keep-alive
Access-Control-Request-Method: POST
Origin: http://192.168.2.101:8888
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36
Access-Control-Request-Headers: accept, authorization, content-type
Accept: */*
Referer: http://192.168.2.101:8888/demo.html
Accept-Encoding: gzip, deflate, sdch
Accept-Language: de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4

鉴于上述设置,我不会期望看到这样的请求。我的请求 header 发生了什么?而且,根本没有剩下请求正文。为什么请求类型是Option?有人可以透露一些信息吗?

最佳答案

这是由于请求是跨域的,您看到的是 CORS 中的预检请求说法。这是预期的行为,服务器必须使用适当的响应 header 对其进行授权。

关于java - GWT RequestBuilder setHeader 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36109695/

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