AbstractHttpMessage getParams().setParameter 构造已弃用~如何现代化?
AbstractHttpMessage getParams().setParameter construct depcrcated ~ How to modernize?
我正在努力使旧的遗留代码现代化,同时 运行 变成一些我不知道如何处理的已弃用的结构。它们看起来都像这样:
HttpPost oauthVerificationRequest = new HttpPost(authURL);
oauthVerificationRequest.getParams().setParameter(OAUTH_TOKEN_KEY, oauthToken);
HttpResponse oauthVerificationRequestResponse = client.getHttpClient().execute(oauthVerificationRequest);
在那里,我的 IDE 抱怨 getParams()
和 setParameter
都被弃用了。
事情是这样写的,我完全明白发生了什么。已弃用的行将请求的键为 OAUTH_TOKEN_KEY
的参数的值设置为 oauthToken
的值,如果它不存在,则可能会创建它。
然而,即使知道这就是这一行中应该发生的事情,我仍然无法找到一种以现代方式编写这一行的方法。我试图弄明白,但是构建 AbstractHttpMessage 的新方法让我很困惑。
由于我通过示例学习得最好,有人可以向我提供上述代码与新逻辑的“t运行slation”吗?
好吧,再次将我的想法写成一个问题似乎有助于让我的思想走上正确的轨道以找到解决方案。因此,为了回答我自己的问题,在新逻辑中编写上述语句的正确方法是:
URIBuilder oauthVerificationRequestBuilder = new URIBuilder(authUrl);
oauthVerificationRequestBuilder.setParameter(OAUTH_TOKEN_KEY, oauthToken);
HttpPost oauthVerificationRequest = new HttpPost(oauthVerificationRequestBuilder.build());
HttpResponse oauthVerificationRequestResponse = client.getHttpClient().execute(oauthVerificationRequest);
基本上,您首先创建一个构建器,然后在构建器中设置参数,然后使用 builder.build()
作为参数创建请求。
奖金问题:
是否还有办法将 addHeader()
修改放入生成器中?因为现在,整个构造对我来说是这样的,使用构建器作为参数感觉有点不一致,然后以“老式”方式在请求之上添加 header:
URIBuilder oauthVerificationRequestBuilder = new URIBuilder(authUrl);
oauthVerificationRequestBuilder.setParameter(OAUTH_TOKEN_KEY, oauthToken);
oauthVerificationRequestBuilder.setParameter(OAUTH_VERIFIER_KEY, oauthVerifier);
oauthVerificationRequestBuilder.setParameter(AUTHORIZE_KEY, VALUE_STRING_TRUE);
HttpPost oauthVerificationRequest = new HttpPost(oauthVerificationRequestBuilder.build());
oauthVerificationRequest.addHeader(CONTENT_TYPE_KEY, CONTENT_TYPE_APPLICATION_X_WWW_FORM_URLENCODED_UTF_8);
谷歌搜索“java httppost”给出 a link to the documentation. Looking for getParams()
in the docs, shows that it is inherited from AbstractHttpMessage
and another google search found the docs for that class。该文档解释了该怎么做而不是使用已弃用的方法:
Deprecated. (4.3) use constructor parameters of configuration API provided by HttpClient
我希望这对未来有所帮助 reader。好的库会记录建议的替代方法是什么,以替代已弃用的方法。查阅这些建议的文档总是一个好主意。
非常简单 http-request 基于 apache http API。
HttpRequest httpRequest = HttpRequestBuilder.create(
ClientBuilder.create().build();
)
.build();
Response response = httpRequest.target(authURL)
.addParameter(OAUTH_TOKEN_KEY, oauthToken)
.addParameter(ANOTHER_PARAM, "VALUE")
.addHeader(ANY_HEADER, "VALUE")
.post();
依赖关系
<dependency>
<groupId>com.jsunsoft.http</groupId>
<artifactId>http-request</artifactId>
<version>2.2.2</version>
</dependency>
我正在努力使旧的遗留代码现代化,同时 运行 变成一些我不知道如何处理的已弃用的结构。它们看起来都像这样:
HttpPost oauthVerificationRequest = new HttpPost(authURL);
oauthVerificationRequest.getParams().setParameter(OAUTH_TOKEN_KEY, oauthToken);
HttpResponse oauthVerificationRequestResponse = client.getHttpClient().execute(oauthVerificationRequest);
在那里,我的 IDE 抱怨 getParams()
和 setParameter
都被弃用了。
事情是这样写的,我完全明白发生了什么。已弃用的行将请求的键为 OAUTH_TOKEN_KEY
的参数的值设置为 oauthToken
的值,如果它不存在,则可能会创建它。
然而,即使知道这就是这一行中应该发生的事情,我仍然无法找到一种以现代方式编写这一行的方法。我试图弄明白,但是构建 AbstractHttpMessage 的新方法让我很困惑。
由于我通过示例学习得最好,有人可以向我提供上述代码与新逻辑的“t运行slation”吗?
好吧,再次将我的想法写成一个问题似乎有助于让我的思想走上正确的轨道以找到解决方案。因此,为了回答我自己的问题,在新逻辑中编写上述语句的正确方法是:
URIBuilder oauthVerificationRequestBuilder = new URIBuilder(authUrl);
oauthVerificationRequestBuilder.setParameter(OAUTH_TOKEN_KEY, oauthToken);
HttpPost oauthVerificationRequest = new HttpPost(oauthVerificationRequestBuilder.build());
HttpResponse oauthVerificationRequestResponse = client.getHttpClient().execute(oauthVerificationRequest);
基本上,您首先创建一个构建器,然后在构建器中设置参数,然后使用 builder.build()
作为参数创建请求。
奖金问题:
是否还有办法将 addHeader()
修改放入生成器中?因为现在,整个构造对我来说是这样的,使用构建器作为参数感觉有点不一致,然后以“老式”方式在请求之上添加 header:
URIBuilder oauthVerificationRequestBuilder = new URIBuilder(authUrl);
oauthVerificationRequestBuilder.setParameter(OAUTH_TOKEN_KEY, oauthToken);
oauthVerificationRequestBuilder.setParameter(OAUTH_VERIFIER_KEY, oauthVerifier);
oauthVerificationRequestBuilder.setParameter(AUTHORIZE_KEY, VALUE_STRING_TRUE);
HttpPost oauthVerificationRequest = new HttpPost(oauthVerificationRequestBuilder.build());
oauthVerificationRequest.addHeader(CONTENT_TYPE_KEY, CONTENT_TYPE_APPLICATION_X_WWW_FORM_URLENCODED_UTF_8);
谷歌搜索“java httppost”给出 a link to the documentation. Looking for getParams()
in the docs, shows that it is inherited from AbstractHttpMessage
and another google search found the docs for that class。该文档解释了该怎么做而不是使用已弃用的方法:
Deprecated. (4.3) use constructor parameters of configuration API provided by HttpClient
我希望这对未来有所帮助 reader。好的库会记录建议的替代方法是什么,以替代已弃用的方法。查阅这些建议的文档总是一个好主意。
非常简单 http-request 基于 apache http API。
HttpRequest httpRequest = HttpRequestBuilder.create(
ClientBuilder.create().build();
)
.build();
Response response = httpRequest.target(authURL)
.addParameter(OAUTH_TOKEN_KEY, oauthToken)
.addParameter(ANOTHER_PARAM, "VALUE")
.addHeader(ANY_HEADER, "VALUE")
.post();
依赖关系
<dependency>
<groupId>com.jsunsoft.http</groupId>
<artifactId>http-request</artifactId>
<version>2.2.2</version>
</dependency>