xamarin,读取post请求(带参数)到服务器

本文关键字:参数 服务器 读取 post 请求 xamarin | 更新日期: 2023-09-27 18:25:06

我正试图从Xamarin(webview应用程序)发送一个请求。刚才,我用一个带有POST方法的简单表单从客户机发送了一个请求。请求接收方式:

bool HandleShouldStartLoad (UIWebView webView, NSUrlRequest request, UIWebViewNavigationType navigationType)
{
            var resources = request.Url.ResourceSpecifier.Split ('?');
            var method = resources [0];
            if(method == "test")
            {
                        //TODO
            }
}

刚才,我可以读取URL形式的参数,但由于某种原因,我无法读取使用POST方法随表单发送的数据。如何获取请求中的键和值?

xamarin,读取post请求(带参数)到服务器

我找到了答案,我找到的唯一方法是:如果你需要向远程服务器发出请求,你可以使用以下方法来完成:

您的视图.cshtml

<form action="http://myaddress:3000/upload" enctype="multipart/form-data" method="post">
<input type="text" name="email" />
<input type="text" name="email" />
<input type="submit" />
</form>

这里你可以使用PostGet方法,但如果你需要先处理数据,你可以使用:

<form action="hybrid:RegisterUser" enctype="multipart/form-data" method="GET">
                <input type="email" name="email"  required="required" placeholder="myemail@mydomain.com" class="input-register" required="required"/>
                <input type="password" name="password" placeholder="Password" class="input-register" required="required"/>
                <br />
                <input type="submit" class="btn" value="Register" />
            </form>

您的HybridViewController.cs

if (method == "RegisterUser") 
{
                RequestProcessor rp = new RequestProcessor();
                var parameters = System.Web.HttpUtility.ParseQueryString (resources [1]);
                Dictionary<string,string> requestParams = new Dictionary<string,string> ();
                string email = parameters["email"];
                requestParams.Add ("email", email )
                rp.HttpPostRequest("users/add", requestParams);
                rp.HttpGetRequest("users/add?email" + email);
}

**您的请求处理器.cs**

public string HttpPostRequest(string url, Dictionary<string,string> postParameters)
        {
            url = "http://Mydomain:3000/" + url;
            string postData = "";
            foreach (string key in postParameters.Keys)
            {
                postData += HttpUtility.UrlEncode(key) + "="
                    + HttpUtility.UrlEncode(postParameters[key]) + "&";
            }
            HttpWebRequest myHttpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
            myHttpWebRequest.Method = "POST";
            byte[] data = System.Text.Encoding.ASCII.GetBytes(postData);
            myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
            myHttpWebRequest.ContentLength = data.Length;
            Stream requestStream = myHttpWebRequest.GetRequestStream();
            requestStream.Write(data, 0, data.Length);
            requestStream.Close();
            HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
            Stream responseStream = myHttpWebResponse.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(responseStream, System.Text.Encoding.Default);
            string pageContent = myStreamReader.ReadToEnd();
            myStreamReader.Close();
            responseStream.Close();
            myHttpWebResponse.Close();
            return pageContent;
        }
        public string HttpGetRequest(string Url)
        {
            string Out = String.Empty;
            Url = "http://Mydomain:3000/" + Url;
            System.Net.WebRequest req = System.Net.WebRequest.Create(Url);
            try
            {
                System.Net.WebResponse resp = req.GetResponse();
                using (System.IO.Stream stream = resp.GetResponseStream())
                {
                    using (System.IO.StreamReader sr = new System.IO.StreamReader(stream))
                    {
                        Out = sr.ReadToEnd();
                        sr.Close();
                    }
                }
            }
            catch (ArgumentException ex)
            {
                Out = string.Format("HTTP_ERROR :: The second HttpWebRequest object has raised an Argument Exception as 'Connection' Property is set to 'Close' :: {0}", ex.Message);
            }
            catch (WebException ex)
            {
                Out = string.Format("HTTP_ERROR :: WebException raised! :: {0}", ex.Message);
            }
            catch (Exception ex)
            {
                Out = string.Format("HTTP_ERROR :: Exception raised! :: {0}", ex.Message);
            }
            return Out;
        }