我正在尝试从 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 方法发送的表单数据。 如何获取请求中的键和值?
我找到了答案,我找到的唯一方法是: 如果您需要向远程服务器发出请求,您可以使用以下方法:
您的 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>
这里你可以使用 Post
或 Get
方法,但如果你需要先处理数据,你可以使用:
<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/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |