コード例 #1
0
        internal static IHttpObservable Submit <T>(IHttpClient http,
                                                   ParsedHtml html,
                                                   string formSelector, int?formIndex, Uri actionUrl,
                                                   Func <HtmlForm, ISubmissionData <T> > submissions)
        {
            var forms =
                from f in formIndex == null
                          ? html.QueryFormSelectorAll(formSelector)
                          : formIndex < html.Forms.Count
                          ? Enumerable.Repeat(html.Forms[(int)formIndex], 1)
                          : Enumerable.Empty <HtmlForm>()
                select new
            {
                Object = f,
                Action = new Uri(html.TryBaseHref(f.Action), UriKind.Absolute),
                // f.EncType, // TODO validate
                Data = f.GetSubmissionData(),
            };

            var form = forms.FirstOrDefault();

            if (form == null)
            {
                throw new Exception("No HTML form for submit.");
            }

            submissions(form.Object).Run(form.Data);

            return(form.Object.Method == HtmlFormMethod.Post
                 ? http.Post(actionUrl ?? form.Action, form.Data)
                 : http.Get(new UriBuilder(actionUrl ?? form.Action)
            {
                Query = form.Data.ToW3FormEncoded()
            }.Uri));
        }
コード例 #2
0
ファイル: HttpQuery.cs プロジェクト: gusty/WebLinq
        static IQuery <HttpFetch <HttpContent> > Submit(ParsedHtml html,
                                                        string formSelector, int?formIndex,
                                                        NameValueCollection data)
        {
            var forms =
                from f in formIndex == null
                          ? html.QueryFormSelectorAll(formSelector)
                          : formIndex < html.Forms.Count
                          ? Enumerable.Repeat(html.Forms[(int)formIndex], 1)
                          : Enumerable.Empty <HtmlForm>()
                select new
            {
                Action = new Uri(html.TryBaseHref(f.Action), UriKind.Absolute),
                f.Method,
                f.EncType,     // TODO validate
                Data = f.GetSubmissionData(),
            };

            var form = forms.FirstOrDefault();

            if (form == null)
            {
                throw new Exception("No HTML form for submit.");
            }

            if (data != null)
            {
                foreach (var e in data.AsEnumerable())
                {
                    form.Data.Remove(e.Key);
                    if (e.Value.Length == 1 && e.Value[0] == null)
                    {
                        continue;
                    }
                    foreach (var value in e.Value)
                    {
                        form.Data.Add(e.Key, value);
                    }
                }
            }

            return(form.Method == HtmlFormMethod.Post
                 ? Http.Post(form.Action, form.Data)
                 : Http.Get(new UriBuilder(form.Action)
            {
                Query = form.Data.ToW3FormEncoded()
            }.Uri));
        }