首頁  >  文章  >  Java  >  java發送帶有Basic Auth使用 httpclient自帶的認證方式

java發送帶有Basic Auth使用 httpclient自帶的認證方式

php是最好的语言
php是最好的语言原創
2018-08-02 11:17:238132瀏覽

Base64加密方式認證方式下的basic auth。

注意base64的basic auth 使用
httpclient自帶的認證方式如下會認證失敗:

CredentialsProvider provider = new BasicCredentialsProvider();UsernamePasswordCredentials credentials
 = new UsernamePasswordCredentials("username", "user1Pass");provider.setCredentials(AuthScope.ANY, credentials);HttpClient client = HttpClientBuilder.create()  .setDefaultCredentialsProvider(provider)  .build();

成功通過的範例:

package com.biologic.api.service.impl;import java.io.IOException;import java.util.Base64;import java.util.Map;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.client.ClientProtocolException;import org.apache.http.client.methods.HttpPost;import org.apache.http.entity.ContentType;import org.apache.http.entity.mime.MultipartEntityBuilder;import org.apache.http.entity.mime.content.StringBody;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClientBuilder;import org.apache.http.util.EntityUtils;import org.springframework.stereotype.Service;import com.biologic.api.service.HttpService;@Service
public class HttpServiceImpl implements HttpService {


    @Override
    public int httpClientWithBasicAuth(String username, String password, String uri, Map<String, String> paramMap) {
        try { 
            // 创建HttpClientBuilder
            HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
            CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
            HttpPost httpPost = new HttpPost(uri); 
            //添加http头信息 
            httpPost.addHeader("Authorization", "Basic " + Base64.getUrlEncoder().encodeToString((username + ":" + password).getBytes()));

            MultipartEntityBuilder builder = MultipartEntityBuilder.create();



            paramMap.forEach((k,v)->{
                 builder.addPart(k, new StringBody(v, ContentType.MULTIPART_FORM_DATA));
            });
            HttpEntity postEntity = builder.build();
            httpPost.setEntity(postEntity);
            String result = "";
            HttpResponse httpResponse = null;
            HttpEntity entity = null;
            try {
                httpResponse = closeableHttpClient.execute(httpPost);
                entity = httpResponse.getEntity();
                if( entity != null ){
                    result = EntityUtils.toString(entity);
                }
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

            // 关闭连接
            closeableHttpClient.close();

            System.out.println(result);
    }catch (Exception e) {
        System.out.println(e.getStackTrace());
    }
    return 0;
    }



}

相關文章:

Java發送post請求的實例程式碼分享

Java使用原生發送http請求的實例

##相關影片:


全面解析Java註解#

以上是java發送帶有Basic Auth使用 httpclient自帶的認證方式的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn