How to configure HTTP authentication of HTTP header using HTTPClient?

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

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

The server requires me to set a specific value for the authorization field: form identifier: signature, which they will then use to authenticate the request.

Thanks Ajay

+3
source share
3 answers

The following is an example of setting request headers

    HttpPost post = new HttpPost("someurl");

    post.addHeader(key1, value1));
    post.addHeader(key2, value2));
+3
source

:

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());
+2

Source: https://habr.com/ru/post/1770644/


All Articles