如何在C#中复制此JavaScript

时间:2012-03-27 16:15:00

标签: c# javascript httpwebrequest

这是JavaScript

function ChangeVol(id)
{
    document.form.selectFS_devId.value = id;
    document.form.selectFS_currentNameSpace.value = "";
    document.form.submit();
}
function ChangeEvsVol(id, vNodeId)
{
    document.form.selectFS_evsId.value = vNodeId;
    document.form.selectFS_currentNameSpace.value = "";
    ChangeVol(id);
}

document.form.selectFS_devId.value = "all"  
document.form.selectFS_evsId.value = "2"

这是我正在使用的当前C#代码

Uri url = new Uri("https://mgr/app");
HttpWebRequest request = null;

ServicePointManager.ServerCertificateValidationCallback =
   ((sender, certificate, chain, sslPolicyErrors) => true);
CookieContainer cookieJar = new CookieContainer();

request = (HttpWebRequest)WebRequest.Create(url);
request.CookieContainer = cookieJar;
request.Method = "GET";
HttpStatusCode responseStatus;

using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
     responseStatus = response.StatusCode;
     url = request.Address;
}

if (responseStatus == HttpStatusCode.OK)
{
      UriBuilder urlBuilder = new UriBuilder(url);
      urlBuilder.Path = 
          urlBuilder.Path.Remove(urlBuilder.Path.LastIndexOf('/')) + 
          "/j_security_check";

      request = (HttpWebRequest)WebRequest.Create(urlBuilder.ToString());
      request.Referer = url.ToString();
      request.CookieContainer = cookieJar;
      request.Method = "POST";
      request.ContentType = "application/x-www-form-urlencoded";

      using (Stream requestStream = request.GetRequestStream())
      using (StreamWriter requestWriter = new StreamWriter(requestStream, Encoding.ASCII))
      {
           string postData = "j_username=user&j_password=user&submit=Send";
           requestWriter.Write(postData);
      }

      string responseContent = null;
      string myTargetString = "https://mgr/app/action/storage.VivolAction/eventsubmit_dopreparevivollist/ignored/f5/true";
      using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
      using (Stream responseStream = response.GetResponseStream())
      using (StreamReader responseReader = new StreamReader(responseStream))
      {
         responseContent = responseReader.ReadToEnd();
      }
      Console.WriteLine(responseContent);
      request = (HttpWebRequest)WebRequest.Create(myTargetString);
      request.Method = "GET";
      request.CookieContainer = cookieJar;
      using (HttpWebResponse responsedownload = (HttpWebResponse)request.GetResponse())
      using (Stream responseStream = responsedownload.GetResponseStream())
      using (StreamReader responseReader = new StreamReader(responseStream))
      {
            responseContent = responseReader.ReadToEnd();
      }
      Console.WriteLine(responseContent);

问题是字符串myTargetString没有加载javascript参数,如果我可以复制URL中的那些参数会很棒,如果没有,我需要做什么来提交像我上面的帖子请求中的那些在StreamWriter?

using (StreamWriter requestWriter = new StreamWriter(requestStream, Encoding.ASCII))
            {
                string postData = "j_username=user&j_password=user&submit=Send";
                requestWriter.Write(postData);
            }

我在网址中的含义可能是:

https://mgr/app/action/storage.VivolAction/eventsubmit_dopreparevivollist/ignored?&evsId=1&devId=all&currentpagenumberbottom=1&filtername=&currentpagenumber=1&quotaactionlink=/mgr/app/action/storage.VivolQuotaAction&ascending=true&currentpagesize=20&ignoreErrorMessages=true&pageindex=1&sortby=name&filterpath=

Fiddler向我提供了这个

POST https://mgr/app/action/storage.SelectFileSystemAction/eventsubmit_doprocessselectfilesystem/ignored  
Accept: image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash,   application/xaml+xml, application/x-ms-xbap, application/x-ms-application,   application/vnd.ms-xpsdocument, application/vnd.ms-excel, application/vnd.ms-powerpoint,   application/msword, */*  
Referer: https://mgr/app/action/storage.SelectFileSystemAction  /eventsubmit_doprepareselectfilesystem/ignored  
Accept-Language: en-us  
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0)  
Content-Type: application/x-www-form-urlencoded  
Accept-Encoding: gzip, deflate  
Host: arc  
Content-Length: 378  
Connection: Keep-Alive  
Cache-Control: no-cache  
Cookie: jid=asdsad ; jsso = asdas2sa    
op=&selectFS_devId=all&selectFS_previous_template=&selectFS_evsId=2&selectFS_currentNameSpace=&selectFS_action_class=storage.VivolAction&selectFS_action_method=doPreparevivollist&selectFS_uniqueId=13655b454e3951462f&selectFS_dont_alter_current=false&selectFS_disableReplicationTargets=true&selectFS_disableReadCache=true&selectFS_disableWorm=false&selectFS_disableUnmounted=true

我可以在其中看到electFS_devId=allselectFS_evsId=2,我需要更改EVSID,但我不确定如何构建URL。是的我改变了cookie id

3 个答案:

答案 0 :(得分:0)

您的javascript只是在执行提交之前为表单上可能隐藏的字段设置表单值。您需要执行POST请求,方法与登录时相同。

查看HTML中表单标记的操作值,以确定提交数据的位置并将以下表单项放入postData:

  1. selectFS_devId
  2. selectFS_currentNameSpace
  3. selectFS_evsId
  4. 您可以使用以下内容:

    string postData = string.Format("selectFS_currentNameSpace={0}&selectFS_evsId={1}&selectFS_devId={2}", "", "2", "all");
    

    看起来这会涉及到,因为它似乎使用selectFS_uniqueId进行某种形式的会话或事务跟踪,您可能必须首先执行GET操作,然后从表单中提取该值。另外,请注意您的提交位置与之前的j_security_check一样,不会与检索表单的GET(doprepare)进入POST(doprocess)的相同位置。

    GET https://mgr/app/action/storage.SelectFileSystemAction/eventsubmit_doprepareselectfilesystem/ignored

    POST https://mgr/app/action/storage.SelectFileSystemAction/eventsubmit_doprocessselectfilesystem/ignored

    在这里再看一下你的帖子值。很明显,有一个命令发布了一个类(selectFS_action_class)和方法(selectFS_action_method),如果没有指定它们,可能什么都不会完成。

    op=
    &selectFS_devId=all
    &selectFS_previous_template=
    &selectFS_evsId=2
    &selectFS_currentNameSpace=
    &selectFS_action_class=storage.VivolAction
    &selectFS_action_method=doPreparevivollist
    &selectFS_uniqueId=13655b454e3951462f
    &selectFS_dont_alter_current=false
    &selectFS_disableReplicationTargets=true
    &selectFS_disableReadCache=true
    &selectFS_disableWorm=false
    &selectFS_disableUnmounted=true
    

    与其编写所有这些内容来模拟通过Web界面进行操作的用户,您是否已使用F5检查他们是否有可以调用的Web服务来执行此操作?

答案 1 :(得分:0)

也许问题在于,这种形式的参数“j_username = user& j_password = user& submit = Send”是HTTP“GET”而非“POST”的参数,您可以尝试以这种方式获取参数。 C#

var operacion = context.Request.Form[0] "POST"
var operacion = context.Request.Params[0];

Request.Querystring("parameterName") for "GET"

或在javascript中你可以使用$ _GET或$ _POST

答案 2 :(得分:-1)

我认为你的问题是渴望得到你所要求的。另外,您没有显示有关在浏览器中发布的任何代码。

好像你只想知道如何让表单发布工作......我注意到你没有设置contentlength属性......这可能与它有关。另外,看看这个“帖子”......

How to post data to specific URL using WebClient in C#