编程语言
首页 > 编程语言> > c# – 使用cookie和viewState将数据发布到网站

c# – 使用cookie和viewState将数据发布到网站

作者:互联网

我想将数据发布到网站上.我使用以下代码处理cookie:

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

我使用以下代码处理viewstate值:

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来解析同一个请求中的viewstate,但我知道webrequest不能被重用.你能帮助我吗?

解决方法:

我正在使用CsQuery lib.它有用且易于使用. 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));
    }
}

根据需要设置post参数.
然后就做:

        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);

标签:c,http-post,viewstate
来源: https://codeday.me/bug/20190629/1323037.html