C# 如何使用Xamarin从iOS向UIWebView提交表单字段?
我目前有一个带有密码登录表单的简单网站:C# 如何使用Xamarin从iOS向UIWebView提交表单字段?,c#,ios,xamarin,C#,Ios,Xamarin,我目前有一个带有密码登录表单的简单网站: <input type="text" id="passwd" name="passwd" /> 我发现有人可以使用obj-c实现这一点,但我不确定如何使用c#和Xamarin实现这一点。请帮忙 谢谢 这应该可以做到: string password = "1234"; var req = new NSMutableUrlRequest (new NSUrl ("http://example.com")); req.HttpMethod =
<input type="text" id="passwd" name="passwd" />
我发现有人可以使用obj-c实现这一点,但我不确定如何使用c#和Xamarin实现这一点。请帮忙
谢谢 这应该可以做到:
string password = "1234";
var req = new NSMutableUrlRequest (new NSUrl ("http://example.com"));
req.HttpMethod = "POST";
req.Body = NSData.FromString(String.Format("passwd={0}", password));
//req["Content-Length"] = req.Body.Length.ToString();
//req["Content-Type"] = "application/x-www-form-urlencoded charset=utf-8";
myWebView.LoadRequest (req);
这适用于
UIWebview
(从2020年4月起已弃用
var request = new NSMutableUrlRequest(new NSUrl(new
NSString("Url - here")));
request.Body = "Data for Post";
request.HttpMethod = "POST";
LoadRequest(request);
这是针对WkWebview
var request = new NSMutableUrlRequest(new NSUrl(new
NSString("Url - here")));
request.Body = "Data for Post";
request.HttpMethod = "POST";
request["Content-Length"] = request.Body.Length.ToString();
request["Content-Type"] = "application/x-www-form-urlencoded
charset=utf-8";
LoadRequest(request);您的html表单是否使用GET或POST?抱歉,应该使用NSMutableRequest来设置方法。我已更新了答案。可能您的网站有问题,因为我已使用“”进行了测试作为url,它正在WebView中打印我的帖子。它看起来像是在发布,但没有加载页面或其他内容。我删除了内容类型和内容长度,效果很好。这对我很有帮助,谢谢。没有“内容长度”标题,也没有“内容类型”结尾的“charset=utf-8”,效果很好标题。我已经使用WKWebView发送了这样的Post请求。
var request = new NSMutableUrlRequest(new NSUrl(new
NSString("Url - here")));
request.Body = "Data for Post";
request.HttpMethod = "POST";
request["Content-Length"] = request.Body.Length.ToString();
request["Content-Type"] = "application/x-www-form-urlencoded
charset=utf-8";