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

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>


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

谷哥的小弟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值