C#和JavaScript。 [英] C# and JavaScript.

查看:85
本文介绍了C#和JavaScript。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


前段时间我写了一些自动化工具来获取某些网站的内容。

一切正常,直到有一天...


我的C#代码创建了url字符串并使http命中。我使用了HttpRequest和Response。

请求看起来像http://somesite.con/param1=a& param2 = b& param3 = c

现在,有一个变化在网站中,它总是重定向到看起来像这样的网址:

http://somesite.con/param1=a& param2 = b& param3 = c& rnd = AbCdEfG1。

使用小提琴手,我看到网页浏览器有2次点击:

1.旧版本。

2.新版本。

现在第一个命中的响应包含一些JS代码,它生成这个随机代码(AbCdEfG1),将它连接到主URL并进行重定向。


就我而言明白我需要从C#执行这个JS。这可能是以几种方式完成的,似乎并不太复杂。

但是我需要或者:

a。从JS中取回这个随机值。

b。允许JS进行重定向,但需要取回响应。

我更喜欢b。因为生成这个随机值的代码总是不同的(代码生成或其他东西)。


JS的代码是这样的:

展开 | 选择 | Wrap | 行号

解决方案

我不完全理解你的问题。

用于代码的随机位是什么?



无论如何,你无法在C#代码中执行JavaScript代码。 JavaScript在Web浏览器中运行,C#代码在Web服务器上运行。


因此,没有办法从C#代码中执行JavaScript ...


你真的想做什么?


@Frinavale


首先感谢您的回复。<那么,有一种方法可以通过使用WebBrowser对象及其方法InvokeSkript从C#执行JS代码。问题是,当我使用这种方法时,我无法得到回复以供将来解析。


正如我所说,我正在解析一些网站的内容很多广告内容。

前段时间我只需要发出http命中并从服务器获得回复。

现在服务器没有返回回复它在此之前。它通过向url添加由第一个代码中显示的代码生成的一些随机字符串来进行重定向。

代码也在我访问服务器时生成了randomaly。然后它用生成的版本重定向我的命中。


我点击它看起来像这样:
http://somesite.com/param1=123

对此匹配的回复包含您可以在第一篇文章。

代码添加到我的命中& rnd = A(例如)然后用新网址命中( http://somesite.com/param1=123&rnd=A )。


希望它能解决这个问题。

谢谢。


抱歉,由于某种原因(不知道为什么)我以为你正在开发一个C#Web应用程序。 ..不是一个连接到网站的桌面应用程序。我无法帮助你,因为我没有使用你正在使用的工具的经验。


祝你好运,

-Frinny

Hi all,

Sometime ago I wrote some automation tool to get content of some site.
Everything worked fine untill one day...

My C# code creted url string and made http hit. I used HttpRequest and Response.
Request looked like http://somesite.con/param1=a&param2=b&param3=c
Now, there was a change in the site and it always makes redirect to the url that looks like:
http://somesite.con/param1=a&param2=b&param3=c&rnd=AbCdEfG1.
With the fiddler I saw that actially web browser makes 2 hits:
1. The old one.
2. The new one.
Now the response of the first hit contains some JS code that generates this random code (AbCdEfG1), concatinates it to the main url and makes redirect.

As far as I understand I need to execute this JS from C#. It''s may be done in the several ways and seems not too complex to do.
But then I need to or:
a. get back this random value from JS.
b. allow to JS to make redirection, but need to get back the response.
I prefer the b. because code that generates this random value always different (code generation or something).

The code of JS is something like that:

Expand|Select|Wrap|Line Numbers

解决方案

I don''t completely understand your problem.
What is the random bit of code used for?


Anyways, there''s no way for you to execute JavaScript code in your C# code. JavaScript runs in the web browser, and C# code runs on the web server.

So, there''s no way to execute JavaScript from your C# code...

What are you really trying to do?


@Frinavale
First of all thanks for reply.


Then, there is a way to execute JS code from C# by using WebBrowser object and its method InvokeSkript. The problem is when I use this method I can''t get reply for future parsing.


As I said, I''m parsing a content of some site with lots of advertising content.
Some time ago I just to need to make http hit and get the reply from the server.
Now server doesn''t return the reply as it was before. It makes redirection by adding to url some random string generated by code I showed in the first code.
The code also generated randomaly, at the time I access the server. And then it redirects my hit with edition that was generated.

I make hit that looks like this:
http://somesite.com/param1=123
The reply to this hit contains the code you can see in the first post.
The code adds to my hit &rnd=A (for example) and then makes hit with the new url (http://somesite.com/param1=123&rnd=A).

Hope it clears the question.
Thanks.


Sorry, for some reason (don''t know why) I thought you were developing a C# web application...not a desktop application that''s connecting to a website. I can''t help you with this because I have no experience working with the tools you''re working.

Best of luck,
-Frinny


这篇关于C#和JavaScript。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆