HttpWebRequest 与从 javascript 动态生成 html 的页面?

发布于 2024-11-02 15:19:55 字数 1217 浏览 0 评论 0原文

这是一个由两部分组成的问题。是否可以使用 WebClient/HttpWebRequest 从通过 javascript 动态加载内容的页面中检索数据?

我还想知道如何使用 WebClient/HttpWebRequest 来复制 XMLHttpRequest,就像您通过 javascript 执行的那样。

编辑: 我捕获了我尝试复制的请求的标头,如下所示: http://www.tagged.com/api/?application_id=user&format=json&session_token=6thk20fhv7d727emgdhfka6034

POST /api/?application_id=user&format=json&session_token=6thk20fhv7d727emgdhfka6034 HTTP/1.1 主机:www.tagged.com

接受:text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8 接受语言:en-us,en;q=0.5

接受编码:gzip,deflate

接受字符集:ISO-8859-1,utf-8;q=0.7,*;q=0.7

X-Requested-With:XMLHttpRequest

内容类型:application/x-www-form-urlencoded;字符集=UTF-8

内容长度:88

connect_status=-1; __utmb=50703532.0.10.1303366930

DNT: 1

Connection: keep-alive

Pragma: no-cache

Cache-Control: no-cache

method=tagged.usermgmt.addFriend&uid_to_add=5402501977&api_signature=&track=1mJ0lY7-W3

我明白一切直到 方法。据我所知,您只能向 HttpWebRequest.Method 提供 GET 或 POST。有人可以帮我填补空白吗?

This is kind of a two part question. Is it possible to retrieve data from a page that loads content dynamically through javascript by using WebClient/HttpWebRequest?

I'd also like to how I would be able to use WebClient/HttpWebRequest to replicate a XMLHttpRequest like you would see executed via javascript.

Edit:
I captured the headers of the request I am trying to replicate which looks like this:
http://www.tagged.com/api/?application_id=user&format=json&session_token=6thk20fhv7d727emgdhfka6034

POST /api/?application_id=user&format=json&session_token=6thk20fhv7d727emgdhfka6034 HTTP/1.1
Host: www.tagged.com

Accept: text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8
Accept-Language: en-us,en;q=0.5

Accept-Encoding: gzip,deflate

Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7

X-Requested-With: XMLHttpRequest

Content-Type: application/x-www-form-urlencoded; charset=UTF-8

Content-Length: 88

connect_status=-1; __utmb=50703532.0.10.1303366930

DNT: 1

Connection: keep-alive

Pragma: no-cache

Cache-Control: no-cache

method=tagged.usermgmt.addFriend&uid_to_add=5402501977&api_signature=&track=1mJ0lY7-W3

I understand everything up until the method. As far as I know you can only supply GET or POST to HttpWebRequest.Method. Could someone maybe fill in the blanks for me?

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

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

发布评论

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

评论(2

樱&纷飞 2024-11-09 15:19:55

如果您尝试以与 javascript 相同的方式与服务器对话,那么您可能有一些选择。

首先,您必须确定这些页面如何与服务器通信。例如,JSON 或 SOAP。您可以使用网络嗅探器(例如 wireshark)来执行此操作。

完成此操作后,您可以使用可用的 C# JSON 解析器之一(例如 JSON.NET

对于 SOAP,您可以使用类似的方法。请注意,他们可能不喜欢您以这种方式与他们的 API 交谈。

If you are trying to talk to the server in the same manner that javascript does however you might have some options.

First, you have to determine how those pages communicate with the server. For example, JSON or SOAP. You can do this with a network sniffer such as wireshark.

Once you have done this, you can send a JSON request of your own to that server using and parse the JSON response using one of the available C# JSON parsers such as JSON.NET.

You can use a similar method in the case of SOAP. Be mindful they might not like you talking to their API's this way.

陌生 2024-11-09 15:19:55

是否可以使用 WebClient/HttpWebRequest 从通过 JavaScript 动态加载内容的页面检索数据?

不 - 您可以检索执行 JavaScript 所需的所有 JavaScript 文件和其他文档,但 HttpWebRequest 类不会为您执行该 JavaScript - 这完全是一个问题更复杂的任务。

如果您想这样做,那么您可以在应用程序中托管一个 Web 浏览器,让浏览器打开并“渲染”页面,然后检查结果。不过,一般来说,根据具体情况提出替代解决方案要容易得多,例如,如果 JavaScript 通过 AJAX 请求填充页面,那么您只需直接运行该请求即可。

我是否能够使用 WebClient/HttpWebRequest 来复制 XMLHttpRequest,就像您通过 javascript 执行的那样?

如果您的意思是“我可以使用 HttpWebRequest 来执行类似于客户端 JavaScript 执行的 AJAX 请求的 HTTP 请求吗”,那么答案是肯定的,但是具体如何执行此操作将取决于 JavaScript 和参数用于执行AJAX请求。

在这种情况下,Web 调试工具(例如 Fiddler)很有用,因为它允许您检查和比较正在提出的请求。

Is it possible to retrieve data from a page that loads content dynamically through javascript by using WebClient/HttpWebRequest?

Nope - you can retrieve all of the JavaScript files and other documents required to execute the JavaScript, but the HttpWebRequest class won't execute that JavaScript for you - this is an altogether more complex task.

If you wanted to do this then you could host a web browser inside your application, get the browser to open and "render" the page, then inspect the result. In general however is far easier just to come up with an alternative solution based on the situation, for example if the JavaScript populates the page from an AJAX request then just run the request yourself directly.

Would I be able to use WebClient/HttpWebRequest to replicate a XMLHttpRequest like you would see executed via javascript?

If you mean "Can I use HttpWebRequest to execute a HTTP request similar to an AJAX request executed by client JavaScript" then the answer is yes, however exactly how you do this will depend on the JavaScript and the parameters used to exeucte the AJAX request.

In this situation a web debugging tool (such as Fiddler) is useful as it allows you to inspect and compare the request being made.

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