I'm trying to Post to a site using WebClient in C# but it doesn't seem to Post the values at all

I try posting two strings to a testform that I have set up on my localhost. In the PostBack of the page I update a label to say "Login Succesfull" if the correct username/password have been entered. But the PostBack code is never triggered to begin with after the code below runs. It just returns the login page as the response. What am I doing wrong?

string msSite = "http://localhost/testform";
WebClient oClient = new WebClient();
oClient.QueryString.Add("username", "test");
oClient.QueryString.Add("password", "test");
string msConnect = oClient.DownloadString(msSite);

Hello coder 2050 and welcome to Daniweb! :)

Please use code tags when you paste code on Daniweb, it helps keep the threads clean.

To answer your question you will want to do something like this. Also -- Are you sure that you shouldn't be submitting form fields instead of the query string?

private static void DoPost()
    {
      string userName = "test";
      string password = "test";
      string URL = string.Format(@"http://www.website.com/login.aspx?user={0}&password={1}", userName, password);
      HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(URL);
      req.Method = "POST";
      using (Stream s = req.GetRequestStream())
      {
        string postData = "a";
        byte[] bPostData = System.Text.ASCIIEncoding.UTF8.GetBytes(postData);
        s.Write(bPostData, 0, bPostData.Length);
        s.Flush();
        s.Close();
        s.Dispose();
        WebResponse resp = req.GetResponse();
        resp.Close();
      }
    }

Hey sknake,

I tried the code below and I end up with the same thing. It doesn't seem to trigger a postback when I look at the content returned from the webresponse.

What i'm trying to do is
1) Log into a Form on a website with Username/Password
2) Automatically fill in the start and end date fields on the form that is returned after a succesful login
3) POST these final values and get back a csv file

How would I submit these are form fields?

Have you looked on google?

/* 
 * Required references 
 */ 
 using System.Net; 
 using System.IO; 
/* 
 * Usage. This url is real and you can use it for testing. 
 */ 
  string url = "http://www.nicecleanexample.com"; 
  url += "/PublicTest/autosubmit.php?cmd=login"; 
  
  oRequest = WebRequest.Create(url); 
/* 
 * Set values so server expects post data 
 */ 
  oRequest.Method = "POST"; 
  oRequest.ContentType = "application/x-www-form-urlencoded"; 
/* 
 * Must url encode data going to server 
 */ 
  string postdata = "txtName=my%20name&txtPassword=secret"; 
  oRequest.ContentLength = postdata.Length; 
/* 
 * Send post data into request stream first 
 */ 
  StreamWriter sw = new StreamWriter(oRequest.GetRequestStream()); 
  sw.Write(postdata); 
  sw.Close(); 
/* 
 * Connect, send and get response 
 */ 
  oResponse = oRequest.GetResponse(); 
  StreamReader sr = new StreamReader(oResponse.GetResponseStream()); 
  
  string pagedata = sr.ReadToEnd();

Taken from: http://www.nicecleanexample.com/ViewSnippet.aspx?TID=dotnet_submitform

After searching: http://www.google.com/search?hl=en&q=C%23+submit+form+fields&aq=f&oq=&aqi=

If you still have issues then post your new code and we'll go from there.

Checked multiple sites.

Does the above code you pasted work for you when you try to Post? If you were to try and Post to a test login page would it perform a Postback on the login page or just display the page as if it had just loaded?

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.