Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Because of its limited functionality, I avoid using <code>WebClient</code> and use <code>WebRequest</code> instead. The code below: </p> <ol> <li>does not expect an HTTP 100 status code to be returned,</li> <li>creates a <code>CookieContainer</code> to store any cookies we pick up, </li> <li>sets the <em>Content Length</em> header, and</li> <li><code>UrlEncode</code>s each value in the post data.</li> </ol> <p>Give the following a try and see if it works for you.</p> <pre><code>System.Net.ServicePointManager.Expect100Continue = false; System.Net.CookieContainer cookies = new System.Net.CookieContainer(); // this first request just ensures we have a session cookie, if one exists System.Net.WebRequest req = System.Net.WebRequest.Create("http://localhost/test.aspx"); ((System.Net.HttpWebRequest)req).CookieContainer = cookies; req.GetResponse().Close(); // this request submits the data to the server req = System.Net.WebRequest.Create("http://localhost/test.aspx"); req.ContentType = "application/x-www-form-urlencoded"; req.Method = "POST"; ((System.Net.HttpWebRequest)req).CookieContainer = cookies; string parms = string.Format("email={0}&amp;password={1}", System.Web.HttpUtility.UrlEncode("e2@email.com"), System.Web.HttpUtility.UrlEncode("hunter2")); byte[] bytes = System.Text.Encoding.ASCII.GetBytes(parms); req.ContentLength = bytes.Length; // perform the POST using (System.IO.Stream os = req.GetRequestStream()) { os.Write(bytes, 0, bytes.Length); } // read the response string response; using (System.Net.WebResponse resp = req.GetResponse()) { if (resp == null) return; using (System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream())) { response = sr.ReadToEnd().Trim(); } } // the variable response holds the results of the request... </code></pre> <p>Credits: <a href="http://www.hanselman.com/blog/HTTPPOSTsAndHTTPGETsWithWebClientAndCAndFakingAPostBack.aspx" rel="nofollow noreferrer">Hanselman</a>, <a href="https://stackoverflow.com/questions/2938330/httpwebrequest-response-produces-http-422-why/2939727#2939727">Simon</a> (SO Question)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload