Reputation: 12034
I'm trying to send a request from Xamarin (webview application). Just now, I'm sending a request from my client with a simple form with a POST method. The request is received by:
bool HandleShouldStartLoad (UIWebView webView, NSUrlRequest request, UIWebViewNavigationType navigationType)
{
var resources = request.Url.ResourceSpecifier.Split ('?');
var method = resources [0];
if(method == "test")
{
//TODO
}
}
Just now, I can read parameters as URL, but for some reason, I can't read the data that I sent with the form with POST Method. How I can get the keys and values in the request?
Upvotes: 2
Views: 3899
Reputation: 12034
I find an answer, the only way that I found was: If you need do a request to a remote server, you can do it using something like:
Your 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>
And here you can use a Post
or Get
Method, but if you need process the data first you can use:
<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="Password" class="input-register" required="required"/>
<br />
<input type="submit" class="btn" value="Register" />
</form>
Your 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);
}
**Your 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 = "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;
}
Upvotes: 1