Android sets a certificate (user name and password) for HttpClient)

Source: Internet
Author: User

MainActivity is as follows:

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. defaul THttpClient; import org. apache. http. params. httpConnectionParams; import org. apache. http. protocol. HTTP; import android. OS. bundle; import android. app. activity;/*** Demo Description: * 1 set the Certificate for HttpClient (CredentialsProvider certificate provider) * 2 then obtain the HTML content **/public class MainActivity extends Activity {private final String USERNAME = "your username"; private final String PASSWORD = "your password "; private final String URL = "yo Ur url "; @ Overrideprotected 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. o Ut. 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 (); // when a certificate is required // configure HttpClient as follows: if (HasCredential) {AuthScope authScope = new AuthScope (AuthScope. ANY_HOST, AuthScope. ANY_PORT); UsernamePasswordCredentials usernamePasswordCredentials = new UsernamePasswordCredentials (USERNAME, PASSWORD); CredentialsProvider credentialsProvider = new role (); 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.exe cute (httpPost); if (httpResponse. getStatusLine (). getStatusCode () = H TtpStatus. 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;} // close the client link public static void clientShutdown (HttpClient client) {client. getConnectionManager (). shutdown ();}}


 

Main. xml is as follows:

<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 = "Get HTML content" android: layout_centerInParent = "true"/> </RelativeLayout>


 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.