Rumah > Java > javaTutorial > teks badan

java发送带Basic Auth使用 httpclient自带的认证方式

php是最好的语言
Lepaskan: 2018-08-02 11:17:23
asal
8126 orang telah melayarinya

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();
Salin selepas log masuk

成功通过的例子:

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;
    }



}
Salin selepas log masuk

相关文章:

Java发送post请求的实例代码分享

Java使用原生发送http请求的实例

相关视频:

全面解析Java注解

Atas ialah kandungan terperinci java发送带Basic Auth使用 httpclient自带的认证方式. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan