I am having some trouble posting to website..I've tried posting to many different websites and get the same result.. An error message referencing the js on the page, asking if i want to continue.. Then the response i get is the form I am posting to, with all fields correctly filled in. It looks like data was posted and never submitted?? I can't figure out where it's going wrong, and any help would be greatly appreciated. Here is my code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Diagnostics;


namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {


            WebRequest req = WebRequest.Create("https://propaccess.trueautomation.com/clientdb/?cid=68");
            string postData = "propertySearchOptions:searchType=Owner Name&propertySearchOptions:taxyear=2013&propertySearchOptions_ownerName=" + "xto";

            byte[] send = Encoding.Default.GetBytes(postData);
            req.Method = "POST";
            req.ContentType = "application/x-www-form-urlencoded";
            req.ContentLength = send.Length;

            Stream sout = req.GetRequestStream();
            sout.Write(send, 0, send.Length);
            sout.Flush();
            sout.Close();

            WebResponse res = req.GetResponse();
            StreamReader sr = new StreamReader(res.GetResponseStream());
            string returnvalue = sr.ReadToEnd();
            webBrowser1.DocumentText = returnvalue;


        }

    }
}

I think there is validation on that page. There are hidden input fields which generates a hash and it is then used to identify whether the request came from that website or from outside.

Does that mean this goal is not achievable?

You will need to scrape the website for the Anti-CSRF token that is attached to the form and make the request as though you are a user, not a program that is using an API.

If you search the forum for "FakeWebClient" you will find some code that will be able to emulate a browser inside your application, without having to actually have a browser displayed to the user.

Sorry, I am still new to C# and familiar only with Data Manipulation and IE Automation in VB so you lost me there.

On my search for "FakeWebClient" I found several links for different ways to Download files/pages using WebClient.. But not really what I was looking for.

When I started with C#, one of the first functions I made was something along the lines of:

WebClient client = new WebClient();
string reply = client.DownloadString("http://www.whatever.com");

Which works great, if there is no Form to fill or data to Post..I suspected a flaw in my code, and only reasoning is that no matter which website I used my code with, I got the same weird "js error message" followed by the same result being returned (Which was the form being posted to, with all fields filled out correctly, but that's it).. As if there was no response, or the form was filled out but not submitted. I've tried using this same code to fill out forms, and login to about a dozen different sites. Same result?

I meant for you to find this post: http://www.daniweb.com/software-development/csharp/code/457310/using-httpwebrequest-with-cookies

However, based on what you're saying it might be too advanced for you to adapt to your needs.

On the webform for the page you're trying to post to is an encryption token. Without this token, the page will not accept any data and will return an error to you.

The only solution is to extract this token from your response data and send it back as part of the form request (You can attach form data to the webclient)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.