代码改变世界

Android与IIS身份验证——基本验证

2011-08-19 09:58  Kevin-wang  阅读(387)  评论(0编辑  收藏  举报

摘自:http://www.cnblogs.com/GoodHelper/archive/2011/08/17/android_iis_01.html

一、ASP.NET服务器端

首先,我们使用VS2010创建一个web项目(可以是WebForms,也可以是MVC,我这里使用的是ASP.NET MVC项目)。图1.1所示

image_thumb

图1.1

然后,在HomeController的Index Action中输入:登陆成功。

[HandleError]
public class HomeController : Controller
{
   public ActionResult Index()
   {
        return Content("登陆成功");
   }
}

二、Android客户端

首先,创建一个Android项目,并新建一个MainActivity类。

接着,编写一个访问IIS服务器的类。

package ld.com.authorize;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.DefaultHttpClient;
import android.util.Log;
public abstract class HttpHelper {
private final static String TAG = "HttpHelper";
public static String invoke() {
        String result = null;
try {
final String url = "http://192.168.1.104:180/";
            HttpPost httpPost = new HttpPost(url);
            DefaultHttpClient httpClient = new DefaultHttpClient();
//基本身份验证
            BasicCredentialsProvider bcp = new BasicCredentialsProvider();
            String userName = "liudong";
            String password = "123";
            bcp.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(
                    userName, password));
            httpClient.setCredentialsProvider(bcp);
            HttpResponse httpResponse = httpClient.execute(httpPost);
            StringBuilder builder = new StringBuilder();
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    httpResponse.getEntity().getContent()));
for (String s = reader.readLine(); s != null; s = reader.readLine()) {
                builder.append(s);
            }
            result = builder.toString();
            Log.d(TAG, "result is ( " + result + " )");
        } catch (Exception e) {
            Log.e(TAG, e.toString());
        }
        Log.d(TAG, "over");
return result;
    }
}

注意的是,我这里用户名和密码分别是:liudong和123。

然后,修改layout文件:main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:text="@string/hello" />
<Button android:text="身份码验证" android:id="@+id/btnPassword"
        android:layout_width="fill_parent" android:layout_height="wrap_content"></Button>
</LinearLayout>

最后,修改MainActivity。

package ld.com.authorize;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
private final String TAG = this.getClass().getSimpleName();
private Button btnPassword;
/** Called when the activity is first created. */
    @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btnPassword = (Button) this.findViewById(R.id.btnPassword);
        setInvokeOnClick();
    }
private void setInvokeOnClick() {
        btnPassword.setOnClickListener(new OnClickListener() {
            @Override
public void onClick(View v) {
// TODO Auto-generated method stub
                AsyncTask<Void, Void, String> task = new AsyncTask<Void, Void, String>() {
private ProgressDialog progressDialog;
                    @Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
// super.onPostExecute(result);
                        progressDialog.cancel();
                        Toast.makeText(MainActivity.this, result,
                                Toast.LENGTH_SHORT).show();
                        Log.d(TAG, result);
                    }
                    @Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
                        progressDialog = new ProgressDialog(MainActivity.this);
                        progressDialog
                                .setProgressStyle(ProgressDialog.STYLE_SPINNER);
                        progressDialog.setTitle("调用中,请稍后...");
                        progressDialog.show();
                    }
                    @Override
protected String doInBackground(Void... arg0) {
// TODO Auto-generated method stub
try {
return HttpHelper.invoke();
                        } catch (Exception e) {
return null;
                        }
                    }
                };
                task.execute();
            }
        });
    }
}

设置访问权限为:

<uses-permission android:name="android.permission.INTERNET" />

运行模拟器的效果如图2.1所示。

image_thumb17

图2.1

三、IIS部署

首先、添加一个网站,如图3.1所示。

image_thumb6

如图3.1

最后,我们在浏览器中输入网址进行验证,验证结果见图3.5和图3.6。

image_thumb8

图3.5

image_thumb10

图3.6

从图中我们可以发现,浏览该网页时需要用户名和密码。我们输入了正确的用户名和密码后就可以登录这个页面了。

四,运行效果。

见图4.1和图4.2所示。

image_thumb12

图4.1

image_thumb14

图4.2

代码下载