本文整理汇总了Java中org.apache.http.auth.UsernamePasswordCredentials类的典型用法代码示例。如果您正苦于以下问题:Java UsernamePasswordCredentials类的具体用法?Java UsernamePasswordCredentials怎么用?Java UsernamePasswordCredentials使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
UsernamePasswordCredentials类属于org.apache.http.auth包,在下文中一共展示了UsernamePasswordCredentials类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: setDefaultUser
import org.apache.http.auth.UsernamePasswordCredentials; //导入依赖的package包/类
public static void setDefaultUser(String usr,String restServerName) throws ClientProtocolException, IOException {
DefaultHttpClient client = new DefaultHttpClient();
client.getCredentialsProvider().setCredentials(
new AuthScope(host, 8002),
new UsernamePasswordCredentials("admin", "admin"));
String body = "{\"default-user\": \""+usr+"\"}";
HttpPut put = new HttpPut("http://"+host+":8002/manage/v2/servers/"+restServerName+"/properties?server-type=http&group-id=Default");
put.addHeader("Content-type", "application/json");
put.setEntity(new StringEntity(body));
HttpResponse response2 = client.execute(put);
HttpEntity respEntity = response2.getEntity();
if(respEntity != null){
String content = EntityUtils.toString(respEntity);
System.out.println(content);
}
}
示例2: constructHttpClient
import org.apache.http.auth.UsernamePasswordCredentials; //导入依赖的package包/类
protected CloseableHttpClient constructHttpClient() throws IOException {
RequestConfig config = RequestConfig.custom()
.setConnectTimeout(20 * 1000)
.setConnectionRequestTimeout(20 * 1000)
.setSocketTimeout(20 * 1000)
.setMaxRedirects(20)
.build();
URL mmsc = new URL(apn.getMmsc());
CredentialsProvider credsProvider = new BasicCredentialsProvider();
if (apn.hasAuthentication()) {
credsProvider.setCredentials(new AuthScope(mmsc.getHost(), mmsc.getPort() > -1 ? mmsc.getPort() : mmsc.getDefaultPort()),
new UsernamePasswordCredentials(apn.getUsername(), apn.getPassword()));
}
return HttpClients.custom()
.setConnectionReuseStrategy(new NoConnectionReuseStrategyHC4())
.setRedirectStrategy(new LaxRedirectStrategy())
.setUserAgent(TextSecurePreferences.getMmsUserAgent(context, USER_AGENT))
.setConnectionManager(new BasicHttpClientConnectionManager())
.setDefaultRequestConfig(config)
.setDefaultCredentialsProvider(credsProvider)
.build();
}
示例3: MyPageProcessor
import org.apache.http.auth.UsernamePasswordCredentials; //导入依赖的package包/类
public MyPageProcessor(SpiderInfo info, Task task) {
this.site = Site.me().setDomain(info.getDomain()).setTimeOut(info.getTimeout())
.setRetryTimes(info.getRetry()).setSleepTime(info.getSleep())
.setCharset(StringUtils.isBlank(info.getCharset()) ? null : info.getCharset())
.setUserAgent(info.getUserAgent());
//设置抓取代理IP与接口
if (StringUtils.isNotBlank(info.getProxyHost()) && info.getProxyPort() > 0) {
this.site.setHttpProxy(new HttpHost(info.getProxyHost(), info.getProxyPort()));
//设置代理的认证
if (StringUtils.isNotBlank(info.getProxyUsername()) && StringUtils.isNotBlank(info.getProxyPassword())) {
this.site.setUsernamePasswordCredentials(new UsernamePasswordCredentials(info.getProxyUsername(), info.getProxyPassword()));
}
}
this.info = info;
this.task = task;
}
示例4: setAuth
import org.apache.http.auth.UsernamePasswordCredentials; //导入依赖的package包/类
@Override
public void setAuth(SensorThingsService service) {
try {
CredentialsProvider credsProvider = new BasicCredentialsProvider();
URL url = service.getEndpoint().toURL();
credsProvider.setCredentials(
new AuthScope(url.getHost(), url.getPort()),
new UsernamePasswordCredentials(editorUsername.getValue(), editorPassword.getValue()));
CloseableHttpClient httpclient = HttpClients.custom()
.setDefaultCredentialsProvider(credsProvider)
.build();
service.setClient(httpclient);
} catch (MalformedURLException ex) {
LOGGER.error("Failed to initialise basic auth.", ex);
}
}
示例5: MyAnimeListQuery
import org.apache.http.auth.UsernamePasswordCredentials; //导入依赖的package包/类
public MyAnimeListQuery(Charrizard charrizard) {
status = MyAnimeListStatus.UNKNOWN_ERROR;
errorDescription = "Unknown error.";
try {
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("https://myanimelist.net/api/account/verify_credentials.xml");
httpGet.addHeader(BasicScheme.authenticate(
new UsernamePasswordCredentials(charrizard.getSettings().getMyAnimeList().getUsername(), charrizard.getSettings().getMyAnimeList().getUsername()),
"UTF-8", false));
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity responseEntity = httpResponse.getEntity();
if (httpResponse.getStatusLine().getStatusCode() >= 400) {
status = MyAnimeListStatus.AUTH_ERROR;
errorDescription = "Authorization Error: " + httpResponse.getStatusLine().getReasonPhrase();
return;
}
} catch (IOException e) {
status = MyAnimeListStatus.REQUEST_ERROR;
errorDescription = "Can't connect to MyAnimeList: " + e.getMessage();
e.printStackTrace();
}
}
示例6: generateClient
import org.apache.http.auth.UsernamePasswordCredentials; //导入依赖的package包/类
@Override
public CloseableHttpAsyncClient generateClient ()
{
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope (AuthScope.ANY),
new UsernamePasswordCredentials(serviceUser, servicePass));
RequestConfig rqconf = RequestConfig.custom()
.setCookieSpec(CookieSpecs.DEFAULT)
.setSocketTimeout(Timeouts.SOCKET_TIMEOUT)
.setConnectTimeout(Timeouts.CONNECTION_TIMEOUT)
.setConnectionRequestTimeout(Timeouts.CONNECTION_REQUEST_TIMEOUT)
.build();
CloseableHttpAsyncClient res = HttpAsyncClients.custom ()
.setDefaultCredentialsProvider (credsProvider)
.setDefaultRequestConfig(rqconf)
.build ();
res.start ();
return res;
}
示例7: generateClient
import org.apache.http.auth.UsernamePasswordCredentials; //导入依赖的package包/类
@Override
public CloseableHttpAsyncClient generateClient ()
{
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope (AuthScope.ANY),
new UsernamePasswordCredentials(username, password));
RequestConfig rqconf = RequestConfig.custom()
.setCookieSpec(CookieSpecs.DEFAULT)
.setSocketTimeout(Timeouts.SOCKET_TIMEOUT)
.setConnectTimeout(Timeouts.CONNECTION_TIMEOUT)
.setConnectionRequestTimeout(Timeouts.CONNECTION_REQUEST_TIMEOUT)
.build();
CloseableHttpAsyncClient res = HttpAsyncClients.custom ()
.setDefaultCredentialsProvider (credsProvider)
.setDefaultRequestConfig(rqconf)
.build ();
res.start ();
return res;
}
示例8: ElasticBulkSender
import org.apache.http.auth.UsernamePasswordCredentials; //导入依赖的package包/类
public ElasticBulkSender(String user, String password, HttpHost... hosts) {
this.user = user;
this.password = password;
this.hosts = hosts;
this.restClient = RestClient.builder(hosts)
.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
@Override
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
if (!Strings.isBlank(user)) {
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(user, password));
return httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
} else {
return httpClientBuilder;
}
}
})
.build();
}
示例9: associateRESTServerWithDefaultUser
import org.apache.http.auth.UsernamePasswordCredentials; //导入依赖的package包/类
public static void associateRESTServerWithDefaultUser(String restServerName,String userName,String authType)throws Exception{
DefaultHttpClient client = new DefaultHttpClient();
client.getCredentialsProvider().setCredentials(
new AuthScope(host, 8002),
new UsernamePasswordCredentials("admin", "admin"));
String body = "{ \"default-user\":\""+userName+"\",\"authentication\": \""+authType+"\",\"group-name\": \"Default\"}";
HttpPut put = new HttpPut("http://"+host+":8002/manage/v2/servers/"+restServerName+"/properties?server-type=http");
put.addHeader("Content-type", "application/json");
put.setEntity(new StringEntity(body));
HttpResponse response2 = client.execute(put);
HttpEntity respEntity = response2.getEntity();
if(respEntity != null){
String content = EntityUtils.toString(respEntity);
System.out.println(content);
}
}
示例10: deleteRESTUser
import org.apache.http.auth.UsernamePasswordCredentials; //导入依赖的package包/类
public static void deleteRESTUser(String usrName){
try{
DefaultHttpClient client = new DefaultHttpClient();
client.getCredentialsProvider().setCredentials(
new AuthScope(host, 8002),
new UsernamePasswordCredentials("admin", "admin"));
HttpDelete delete = new HttpDelete("http://"+host+":8002/manage/v2/users/"+usrName);
HttpResponse response = client.execute(delete);
if(response.getStatusLine().getStatusCode()== 202){
Thread.sleep(3500);
}
}catch (Exception e) {
// writing error to Log
e.printStackTrace();
}
}
示例11: deleteUserRole
import org.apache.http.auth.UsernamePasswordCredentials; //导入依赖的package包/类
public static void deleteUserRole(String roleName){
try{
DefaultHttpClient client = new DefaultHttpClient();
client.getCredentialsProvider().setCredentials(
new AuthScope(host, 8002),
new UsernamePasswordCredentials("admin", "admin"));
HttpDelete delete = new HttpDelete("http://"+host+":8002/manage/v2/roles/"+roleName);
HttpResponse response = client.execute(delete);
if(response.getStatusLine().getStatusCode()== 202){
Thread.sleep(3500);
}
}catch (Exception e) {
// writing error to Log
e.printStackTrace();
}
}
示例12: deleteRESTServerWithDB
import org.apache.http.auth.UsernamePasswordCredentials; //导入依赖的package包/类
public static void deleteRESTServerWithDB(String restServerName) {
try{
DefaultHttpClient client = new DefaultHttpClient();
client.getCredentialsProvider().setCredentials(
new AuthScope(host, 8002),
new UsernamePasswordCredentials("admin", "admin"));
HttpDelete delete = new HttpDelete("http://"+host+":8002/v1/rest-apis/"+restServerName+"?include=content&include=modules");
HttpResponse response = client.execute(delete);
if(response.getStatusLine().getStatusCode()== 202){
Thread.sleep(9500);
}
}catch (Exception e) {
// writing error to Log
e.printStackTrace();
}
}
示例13: deleteRESTServer
import org.apache.http.auth.UsernamePasswordCredentials; //导入依赖的package包/类
public static void deleteRESTServer(String restServerName) {
try{
DefaultHttpClient client = new DefaultHttpClient();
client.getCredentialsProvider().setCredentials(
new AuthScope(host, 8002),
new UsernamePasswordCredentials("admin", "admin"));
HttpDelete delete = new HttpDelete("http://"+host+":8002/v1/rest-apis/"+restServerName+"&include=modules");
HttpResponse response = client.execute(delete);
if(response.getStatusLine().getStatusCode()== 202){
Thread.sleep(3500);
waitForServerRestart();
}
else System.out.println("Server response "+response.getStatusLine().getStatusCode());
}catch (Exception e) {
// writing error to Log
System.out.println("Inside Deleting Rest server is throwing an error");
e.printStackTrace();
}
}
示例14: loadBug18993
import org.apache.http.auth.UsernamePasswordCredentials; //导入依赖的package包/类
public static void loadBug18993(){
try{
DefaultHttpClient client = new DefaultHttpClient();
client.getCredentialsProvider().setCredentials(
new AuthScope(host, 8011),
new UsernamePasswordCredentials("admin", "admin"));
String document ="<foo>a space b</foo>";
String perm = "perm:rest-writer=read&perm:rest-writer=insert&perm:rest-writer=update&perm:rest-writer=execute";
HttpPut put = new HttpPut("http://"+host+":8011/v1/documents?uri=/a%20b&"+perm);
put.addHeader("Content-type", "application/xml");
put.setEntity(new StringEntity(document));
HttpResponse response = client.execute(put);
HttpEntity respEntity = response.getEntity();
if(respEntity != null){
String content = EntityUtils.toString(respEntity);
System.out.println(content);
}
}catch (Exception e) {
// writing error to Log
e.printStackTrace();
}
}
示例15: setAuthentication
import org.apache.http.auth.UsernamePasswordCredentials; //导入依赖的package包/类
public static void setAuthentication(String level,String restServerName) throws ClientProtocolException, IOException
{
DefaultHttpClient client = new DefaultHttpClient();
client.getCredentialsProvider().setCredentials(
new AuthScope(host, 8002),
new UsernamePasswordCredentials("admin", "admin"));
String body = "{\"authentication\": \""+level+"\"}";
HttpPut put = new HttpPut("http://"+host+":8002/manage/v2/servers/"+restServerName+"/properties?server-type=http&group-id=Default");
put.addHeader("Content-type", "application/json");
put.setEntity(new StringEntity(body));
HttpResponse response2 = client.execute(put);
HttpEntity respEntity = response2.getEntity();
if(respEntity != null){
String content = EntityUtils.toString(respEntity);
System.out.println(content);
}
}