死亡历险,asp.net做的Webservice,Java做的安卓调用Webservice进行登录验证

来源:互联网 发布:linux grep wc -l 编辑:程序博客网 时间:2024/05/22 14:55

1:asp.net做的Webservice


1.打开VS2010后,文件-->新建-->项目-->其他项目类型-->Visual?Studio?解决方案-->空白解决方案?就起名为:?Test?
2.建立表现层(UI)  对着解决方案右键--添加---新建项目--Visual C#--ASP.NET Web应用程序  随便起个名字Android    确定  (注意不是Visual C#--Web-ASP.NET Web应用程序)
3.建立业务逻辑层(BLL)
对着解决方案右键--添加---新建项目--Visual C#--选择类库  随便起个名字BLL确定   
4.建立数据访问层(DAL)  对着解决方案右键--添加---新建项目--Visual C#--选择类库  随便起个名字DAL 确定   
5.建立Model层(Model)  对着解决方案右键--添加---新建项目--Visual C#--选择类库  随便起个名字Model确定   
6建立各层关系,对着WEB层(刚刚建立的UI层)右键--添加引用--选择BLL--确定 同样建立其它关系   
1) WEB引用 DAL,Model 
2)BLL引用 DAL,Model  
3)DAL引用Model (以及解决错误时 引用的System.Configuration ) 
4)Model无引用


再在刚才Web应用程序Android上右键,添加,新建项Visual C#--Web-Web服务,即可产生一个默认的WebService1.asmx文件

-------------------------------------------------------------------

碰到的问题1:新建项目后解决方案不见了

工具---选项----项目和解决方案----勾选“总是显示解决方案”

碰到的问题2:using不了App_Code里面的类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

using BLL;//要先在WebSite1上添加引用,引用BLL
//为什么using不了App_Code里面的类
//右击类文件,选择属性,有个"bulid action"生成操作,把"content"内容改成"Compile"编译.就ok 了
using skyiv;

碰到的问题3:启动IIS发生意外错误 0x8ffe2740。

打开IIS,在默认网站上按右键,点属性,把TCP端口80改成81即可

------------------------------------------------------------------

上asp.net的WebService1.asmx.cs代码

http://192.168.11.211是本机IP地址

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

using BLL;//要先在WebSite1上添加引用,引用BLL
//为什么using不了App_Code里面的类
//右击类文件,选择属性,有个"bulid action"生成操作,把"content"内容改成"Compile"编译.就ok 了
using skyiv;


namespace Android
{
    /// <summary>
    /// WebService1 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://192.168.11.211:81/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
    // [System.Web.Script.Services.ScriptService]
    public class WebService1 : System.Web.Services.WebService
    {


        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }


        //必须加[WebMethod],不然运行http://localhost:81/netWebService/WebService1.asmx
        //(在IIS里项目上点右键,点浏览)后这个Add方法出不来 
        //如果端口有冲突,那么80改为81
        //打开IIS,在默认网站上按右键,点属性,把TCP端口80改成81即可
        //http://localhost:81/netWebService/WebService1.asmx?wsdl
        [WebMethod]
        public int Add(int a, int b)
        {
            return(a+b);
        }


        [WebMethod]
        public string login(string username, string password)
        {
            string result = "登录成功";
            string validatePwd = UserManager.GetUserPasswordByUserName(username);
            string qm = UserManager.GetnUserIDByUserName(username);
            //加密密码
            //string inputPwd = CryptogramManager.EncryptPassword(this.txtPassword.Text.Trim());
            des d = new des();
            string inputPwd = d.EncryStrHex(password);
            //通过对比密码,验证登录信息是否正确
            if (inputPwd.Trim() == validatePwd.Trim())
            {
                result = "恭喜您:登录成功";
            }
            else
            {
                result = "抱歉:登录失败";
            }
            return result;
        }
    }
}

最后Android上右键,发布,发布方法选择“文件系统”,发布,最后打开IIS,部署,再在部署的WebService1.asmx.上按右键,点浏览,

即可打开http://localhost:81/netWebService/WebService1.asmx

-----------------------------------------------------------------------------------------------------------------------------------

2:Java做的安卓,eclipse,sdk,Android4.3,jdk1.7

碰到2个问题,

1:Android4以后的要放到线程里才可以显示

2:要想取得手机的归属地,必须取得网络访问权限,<uses-permission android:name="android.permission.INTERNET"></uses-permission>

这句放到AndroidManifest.xml的</manifest>上面就可以

package com.ui;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
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.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.StrictMode;
import android.app.Activity;
import android.app.ProgressDialog;
import android.text.TextUtils;
import android.view.Menu;


import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.EditText;


public class MainActivity extends Activity implements OnClickListener{

private Button btn1, btn2;
private EditText et1, et2;
private String username, password;
TextView mMsgRev; 
String mReceivedMsg; 

private ProgressDialog proDialog;
    private Thread thread;
    
    private TextView resultView;
    private static final int NUMBER_FORMAT_ERROR = 0;
    private static final int QUERY_SUCCESS_MSG = 1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //初始化View
        initView();
        
        proDialog = new ProgressDialog(MainActivity.this);
    }
    // 方法:初始化View
     private void initView() {
    btn1 = (Button) MainActivity.this.findViewById(R.id.button1);
    btn2 = (Button) MainActivity.this.findViewById(R.id.button2);    
         
         //按钮绑定点击事件的监听器
    //2016-8-7下面必须这么写implements OnClickListener,否则错误
    //public class MainActivity extends Activity implements OnClickListener{
    btn1.setOnClickListener(this);
    btn2.setOnClickListener(this);
     
    resultView = (TextView) findViewById(R.id.resultView);
     }
    
     //方法:按钮的单击事件
     @Override
     public void onClick(View v) 
     {
         switch (v.getId()) {
         case R.id.button1:
    //连接WEBSERVICE
    if (dialPhone() == true)
    {
             //Toast.makeText(MainActivity.this, "登录仓库管理系统...", Toast.LENGTH_SHORT).show();
    //2、调用带参数的WebMethod 
     
    //Toast.makeText(MainActivity.this, "4", Toast.LENGTH_SHORT).show();
    //getRemoteInfo(username, password); 
     
    thread = new Thread(new QueryThread());
             thread.start();
             //proDialog.onStart();
             //proDialog.show();
    }
         break;
         case R.id.button2:
             Toast.makeText(MainActivity.this, "退出系统", Toast.LENGTH_SHORT).show();
             break;
         default:
             break;
         }            
     }
     
     class QueryThread implements Runnable{
         @Override
         public void run() {
             getRemoteInfo3(username,password);//getRemoteInfo实现的功能是判断手机的归属地,getRemoteInfo2好像没反应
         }
     }
     
     public void getRemoteInfo3(String username, String password) 
     {        
// 命名空间  
        String nameSpace = "http://192.168.11.211:81/";  
        // 调用的方法名称  
        String methodName = "login";  
        // EndPoint  
        String endPoint = "http://192.168.11.211:81/netWebService/WebService1.asmx";  
        // SOAP Action  
        String soapAction = "http://192.168.11.211:81/login"; 


        // 指定WebService的命名空间和调用的方法名  
        SoapObject rpc = new SoapObject(nameSpace, methodName);   
        
        // 设置需调用WebService接口需要传入的两个参数mobileCode、userId  
        rpc.addProperty("username", username);  
        rpc.addProperty("password", password); 
        // 生成调用WebService方法的SOAP请求信息,并指定SOAP的版本  
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);     
        //envelope.bodyOut = rpc;  
        // 设置是否调用的是dotNet开发的WebService  
        envelope.dotNet = true;  
        // 等价于envelope.bodyOut = rpc;  
        envelope.setOutputSoapObject(rpc);        
        HttpTransportSE transport = new HttpTransportSE(endPoint);  
        
        try {            
            // 调用WebService  
            transport.call(soapAction, envelope);                       
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        // 获取返回的数据  
                
        SoapObject object = (SoapObject) envelope.bodyIn;        
        String result = object.getProperty(0).toString();
        //这里写为什么老错误呢?
        //因为这是子线程 要放到主线程才能更新UI
        //resultView.setText(result);
        Message msg = new Message();
        msg.what = QUERY_SUCCESS_MSG;
        msg.obj = filterHtml(result);
        handler.sendMessage(msg);
     }
     
     public void getRemoteInfo2(String username, String password) 
     {
    // TODO Auto-generated method stub
         // 定义待请求的URL
         String requestUrl = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx/getMobileCodeInfo";
         // 创建HttpClient实例
         HttpClient client = new DefaultHttpClient();
         // 根据URL创建HttpPost实例
         HttpPost post = new HttpPost(requestUrl);
         List<NameValuePair> params = new ArrayList<NameValuePair>();
         // 设置需要传递的参数
         params.add(new BasicNameValuePair("mobileCode", "13510104401"));
         params.add(new BasicNameValuePair("userId", ""));
         try {
        resultView.setText("晕了");
             // 设置URL编码
             post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
             // 发送请求并获取反馈
             HttpResponse response = client.execute(post);


             // 判断请求是否成功处理
             if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                 // 解析返回的内容
                 String result = EntityUtils.toString(response.getEntity());
                 // 将查询结果经过解析后显示在TextView中
                 
                 Message msg = new Message();
                 msg.what = QUERY_SUCCESS_MSG;
                 msg.obj = filterHtml(result);
                 handler.sendMessage(msg);
             }
         } catch (Exception e) {
             e.printStackTrace();
         } 
     }
     private String filterHtml(String source) {  
         if(null == source){  
             return "";  
         }  
         return source.replaceAll("</?[^>]+>","").trim();  
     }
     
     public void getRemoteInfo(String username, String password) 
     {          
// 命名空间  
        String nameSpace = "http://WebXml.com.cn/";  
        // 调用的方法名称  
        String methodName = "getMobileCodeInfo";  
        // EndPoint  
        String endPoint = "http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx";  
        // SOAP Action  
        String soapAction = "http://WebXml.com.cn/getMobileCodeInfo"; 
        // 指定WebService的命名空间和调用的方法名  
        SoapObject rpc = new SoapObject(nameSpace, methodName);   
        
        // 设置需调用WebService接口需要传入的两个参数mobileCode、userId  
        //rpc.addProperty("username", username);  
        //rpc.addProperty("password", password); 
        rpc.addProperty("mobileCode", "1351010");  
        rpc.addProperty("userId", "");  
        // 生成调用WebService方法的SOAP请求信息,并指定SOAP的版本  
        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);     
        //envelope.bodyOut = rpc;  
        // 设置是否调用的是dotNet开发的WebService  
        envelope.dotNet = true;  
        // 等价于envelope.bodyOut = rpc;  
        envelope.setOutputSoapObject(rpc);        
        HttpTransportSE transport = new HttpTransportSE(endPoint);  
        
        try {            
            // 调用WebService  
            transport.call(soapAction, envelope);                       
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        // 获取返回的数据  
                
        SoapObject object = (SoapObject) envelope.bodyIn;        
        String result = object.getProperty(0).toString();
        //这里写为什么老错误呢?
        //因为这是子线程 要放到主线程才能更新UI
        //resultView.setText(result);
        Message msg = new Message();
        msg.what = QUERY_SUCCESS_MSG;
        msg.obj = filterHtml(result);
        handler.sendMessage(msg);
     }
     
     Handler handler = new Handler(){


         @Override
         public void handleMessage(Message msg) {
             super.handleMessage(msg);
             switch (msg.what) {
             case NUMBER_FORMAT_ERROR:
                 //phoneSecEditText.setText("");
                 resultView.setText("您输入的号码格式有误");
                 break;
             case QUERY_SUCCESS_MSG:
                 resultView.setText(msg.obj.toString());
                 proDialog.dismiss();
                 break;


             default:
                 break;
             }
         }
         
     };
         
     private boolean dialPhone() 
     { 
boolean boolean1 = true;
et1 = (EditText) MainActivity.this.findViewById(R.id.editText1);
et2 = (EditText) MainActivity.this.findViewById(R.id.editText2); 
username = et1.getText().toString().trim();
password = et2.getText().toString().trim();
 
   if (TextUtils.isEmpty(username)){  
       Toast.makeText(this, "账号不能为空", Toast.LENGTH_LONG).show();  
       boolean1 = false;  
   }
   if (TextUtils.isEmpty(password)){  
       Toast.makeText(this, "密码不能为空", Toast.LENGTH_LONG).show();  
       boolean1 = false; 
   }
return boolean1;
     }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
}



0 0