mono for android代码记录1 WebClient Post数据到Nopcommerce(post数据到MVC5)

WebClient Post数据到Nopcommerce(post数据到MVC5)

注意事项

1.把[ValidateAntiForgeryToken]注释掉,以后再处理CSRF攻击.现在学习先不理

2.post的数据,必须和MVC上面接收的参数字段进行对应,否则post不到函数,就报500错误.

本来想写详细,可是现在不懂写啥了,直接贴代码,不懂就留言问哦.

代码没重构,有空再重构漂亮点.将就看下吧.

using System;

using System.Collections.Specialized;

using System.Net;

using System.Text;

using Android.App;

using Android.OS;

using Android.Widget;

using Nop.DomainPCL.Models.Customer;



namespace Nop.MonoAndroid.Controllers

{

    [Activity(Label = "注册页")]

    public class RegisterActivity : Activity

    {

        protected override void OnCreate(Bundle bundle)

        {

            base.OnCreate(bundle);

            if (Build.VERSION.SdkInt.GetHashCode() >= 11)//处理网络线程的异常

            {

                StrictMode.SetThreadPolicy(

                    new StrictMode.ThreadPolicy.Builder().DetectDiskReads()

                        .DetectDiskWrites()

                        .DetectNetwork()

                        .PenaltyLog()

                        .Build());

                StrictMode.SetVmPolicy(

                    new StrictMode.VmPolicy.Builder().DetectLeakedSqlLiteObjects()

                        .DetectLeakedClosableObjects()

                        .PenaltyLog()

                        .PenaltyDeath()

                        .Build());

            }

            SetContentView(Resource.Layout.RegisterLayout);



            // Create your application here

            var txtEmail = FindViewById<EditText>(Resource.Id.txtEmail);

            var txtPass = FindViewById<EditText>(Resource.Id.txtPassword);

            var txtRePass = FindViewById<EditText>(Resource.Id.txtConfirmPassword);

            var txtFName = FindViewById<EditText>(Resource.Id.txtFirstName);

            var txtLName = FindViewById<EditText>(Resource.Id.txtLastName);



            var btOk = FindViewById<Button>(Resource.Id.btnOK);

            btOk.Click += delegate

            {

                var registInfo = new RegisterModel();

                registInfo.Email = txtEmail.Text;

                registInfo.Password = txtPass.Text;

                registInfo.ConfirmPassword = txtRePass.Text;

                registInfo.FirstName = txtFName.Text;

                registInfo.LastName = txtLName.Text;

//             System.Json.JsonObject postData = new System.Json.JsonObject()

//         { 

//             {"Email",registInfo.Email},{"Password",registInfo.Password},{"ConfirmPassword",registInfo.ConfirmPassword},{"FirstName",registInfo.FirstName},{"LastName",registInfo.LastName}

//            

//         };

//             postData.Add("returnUrl", "");

//             postData.Add("captchaValid", "false");

//             string postStr = postData.ToString();

                const string url = "http://192.168.0.4:8002/register";

                using (var client = new WebClient())

                {

                    try

                    {

                        //PostToMvc1(client, url);

                        PostToMvc2(registInfo, url, client);

                    }

                    catch (WebException)

                    {

                        throw;

                    }

                }

            };

        }



        private static void PostToMvc2(RegisterModel registInfo, string url, WebClient client)

        {

            var form = new NameValueCollection();

            form.Add("Email", registInfo.Email);

            form.Add("Password", registInfo.Password);

            form.Add("ConfirmPassword", registInfo.Password);

            form.Add("FirstName", registInfo.FirstName);

            form.Add("LastName", registInfo.LastName);

            form.Add("LastName", registInfo.LastName);

            form.Add("returnUrl", "");

            form.Add("captchaValid", "false");

            client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");

                //采取POST方式必须加的header,如果改为GET方式的话就去掉这句话即可

            client.Headers.Add("ContentLength", form.ToString().Length.ToString());

            client.Headers.Add("Accept-Language: zh-cn");

            client.Headers.Add("Referer", "http://192.168.0.4:8002/register");

            Byte[] responseData = client.UploadValues(url, "POST", form); //得到返回字符流

            string srcString = Encoding.UTF8.GetString(responseData); //解码

        }



        public void PostToMvc1(WebClient client, string url)

        {

            string postString =

                "FirstName=stupro3&LastName=23fe&DateOfBirthDay=0&DateOfBirthMonth=0&DateOfBirthYear=0&[email protected]&Company=&Newsletter=true&Newsletter=false&Password=123456&ConfirmPassword=123456&register-button=%E6%B3%A8%E5%86%8C&returnUrl=null&captchaValid=false";

                //这里即为传递的参数,可以用工具抓包分析,也可以自己分析,主要是form里面每一个name都要加进来

            byte[] postData = Encoding.UTF8.GetBytes(postString); //编码,尤其是汉字,事先要看下抓取网页的编码方式

            client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");

                //采取POST方式必须加的header,如果改为GET方式的话就去掉这句话即可

            client.Headers.Add("ContentLength", postData.Length.ToString());

            client.Headers.Add("Accept-Language: zh-cn");

            client.Headers.Add("Referer", "http://192.168.0.4:8002/register");

            byte[] responseData = client.UploadData(url, "POST", postData); //得到返回字符流

            string srcString = Encoding.UTF8.GetString(responseData); //解码

        }

    }

}

 

 

你可能感兴趣的:(android)