
该 HttpClient的 不能把从URI登录creditals。
您必须给他们指定的方法。
如果您使用 HttpClient 4.x,请查看以下内容:http :
//hc.apache.org/httpcomponents-client-
ga/tutorial/html/authentication.html
- 但是请注意,如果您不想在 HttpClient
- 上使用新版本(Android使用版本3.x),则应在此处查看:http
- //hc.apache.org/httpclient-3.x/authentication.html
这就是理论,现在我们使用它们:
基本上,我们使用 HTTP ,但是如果要使用 HTTPS ,则必须将以下分配编辑
new HttpHost("www.google.com",80, "http")为new HttpHost("www.google.com", 443, "https")。此外,您还必须针对您的问题编辑主机( www.google.com )。
注意: 仅需要完整的合格域名(FQDN),而不需要完整的URI。
HttpClient 3.x:
package com.test;import org.apache.http.HttpEntity;import org.apache.http.HttpHost;import org.apache.http.HttpResponse;import org.apache.http.auth.AuthScope;import org.apache.http.auth.UsernamePasswordCredentials;import org.apache.http.client.methods.HttpGet;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.util.EntityUtils;import android.app.Activity;import android.os.Bundle;public class Test2aActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); try { HttpHost targetHost = new HttpHost("www.google.com", 80, "http"); DefaultHttpClient httpclient = new DefaultHttpClient(); try { // Store the user login httpclient.getCredentialsProvider().setCredentials( new AuthScope(targetHost.getHostName(), targetHost.getPort()), new UsernamePasswordCredentials("user", "password")); // Create request // You can also use the full URI http://www.google.com/ HttpGet httpget = new HttpGet("/"); // Execute request HttpResponse response = httpclient.execute(targetHost, httpget); HttpEntity entity = response.getEntity(); System.out.println(EntityUtils.toString(entity)); } finally { httpclient.getConnectionManager().shutdown(); } } catch (Exception e) { e.printStackTrace(); } }}HttpClient 4.x:
注意: 您将需要来自Apache 的新 HttpClient ,并且还必须重新排列顺序,即jar文件位于Android库之前。
package com.test;import org.apache.http.HttpEntity;import org.apache.http.HttpHost;import org.apache.http.HttpResponse;import org.apache.http.auth.AuthScope;import org.apache.http.auth.UsernamePasswordCredentials;import org.apache.http.client.AuthCache;import org.apache.http.client.methods.HttpGet;import org.apache.http.client.protocol.ClientContext;import org.apache.http.impl.auth.BasicScheme;import org.apache.http.impl.client.BasicAuthCache;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.protocol.BasicHttpContext;import org.apache.http.util.EntityUtils;import android.app.Activity;import android.os.Bundle;public class TestActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); try { HttpHost targetHost = new HttpHost("www.google.com", 80, "http"); DefaultHttpClient httpclient = new DefaultHttpClient(); try { // Store the user login httpclient.getCredentialsProvider().setCredentials( new AuthScope(targetHost.getHostName(), targetHost.getPort()), new UsernamePasswordCredentials("user", "password")); // Create AuthCache instance AuthCache authCache = new BasicAuthCache(); // Generate BASIC scheme object and add it to the local // auth cache BasicScheme basicAuth = new BasicScheme(); authCache.put(targetHost, basicAuth); // Add AuthCache to the execution context BasicHttpContext localcontext = new BasicHttpContext(); localcontext.setAttribute(ClientContext.AUTH_CACHE, authCache); // Create request // You can also use the full URI http://www.google.com/ HttpGet httpget = new HttpGet("/"); // Execute request HttpResponse response = httpclient.execute(targetHost, httpget, localcontext); HttpEntity entity = response.getEntity(); System.out.println(EntityUtils.toString(entity)); } finally { httpclient.getConnectionManager().shutdown(); } } catch (Exception e) { e.printStackTrace(); } }}欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)