获取 JWS 令牌正在使用 curl 但不适用于 Java
Getting JWS Token is working with curl but not with Java
这是我用 curl 做的:
curl -d "username=<user>&password=<pass>" -X POST https://example.com/wp-json/api/v1/token
这很有效,我收到了令牌。
这是 curl 发送的内容:
POST https://example.com/wp-json/api/v1/token HTTP/1.1
Host: example.com
User-Agent: curl/7.55.1
Accept: */*
Content-Length: <length>
Content-Type: application/x-www-form-urlencoded
username=<user>&password=<pass>
Java 我收到代码 403。
这是 HttpClient 的调试输出:
org.apache.http.wire - http-outgoing-0 >> "POST /wp-json/api/v1/token HTTP/1.1[\r][\n]"
org.apache.http.wire - http-outgoing-0 >> "Content-Length: 45[\r][\n]"
org.apache.http.wire - http-outgoing-0 >> "Content-Type: application/x-www-form-urlencoded; charset=ISO-8859-1[\r][\n]"
org.apache.http.wire - http-outgoing-0 >> "Host: www.example.com[\r][\n]"
org.apache.http.wire - http-outgoing-0 >> "Connection: Keep-Alive[\r][\n]"
org.apache.http.wire - http-outgoing-0 >> "User-Agent: Apache-HttpClient/4.5.13 (Java/1.8.0_121)[\r][\n]"
org.apache.http.wire - http-outgoing-0 >> "Accept-Encoding: gzip,deflate[\r][\n]"
org.apache.http.wire - http-outgoing-0 >> "[\r][\n]"
org.apache.http.wire - http-outgoing-0 >> "username=<user>&password=<pass>"
我在这里看不到 curl 和 java 之间的显着差异。
谁知道我在这里做错了什么?
这是我当前的 java 代码:
public void getToken(URI url, String username, String password) {
HttpEntity entity = new StringEntity("username=" + username + "&password=" + password, ContentType.APPLICATION_FORM_URLENCODED);
HttpHost target = new HttpHost(url.getHost(), 443, "https");
SSLContext sslcontext = SSLContexts.createSystemDefault();
SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(
sslcontext, new String[] { "TLSv1.2", "SSLv3" }, null,
SSLConnectionSocketFactory.getDefaultHostnameVerifier());
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.INSTANCE)
.register("https", sslConnectionSocketFactory)
.build();
PoolingHttpClientConnectionManager cm = new
PoolingHttpClientConnectionManager(socketFactoryRegistry);
CloseableHttpClient httpClient = HttpClients.custom()
.setSSLSocketFactory(sslConnectionSocketFactory)
.setConnectionManager(cm)
.build();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(entity);
int statusCode = 0;
try (CloseableHttpResponse httpResponse = httpClient.execute(target, httpPost)) {
statusCode = httpResponse.getStatusLine().getStatusCode();
//parse response here
}
}
解决方案非常简单:
我的一位朋友告诉我,我应该从 url 中删除“www”。
然而,它奏效了!
解决方案非常简单(而且出乎意料):在我从 URL 中删除“www”部分后,它开始起作用了!!!
这是我用 curl 做的:
curl -d "username=<user>&password=<pass>" -X POST https://example.com/wp-json/api/v1/token
这很有效,我收到了令牌。 这是 curl 发送的内容:
POST https://example.com/wp-json/api/v1/token HTTP/1.1
Host: example.com
User-Agent: curl/7.55.1
Accept: */*
Content-Length: <length>
Content-Type: application/x-www-form-urlencoded
username=<user>&password=<pass>
Java 我收到代码 403。 这是 HttpClient 的调试输出:
org.apache.http.wire - http-outgoing-0 >> "POST /wp-json/api/v1/token HTTP/1.1[\r][\n]"
org.apache.http.wire - http-outgoing-0 >> "Content-Length: 45[\r][\n]"
org.apache.http.wire - http-outgoing-0 >> "Content-Type: application/x-www-form-urlencoded; charset=ISO-8859-1[\r][\n]"
org.apache.http.wire - http-outgoing-0 >> "Host: www.example.com[\r][\n]"
org.apache.http.wire - http-outgoing-0 >> "Connection: Keep-Alive[\r][\n]"
org.apache.http.wire - http-outgoing-0 >> "User-Agent: Apache-HttpClient/4.5.13 (Java/1.8.0_121)[\r][\n]"
org.apache.http.wire - http-outgoing-0 >> "Accept-Encoding: gzip,deflate[\r][\n]"
org.apache.http.wire - http-outgoing-0 >> "[\r][\n]"
org.apache.http.wire - http-outgoing-0 >> "username=<user>&password=<pass>"
我在这里看不到 curl 和 java 之间的显着差异。 谁知道我在这里做错了什么?
这是我当前的 java 代码:
public void getToken(URI url, String username, String password) {
HttpEntity entity = new StringEntity("username=" + username + "&password=" + password, ContentType.APPLICATION_FORM_URLENCODED);
HttpHost target = new HttpHost(url.getHost(), 443, "https");
SSLContext sslcontext = SSLContexts.createSystemDefault();
SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(
sslcontext, new String[] { "TLSv1.2", "SSLv3" }, null,
SSLConnectionSocketFactory.getDefaultHostnameVerifier());
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.INSTANCE)
.register("https", sslConnectionSocketFactory)
.build();
PoolingHttpClientConnectionManager cm = new
PoolingHttpClientConnectionManager(socketFactoryRegistry);
CloseableHttpClient httpClient = HttpClients.custom()
.setSSLSocketFactory(sslConnectionSocketFactory)
.setConnectionManager(cm)
.build();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(entity);
int statusCode = 0;
try (CloseableHttpResponse httpResponse = httpClient.execute(target, httpPost)) {
statusCode = httpResponse.getStatusLine().getStatusCode();
//parse response here
}
}
解决方案非常简单: 我的一位朋友告诉我,我应该从 url 中删除“www”。 然而,它奏效了!
解决方案非常简单(而且出乎意料):在我从 URL 中删除“www”部分后,它开始起作用了!!!