012_01HttpClient用户登录

  HttpClient 是 Apache Jakarta Common 下的子项目,可以用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建议。

  

使用 HttpClient 需要以下 6 个步骤:

1. 创建 HttpClient 的实例

2. 创建某种连接方法的实例,在这里是 GetMethod。在 GetMethod 的构造函数中传入待连接的地址

3. 调用第一步中创建好的实例的 execute 方法来执行第二步中创建好的 method 实例

4. 读 response

5. 释放连接。无论执行方法是否成功,都必须释放连接

6. 对得到后的内容进行处理

MainActivity.java

  1 package com.example.day12_01login;
  2
  3 import java.io.InputStream;
  4 import java.net.URLEncoder;
  5 import java.util.ArrayList;
  6 import java.util.List;
  7 import org.apache.http.HttpEntity;
  8 import org.apache.http.HttpResponse;
  9 import org.apache.http.NameValuePair;
 10 import org.apache.http.client.HttpClient;
 11 import org.apache.http.client.entity.UrlEncodedFormEntity;
 12 import org.apache.http.client.methods.HttpGet;
 13 import org.apache.http.client.methods.HttpPost;
 14 import org.apache.http.impl.client.DefaultHttpClient;
 15 import org.apache.http.message.BasicNameValuePair;
 16 import com.cskaoyan.webutils.WebUtils;
 17 import android.app.Activity;
 18 import android.os.Bundle;
 19 import android.os.Handler;
 20 import android.os.Message;
 21 import android.view.View;
 22 import android.widget.EditText;
 23 import android.widget.Toast;
 24
 25 public class MainActivity extends Activity {
 26
 27     @Override
 28     protected void onCreate(Bundle savedInstanceState) {
 29         super.onCreate(savedInstanceState);
 30         setContentView(R.layout.activity_main);
 31     }
 32
 33     Handler hanlder = new Handler(){
 34         @Override
 35         public void handleMessage(Message msg) {
 36             // TODO Auto-generated method stub
 37             super.handleMessage(msg);
 38
 39             switch (msg.what) {
 40             case 1:
 41                 Toast.makeText(MainActivity.this, (String)msg.obj, 1).show();
 42                 break;
 43             case 2:
 44                 Toast.makeText(MainActivity.this, (String)msg.obj, 0).show();
 45                 break;
 46             default:
 47                 break;
 48             }
 49         }
 50     };
 51
 52
 53     //http client 的get请求
 54     public void login(View v){
 55         EditText et_username = (EditText) findViewById(R.id.et_uername);
 56         EditText et_password = (EditText) findViewById(R.id.et_password);
 57
 58         final String username= et_username.getText().toString();
 59         final String password= et_password.getText().toString();
 60         //http://localhost/LoginDemo/servlet/Login
 61         //发请求的线程
 62         Thread thread = new Thread(){
 63              public void run() {
 64                 String path = "http://192.168.3.30/LoginDemo/servlet/Login?username="+URLEncoder.encode(username)+"&password="+password;
 65                 HttpClient httpClient =null;
 66                 try {
 67
 68                      httpClient = new DefaultHttpClient();
 69                     HttpGet httpget= new HttpGet(path);
 70
 71                     HttpResponse httpResponse =httpClient.execute(httpget);
 72
 73                     //conn.getResponseCode()==200
 74                     if(httpResponse.getStatusLine().getStatusCode()==200){
 75                         InputStream  is=httpResponse.getEntity().getContent();
 76                         String text = WebUtils.gettextFromInputStream(is, null);
 77                         Message msg = hanlder.obtainMessage();
 78                         msg.what=1;
 79                         msg.obj=text;
 80                         hanlder.sendMessage(msg);
 81                     }
 82                     else {
 83                         Message msg = hanlder.obtainMessage();
 84                         msg.what=2;
 85                         msg.obj="连接服务器失败";
 86                         hanlder.sendMessage(msg);
 87                     }
 88                 } catch (Exception e) {
 89                     // TODO Auto-generated catch block
 90                     e.printStackTrace();
 91                 }
 92                 finally{
 93                     if (httpClient!=null) {
 94                         httpClient.getConnectionManager().shutdown();
 95                     }
 96                 }
 97
 98             };
 99         };
100         thread.start();
101     }
102
103     //http client Post 请求实现
104     public void login2(View v){
105
106         EditText et_username = (EditText) findViewById(R.id.et_uername);
107         EditText et_password = (EditText) findViewById(R.id.et_password);
108
109         final String username= et_username.getText().toString();
110         final String password= et_password.getText().toString();
111
112         //http://localhost/LoginDemo/servlet/Login
113         //发请求的线程
114         Thread thread = new Thread(){
115             public void run() {
116                 String path = "http://192.168.3.39/LoginDemo/servlet/Login";
117                      //data = username=%E7%8E%8B%E9%81%93&password=123456
118                 String data = "username="+URLEncoder.encode(username)+"&password="+password;
119                 HttpClient httpClient = null;
120                 try {
121                     httpClient = new DefaultHttpClient();
122                     HttpPost   httpPost     = new HttpPost(path);
123                     //HttpEntity httpEntity = new StringEntity(s);
124                     List<NameValuePair> parameters = new ArrayList<NameValuePair>();
125
126                     NameValuePair namevp= new BasicNameValuePair("username",username );
127                     parameters.add(namevp);
128                     NameValuePair namevp2= new BasicNameValuePair("password",password );
129                     parameters.add(namevp2);
130                     HttpEntity httpEntity = new  UrlEncodedFormEntity(parameters,"utf-8");
131                     httpPost.setEntity(httpEntity);
132
133                     HttpResponse httpResponse =httpClient.execute(httpPost);
134                     if(httpResponse.getStatusLine().getStatusCode()==200){
135                         InputStream  is= httpResponse.getEntity().getContent();
136                         String text = WebUtils.gettextFromInputStream(is, null);
137                         is.close();
138                         Message msg = hanlder.obtainMessage();
139                         msg.what=1;
140                         msg.obj=text;
141                         hanlder.sendMessage(msg);
142                     }
143                     else {
144                         Message msg = hanlder.obtainMessage();
145                         msg.what=2;
146                         msg.obj="连接服务器失败";
147                         hanlder.sendMessage(msg);
148                     }
149                 } catch (Exception e) {
150                     // TODO Auto-generated catch block
151                     e.printStackTrace();
152                 }
153                 finally{
154                     if (httpClient!=null) {
155                         httpClient.getConnectionManager().shutdown();
156                     }
157                 }
158             };
159         };
160         thread.start();
161     }
162 }

activity_main.xml

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     tools:context="com.example.day12_01login.MainActivity"
 6     android:orientation="vertical" >
 7
 8
 9     <EditText
10         android:id="@+id/et_uername"
11         android:layout_width="fill_parent"
12         android:layout_height="wrap_content"
13          />
14     <EditText
15         android:id="@+id/et_password"
16         android:layout_width="fill_parent"
17         android:layout_height="wrap_content"
18          />
19
20     <RelativeLayout
21          android:layout_width="fill_parent"
22          android:layout_height="wrap_content"
23          >
24         <CheckBox
25          android:id="@+id/cb_remeber"
26          android:layout_width="wrap_content"
27          android:layout_height="wrap_content"
28          android:layout_alignParentLeft="true"
29         />
30         <Button
31         android:id="@+id/bt_login"
32         android:layout_width="wrap_content"
33          android:layout_height="wrap_content"
34          android:layout_alignParentRight="true"
35          android:onClick="login"
36                   android:text="get login"
37         />
38
39     </RelativeLayout>
40
41        <RelativeLayout
42          android:layout_width="fill_parent"
43          android:layout_height="wrap_content"
44          >
45         <CheckBox
46          android:id="@+id/cb_remeber2"
47          android:layout_width="wrap_content"
48          android:layout_height="wrap_content"
49          android:layout_alignParentLeft="true"
50         />
51         <Button
52         android:id="@+id/bt_login2"
53         android:layout_width="wrap_content"
54          android:layout_height="wrap_content"
55          android:layout_alignParentRight="true"
56          android:onClick="login2"
57          android:text="post login"
58         />
59
60     </RelativeLayout>
61
62          <Button
63         android:id="@+id/bt_upload"
64         android:layout_width="wrap_content"
65          android:layout_height="wrap_content"
66          android:layout_alignParentRight="true"
67          android:onClick="upload"
68                   android:text="uploadfile"
69         />
70 </LinearLayout>
时间: 2024-08-06 05:29:07

012_01HttpClient用户登录的相关文章

012_02 asynchttpClient用户登录

android-async-http是专门针对Android在Apache的HttpClient基础上构建的异步的callback-based http client. 所有的请求全在UI线程之外发生,而callback发生在创建它的线程中,应用了Android的Handler发送消息机制.你也可 以把AsyncHttpClient应用在Service中或者后台线程中,库代码会自动识别出它所运行的context. 使用android-async-http只需要三步: 1. 创建一个AsyncHt

linux PAM 用户登录认证

PAM(Pluggable Authentication Modules )是由Sun提出的一种认证机制.它通过提供一些动态链接库和一套统一的API,将系统提供的服务 和该服务的认证方式分开,使得系统管理员可以灵活地根据需要给不同的服务配置不同的认证方式而无需更改服务程序,同时也便于向系 统中添加新的认证手段.从本篇开始会总结一些常用的pam模块及其实现的功能,今天讲的是pam_tally2模块. 一.参数与场景 应用场景:设置Linux用户连续N次输入错误密码进行登陆时,自动锁定X分钟或永久锁

【mfc】用对话框分页实现用户登录

所谓的对话框分页就是点击完一个对话框的按钮,切换到另一个对话框, 这样的对话框多用于一些需要用户登录才能够进行操作的软件, 下面就用对话框分页来实现用户登录系统 一.基本目标 有如下的程序,输入用户名与密码,如果用户名为admin,密码为123456,那么则成功登录,切换到一个有"欢迎登录"与"关闭"按钮的对话框 如果用户名或者密码输入错误则弹出提示, 点击关闭能够关闭这个程序,不弹出用户登录的对话框. 二.制作过程 1.首先如同之前的<[mfc]对于对话框程

Window上python开发--4.Django的用户登录模块User

在搭建网站和web的应用程序时,用户的登录和管理是几乎是每个网站都必备的.今天主要从一个实例了解以下django本身自带的user模块.本文并不对user进行扩展. 主要使用原生的模块. 1.User模块基础: 在使用user 之前先import到自己的iew中.相当与我们自己写好的models.只不过这个是系统提供的models. from django.contrib.auth.models import User # 导入user模块 1.1User对象属性 User 对象属性:usern

C#模拟网站用户登录

我们在写灌水机器人.抓资源机器人和Web网游辅助工具的时候第一步要实现的就是用户登录.那么怎么用C#来模拟一个用户的登录拉?要实现用户的登录,那么首先就必须要了解一般网站中是怎么判断用户是否登录的. HTTP协议是一个无连接的协议,也就是说这次对话的内容和状态与上次的无关,为了实现和用户的持久交互,网站与浏览器之前在刚建立会话时将在服务 器内存中建立一个Session,该Session标识了该用户(浏览器),每一个Session都有一个唯一的ID,第一次建立会话时服务器将生成的这 个ID传给浏览

JavaWeb用户登录功能的实现

大四快毕业了,3年多的时间中,乱七八糟得学了一大堆,想趁找工作之前把所学的东西整理一遍,所以就尝试着做一个完整的JavaWeb系统,这几天试着做了一个用户登录的功能,分享给大家,肯定有很多不完善的地方,希望大家提提宝贵的意见,必将努力完善它. 我贴出此篇博客的目的是,将一些以后有可能用到的重复性的代码保存下来,用于以后需要时直接复制粘贴,所以,此篇博客大部分都是代码,讲解性的语句并不多,如果大家看得头疼,不如不看,以后万一用到的话再拿过来修修改改即可. 有可能用得到的部分:生成验证码的Java类

完成用户登录注册功能

一,需求分析 整体分析框架如下图,需要的包也如下图,它们放在src下 所有的需求如下图所示, 1,我们创建一个名为day12的数据库,其中创建了一个users表 2,在myeclipse中我们新建一个名为day12_user的web项目, 3,在src下我们创建了如下几个java包: com.itheima.damain    实体类包,          其中包括 User类 com.itheima.dao          接口包    ,        其中包括 UserDao接口 com

基于session的用户登录识别

框架express 依赖的session模块express-session 1 主页面app.js var express = require('express'); var path = require('path'); var logger = require('morgan'); var cookieParser = require('cookie-parser'); var bodyParser = require('body-parser'); var routes = require

PPTP-VPN第二章——使用mysql进行用户登录认证

在上一篇文章中记录了pptp vpn的创建过程和简单实用测试,其中用户名和密码均使用文本数据库/etc/ppp/chap-secrets,小规模用户下,尚可使用这种登陆验证方式,如果用户数多了,则需要将用户登录验证方式修改为查询数据库,在本文中将介绍如何将pptp vpn的用户名和密码认证信息存储在mysql数据库中. 前文传送门:http://ylw6006.blog.51cto.com/470441/1794577 一.安装和配置整合mysql-server和freeradius,和前文一样