您好,登錄后才能下訂單哦!
雖然摘要認證的安全性比BASIC認證提高了不少,但是從接口調用上來看,并不比BASIC認證復雜,而且Realm和Scheme參數都可以為空,這時候就和BASIC認證的調用方式一模一樣了。
import java.net.URI; import org.apache.http.auth.AuthScope; import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.client.CredentialsProvider; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.client.methods.RequestBuilder; import org.apache.http.impl.client.BasicCredentialsProvider; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; public class DigestTest { public static void main(String[] args) throws Exception { CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope("10.0.0.90", 8080, "favourite digest realm", AuthScope.ANY_SCHEME), new UsernamePasswordCredentials("admin", "password")); CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build(); HttpUriRequest login = RequestBuilder.post() .setUri(new URI("http://10.0.0.100:8080/hibaby/getBabiesByGender")) .addParameter("appKey", "BEST") .addParameter("gender", "boy").build(); CloseableHttpResponse response = httpclient.execute(login); System.out.println("響應狀態:" + response.getStatusLine()); String result = EntityUtils.toString(response.getEntity(), "UTF-8"); System.out.println("Result: " + result); } }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。