Android为HttpClient设置证书(用户名和密码)

简介: MainActivity如下: package cc.testhtmlcontent;import java.io.BufferedReader;import java.

MainActivity如下:

package cc.testhtmlcontent;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.protocol.HTTP;
import android.os.Bundle;
import android.app.Activity;
/**
 * Demo描述:
 * 1 为HttpClient设置证书(CredentialsProvider 证书提供者)
 * 2 然后获取HTML中的内容
 * 
 */
public class MainActivity extends Activity {
	private final String USERNAME="your username";
	private final String PASSWORD="your password";
    private final String URL="your url";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		getHTMLContent();
	}
	
	private void getHTMLContent() {
		try {
			new Thread(new Runnable() {
				public void run() {
					ArrayList<NameValuePair> nameValuePairArrayList = new ArrayList<NameValuePair>();
					String HTMLContent = getHTMLContentByPost(nameValuePairArrayList, URL,false);
					System.out.println("HTMLContent=" + HTMLContent);
				}
			}).start();
		} catch (Exception e) {
		}

	}

	public  String getHTMLContentByPost(ArrayList<NameValuePair> nameValuePairArrayList,String url, boolean hasCredential) {
		InputStream inputStream = null;
		String line = "";
		String htmlContentString = "";
		HttpPost httpPost = new HttpPost(url);
		DefaultHttpClient httpClient = new DefaultHttpClient();
		//当需要证书的时候
		//对HttpClient进行如下设置:
		if (hasCredential) {
			AuthScope authScope=new AuthScope(AuthScope.ANY_HOST,AuthScope.ANY_PORT);
			UsernamePasswordCredentials usernamePasswordCredentials=new UsernamePasswordCredentials(USERNAME, PASSWORD);
			CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
			credentialsProvider.setCredentials(authScope, usernamePasswordCredentials);
			httpClient.setCredentialsProvider(credentialsProvider);
		}
	
		try {
			HttpConnectionParams.setConnectionTimeout(httpClient.getParams(), 8000);
			HttpConnectionParams.setSoTimeout(httpClient.getParams(), 9000);
			HttpConnectionParams.setTcpNoDelay(httpClient.getParams(), true);
			httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairArrayList, HTTP.UTF_8));
			HttpResponse httpResponse = httpClient.execute(httpPost);
			if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				HttpEntity httpEntity = httpResponse.getEntity();
				inputStream = httpEntity.getContent();
				InputStreamReader inputStreamReader=new InputStreamReader(inputStream, HTTP.UTF_8);
				BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
				while ((line = bufferedReader.readLine()) != null) {
					htmlContentString += line;
				}
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		clientShutdown(httpClient);
		return htmlContentString;
	}

	//关闭client链接
	public static void clientShutdown(HttpClient client) {
		client.getConnectionManager().shutdown();
	}

}


 

main.xml如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
     >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="获取HTML的内容"
        android:layout_centerInParent="true" 
    />

</RelativeLayout>


 

相关文章
|
2月前
|
监控 安全 Android开发
【新手必读】Airtest测试Android手机常见的设置问题
【新手必读】Airtest测试Android手机常见的设置问题
|
6月前
|
数据库连接 数据库 Android开发
Android -- Room简化数据库设置图书案例
Android -- Room简化数据库设置图书案例
22 0
|
4天前
|
存储 Java Android开发
Android系统 设置第三方应用为默认Launcher实现和原理分析
Android系统 设置第三方应用为默认Launcher实现和原理分析
18 0
|
3月前
|
网络安全 Android开发
2023安卓逆向 -- 抓包环境设置(Charles+Postern)
2023安卓逆向 -- 抓包环境设置(Charles+Postern)
33 0
|
4月前
|
XML 数据库 数据安全/隐私保护
Android App规范处理中版本设置、发布模式、给数据集SQLite加密的讲解及使用(附源码 超详细必看)
Android App规范处理中版本设置、发布模式、给数据集SQLite加密的讲解及使用(附源码 超详细必看)
43 0
|
4月前
|
XML Java Android开发
Android Studio App开发之通知渠道NotificationChannel及给华为、小米手机桌面应用添加消息数量角标实战(包括消息重要级别的设置 附源码)
Android Studio App开发之通知渠道NotificationChannel及给华为、小米手机桌面应用添加消息数量角标实战(包括消息重要级别的设置 附源码)
90 0
|
4月前
|
XML Java Android开发
Android Studio中视图基础之设置视图的宽高、间距、对齐方式的讲解与实战(附源码 超详细必看)
Android Studio中视图基础之设置视图的宽高、间距、对齐方式的讲解与实战(附源码 超详细必看)
74 0
|
6月前
|
数据安全/隐私保护 Android开发 iOS开发
解决第三方邮箱APP登陆QQ、163邮箱无法验证账户名或密码的问题(IOS、MacOS、Windows、Android)
解决第三方邮箱APP登陆QQ、163邮箱无法验证账户名或密码的问题(IOS、MacOS、Windows、Android)
113 0