将网页中的表单输入值加载到 URLRequest 中?

发布于 2024-07-30 09:21:46 字数 2365 浏览 2 评论 0原文

我如何提取下面 html 源的表单值并将它们添加到 NSURLConnection 中?

<tr>
                            <th><label for="username"><span class="accesskey">N</span>etID:</label></th>
                            <td><input id="username" name="username" class="required" tabindex="1" accesskey="n" type="text" value="" size="32" autocomplete="false"/></td>
                        </tr><tr>
                            <th><label for="password"><span class="accesskey">P</span>assword:</label></th>
                            <td><input id="password" name="password" class="required" tabindex="2" accesskey="p" type="password" value="" size="32"/></td>
                        </tr><tr>
                            <th><label for="authenticationType"><span class="accesskey">A</span>uthentication Type:</label></th>
                            <td>
                               <select id="authenticationType" name="authenticationType">
                                  <option value="Kerberos">Default</option>
                                  <option value="SafeWord">SafeWord</option>
                               </select>
                            </td>
                        </tr><tr>
                            <th></th>
                            <td>
                                <input type="checkbox" id="warn" name="warn" value="true" tabindex="3" />
                                <label for="warn" class="other" accesskey="w"><span class="accesskey">W</span>arn me before logging me into other sites.</label>
                            </td>
                        </tr><tr>
                            <th></th>
                            <td>
                                <input type="hidden" name="_currentStateId" value="" />
                                <input type="hidden" name="_eventId" value="submit" />
                                <input type="submit" accesskey="l" value="LOGIN" tabindex="4" />
                            </td>
                        </tr>

How would I pull out the form values for the html source below and add them to an NSURLConnection?

<tr>
                            <th><label for="username"><span class="accesskey">N</span>etID:</label></th>
                            <td><input id="username" name="username" class="required" tabindex="1" accesskey="n" type="text" value="" size="32" autocomplete="false"/></td>
                        </tr><tr>
                            <th><label for="password"><span class="accesskey">P</span>assword:</label></th>
                            <td><input id="password" name="password" class="required" tabindex="2" accesskey="p" type="password" value="" size="32"/></td>
                        </tr><tr>
                            <th><label for="authenticationType"><span class="accesskey">A</span>uthentication Type:</label></th>
                            <td>
                               <select id="authenticationType" name="authenticationType">
                                  <option value="Kerberos">Default</option>
                                  <option value="SafeWord">SafeWord</option>
                               </select>
                            </td>
                        </tr><tr>
                            <th></th>
                            <td>
                                <input type="checkbox" id="warn" name="warn" value="true" tabindex="3" />
                                <label for="warn" class="other" accesskey="w"><span class="accesskey">W</span>arn me before logging me into other sites.</label>
                            </td>
                        </tr><tr>
                            <th></th>
                            <td>
                                <input type="hidden" name="_currentStateId" value="" />
                                <input type="hidden" name="_eventId" value="submit" />
                                <input type="submit" accesskey="l" value="LOGIN" tabindex="4" />
                            </td>
                        </tr>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最冷一天 2024-08-06 09:21:46

如果您知道 HTML 也是有效的 XML,则可以通过 NSXMLParser(或其他 XML 解析代码)运行整个过程,并在检测到作为表单输入的元素(例如 ;)。

否则,您最好编写自己的字符串解析函数 - 查找您想要抓取的标签的任何实例,提取 << 之间的所有文本。 和> 字符,用空格分隔,然后找到输入字段的名称,以便将其放入 NSURLConnection 中。

If you know the HTML is also valid XML, you can run the whole thing through an NSXMLParser (or other XML-parsing code) and perform some action whenever it detects an element that is a form input (such as <input>).

Otherwise, you might be better off just writing your own string-parsing function(s) - look for any instance of the tag you want to grab, pull all the text between the < and > characters, split it by spaces, and then find the name of the input field so you can put it into an NSURLConnection.

无力看清 2024-08-06 09:21:46

我使用了elementParser。 谢谢!

I used elementParser. Thanks!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文