如何设置HTTP请求头“身份验证”使用 HTTPClient?

发布于 2024-09-28 02:06:40 字数 301 浏览 7 评论 0原文

我想在向服务器发送 POST 请求时设置 HTTP 请求标头“授权”。 我如何在 Java 中做到这一点? HttpClient 有支持吗?

http://www.w3.org/Protocols/HTTP/HTRQ_Headers.html#z9

服务器要求我为授权字段设置一些特定值: 表单 ID:signature 然后他们将使用该签名来验证请求。

谢谢 阿杰

I want to set the HTTP Request header "Authorization" when sending a POST request to a server.
How do I do it in Java? Does HttpClient have any support for it?

http://www.w3.org/Protocols/HTTP/HTRQ_Headers.html#z9

The server requires me to set some specific value for the authorization field:
of the form ID:signature which they will then use to authenticate the request.

Thanks
Ajay

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

辞别 2024-10-05 02:06:44

以下是基本访问身份验证的代码:

HttpPost request = new HttpPost("http://example.com/auth");
request.addHeader("Authorization", "Basic ThisIsJustAnExample");

然后是如何执行它的示例:

HttpParams httpParams = new BasicHttpParams();
HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);
HttpConnectionParams.setConnectionTimeout(httpParams, 3000);
HttpClient httpclient = null;
httpclient = new DefaultHttpClient(httpParams);

HttpResponse response = httpclient.execute(request);

Log.d("Log------------", "Status Code: " + response.getStatusLine().getStatusCode());

Here is the code for a Basic Access Authentication:

HttpPost request = new HttpPost("http://example.com/auth");
request.addHeader("Authorization", "Basic ThisIsJustAnExample");

And then just an example of how to execute it:

HttpParams httpParams = new BasicHttpParams();
HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1);
HttpConnectionParams.setConnectionTimeout(httpParams, 3000);
HttpClient httpclient = null;
httpclient = new DefaultHttpClient(httpParams);

HttpResponse response = httpclient.execute(request);

Log.d("Log------------", "Status Code: " + response.getStatusLine().getStatusCode());
情何以堪。 2024-10-05 02:06:44

这个问题在这里得到“回答”:
使用 HttpClient 在 Java 中进行 Http 基本身份验证?

有很多方法可以做到这。试图寻找答案让我感到沮丧。我发现最好的是 HttpClient 的 Apache 文档。
注意:答案会随着时间的推移而改变,因为所使用的库将具有不推荐使用的方法。
http://hc.apache.org/httpcomponents- client-4.5.x/tutorial/html/authentication.html

This question is "answered" here:
Http Basic Authentication in Java using HttpClient?

There are many ways to do this. It was frustrating for me to try to find the answer. I found that the best was the Apache docs for HttpClient.
Note: answers will change over time as the libraries used will have deprecated methods.
http://hc.apache.org/httpcomponents-client-4.5.x/tutorial/html/authentication.html

岁月苍老的讽刺 2024-10-05 02:06:43

下面是设置请求头的示例

    HttpPost post = new HttpPost("someurl");

    post.addHeader(key1, value1));
    post.addHeader(key2, value2));

Below is the example for setting request headers

    HttpPost post = new HttpPost("someurl");

    post.addHeader(key1, value1));
    post.addHeader(key2, value2));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文