• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

c# - xamarin,向服务器读取发布请求(带参数)

[复制链接]
菜鸟教程小白 发表于 2022-12-13 02:22:24 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在尝试从 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 方法发送的表单数据。 如何获取请求中的键和值?



Best Answer-推荐答案


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

您的 View.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="[email protected]" class="input-register" required="required"/>
                <input type="password" name="password" placeholder="assword" 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);
}

**你的 RequestProcessor.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 = "OST";
            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;
        }

关于c# - xamarin,向服务器读取发布请求(带参数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26135148/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap