Question

I want to post data to a website. I handle the cookies with this code :

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

I handle viewstate value with this code:

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

and finally I'm posting data with this code:

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

My main problem is viewState because if I don't post viewstate it returns me the same page or if I parse viewstate value before the posting data and post with data it returns me your session timed out. For example I'm able to login facebook but I could not post data to website which uses viewState. I couldn't figure out this situation. I think I have to parse viewstate in the same request with post but I know that webrequest can not be reused. Can you help me?

Was it helpful?

Solution

I'm using CsQuery lib. It's helpful and easy to use. https://www.nuget.org/packages/CsQuery

Use some class like that:

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

Setup the post parameters as you want. Then just do:

        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);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top