電子技術論壇_中國專業的電子工程師學習交流社區-中電網技術論壇's Archiver

look_w 發表于 2019-5-18 09:46

java發送帶Basic Auth認證的http post請求實例代碼

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



}

頁: [1]

Powered by eccn 7.0.0  © 2001-2009 Eccn Inc.

成茄子视频人app污下载-茄子视频ios下载安装-茄子视频懂你更多qz8app懂你更多