【问题标题】:Post data to website with cookies and viewState使用 cookie 和 viewState 将数据发布到网站
【发布时间】:2014-02-15 12:15:59
【问题描述】:

我想将数据发布到网站。我用这段代码处理 cookie:

CookieCollection cookies = new CookieCollection();
         request.CookieContainer = new CookieContainer();
              request.CookieContainer.Add(cookies);     HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

我使用以下代码处理视图状态值:

var doc = new HtmlAgilityPack.HtmlDocument();
              doc.Load(resp.GetResponseStream());
              foreach (HtmlNode input in doc.DocumentNode.SelectNodes("//input"))
              {                   
               if (input.Attributes["value"] != null)
                  {
                  val = input.Attributes["value"].Value;
                   if (val.IndexOf("1")!=-1)
                      {
                         viewState = val;
                      }
                   }
              }

最后我用这段代码发布数据:

 HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                         cookies = response.Cookies;
                       response.Close();
                       string getUrl = "url";
                       string postData = String.Format(""+viewstate);
                       HttpWebRequest getRequest = (HttpWebRequest)WebRequest.Create(getUrl);
                       getRequest.CookieContainer = new CookieContainer();
                       getRequest.CookieContainer.Add(cookies);

我的主要问题是 viewState,因为如果我不发布 viewstate,它会返回相同的页面,或者如果我在发布数据之前解析 viewstate 值并发布数据,它会返回你的会话超时。例如,我可以登录 facebook,但无法将数据发布到使用 viewState 的网站。我无法弄清楚这种情况。我想我必须在与 post 相同的请求中解析视图状态,但我知道 webrequest 不能被重用。你能帮帮我吗?

【问题讨论】:

  • 你能分享链接和你想做什么吗?
  • apps.db.ripe.net/syncupdates/simple-rpsl.html

标签: c# http-post viewstate


【解决方案1】:

我正在使用 CsQuery 库。它很有帮助且易于使用。 https://www.nuget.org/packages/CsQuery

使用类似的类:

public class WebClientEx : WebClient
{
    public CookieContainer _cookies = new CookieContainer();

    public string Get(string URL)
    {
        return this.DownloadString(URL);
    }

    public WebClientEx()
    {
        this.Encoding = Encoding.UTF8;
        System.Net.ServicePointManager.Expect100Continue = false;
    }

    protected override WebRequest GetWebRequest(Uri address)
    {

        var request = (HttpWebRequest)base.GetWebRequest(address);
        request.Host = "apps.db.ripe.net";
        request.UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64; rv:24.0) Gecko/20100101 Firefox/24.0";
        request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
        request.KeepAlive = true;
        request.Proxy = new WebProxy();
        request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
        request.CookieContainer = _cookies;
        request.Headers.Add("Accept-Language", "en-us;q=0.7,en;q=0.3");
        request.ContentType = "application/x-www-form-urlencoded";

        return request;
    }

    public string Post(string URL, NameValueCollection data)
    {
        return this.Encoding.GetString(this.UploadValues(URL, data));
    }
}

根据需要设置帖子参数。 然后就做:

        WebClientEx client = new WebClientEx();
        string Page = client.Get("https://apps.db.ripe.net/syncupdates/simple-rpsl.html"); //get some cookies and viewstate

        CQ dom = CQ.Create(Page);
        string viewstate = dom["input#javax\\.faces\\.ViewState"].Val(); //get viewstate

        NameValueCollection data = new NameValueCollection();
        data.Add("rpslBox:postRpsl", "rpslBox:postRpsl");
        data.Add("rpslBox:postRpsl:sourceRadioSelect", "RIPE_NCC");
        data.Add("rpslBox:postRpsl:rpslObject", "your some string"); //your string
        data.Add("rpslBox:postRpsl:update", "Update");
        data.Add("javax.faces.ViewState", viewstate);

        Page = client.Post("https://apps.db.ripe.net/syncupdates/simple-rpsl.html", data);

【讨论】:

  • 我收到错误:找不到类型或命名空间 NameValueCollection 为什么?
  • 正确添加使用:System.Collections.Specialized
  • 感谢它工作得很好。我还有一个问题。我不明白它是如何工作的。它在一个连接中发出多个请求?
  • 这很简单。首先我们创建类 WebClientEx。它有两种方法 Get 和 Post。然后我们获取我们的页面来解析视图状态值。之后,当我们有视图状态时,我们可以将 Post 请求发送到我们想要发送的 post 数据的 url。
  • 我无法在不同的文件夹中运行 exe,因为程序找不到 csquery.dll 。我如何在我的代码中确定 dll 的路径?
猜你喜欢
  • 1970-01-01
  • 2012-04-18
  • 2012-01-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多